diff options
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 14 | ||||
-rw-r--r-- | examples/direct_bt_scanner00/dbt_scanner00.cpp | 5 | ||||
-rw-r--r-- | examples/direct_bt_scanner01/dbt_scanner01.cpp | 5 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 5 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 5 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 5 | ||||
-rw-r--r-- | examples/java/ScannerTinyB02.java | 5 | ||||
-rw-r--r-- | java/direct_bt/tinyb/DBTAdapter.java | 37 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 54 | ||||
-rw-r--r-- | java/org/tinyb/AdapterStatusListener.java | 8 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothAdapter.java | 17 | ||||
-rw-r--r-- | java/org/tinyb/ScanType.java | 72 | ||||
-rw-r--r-- | java/tinyb/dbus/DBusAdapter.java | 5 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 27 |
14 files changed, 199 insertions, 65 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index e43d447b..c9598a41 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -94,16 +94,16 @@ namespace direct_bt { /** * DBTAdapter's discovery state has changed, i.e. enabled or disabled. - * <p> - * FIXME: Method shall include the enabled/disabled ScanType plus the current overall ScanType state. - * For now, we only report LE discovery state. - * </p> * @param adapter the adapter which discovering state has changed. - * @param enabled the new discovery state - * @param keepAlive if {@code true}, the discovery will be re-enabled if disabled by the underlying Bluetooth implementation. + * @param currentMeta the current meta ScanType + * @param changedType denotes the changed ScanType + * @param changedEnabled denotes whether the changed ScanType has been enabled or disabled + * @param keepAlive if {@code true}, the denoted changed ScanType will be re-enabled if disabled by the underlying Bluetooth implementation. * @param timestamp the time in monotonic milliseconds when this event occurred. See BasicTypes::getCurrentMilliseconds(). + * + * changeScanType(const ScanType current, const bool enable, const ScanType enableChanged) noexcept { */ - virtual void discoveringChanged(DBTAdapter &adapter, const bool enabled, const bool keepAlive, const uint64_t timestamp) = 0; + virtual void discoveringChanged(DBTAdapter &adapter, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) = 0; /** * A DBTDevice has been newly discovered. diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index 17328897..ede0ea95 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -64,8 +64,9 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { - fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); + void discoveringChanged(DBTAdapter &a, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) override { + fprintf(stderr, "****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n", + getScanTypeString(currentMeta).c_str(), getScanTypeString(changedType).c_str(), changedEnabled, keepAlive, a.toString().c_str()); (void)timestamp; } diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index 9f71bf27..9759faa6 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -63,8 +63,9 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { - fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); + void discoveringChanged(DBTAdapter &a, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) override { + fprintf(stderr, "****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n", + getScanTypeString(currentMeta).c_str(), getScanTypeString(changedType).c_str(), changedEnabled, keepAlive, a.toString().c_str()); (void)timestamp; } diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index c20b02cc..b89c498b 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -172,8 +172,9 @@ class MyAdapterStatusListener : public AdapterStatusListener { } } - void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { - fprintf(stderr, "****** DISCOVERING: enabled %d, keepAlive %d: %s\n", enabled, keepAlive, a.toString().c_str()); + void discoveringChanged(DBTAdapter &a, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) override { + fprintf(stderr, "****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n", + getScanTypeString(currentMeta).c_str(), getScanTypeString(changedType).c_str(), changedEnabled, keepAlive, a.toString().c_str()); (void)timestamp; } diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index 478d7c48..843cf54e 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -53,6 +53,7 @@ import org.tinyb.GATTCharacteristicListener; import org.tinyb.HCIStatusCode; import org.tinyb.HCIWhitelistConnectType; import org.tinyb.PairingMode; +import org.tinyb.ScanType; import direct_bt.tinyb.DBTManager; @@ -134,8 +135,8 @@ public class DBTScanner10 { } @Override - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { - println("****** DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter); + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final boolean keepAlive, final long timestamp) { + println("****** DISCOVERING: meta "+currentMeta+", changed["+changedType+", enabled "+changedEnabled+", keepAlive "+keepAlive+"] on "+adapter); } @Override diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index cfd45ec9..533b3160 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -44,6 +44,7 @@ import org.tinyb.BluetoothUtils; import org.tinyb.EIRDataTypeSet; import org.tinyb.GATTCharacteristicListener; import org.tinyb.HCIStatusCode; +import org.tinyb.ScanType; /** * This Java scanner example is a TinyB backward compatible and not fully event driven. @@ -167,8 +168,8 @@ public class ScannerTinyB01 { } @Override - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { - System.err.println("****** DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter); + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final boolean keepAlive, final long timestamp) { + System.err.println("****** DISCOVERING: meta "+currentMeta+", changed["+changedType+", enabled "+changedEnabled+", keepAlive "+keepAlive+"] on "+adapter); System.err.println("Status Adapter:"); System.err.println(adapter.toString()); } diff --git a/examples/java/ScannerTinyB02.java b/examples/java/ScannerTinyB02.java index 815d5876..d8409397 100644 --- a/examples/java/ScannerTinyB02.java +++ b/examples/java/ScannerTinyB02.java @@ -42,6 +42,7 @@ import org.tinyb.BluetoothUtils; import org.tinyb.EIRDataTypeSet; import org.tinyb.GATTCharacteristicListener; import org.tinyb.HCIStatusCode; +import org.tinyb.ScanType; /** * Test and debugging application for certain situation. @@ -147,8 +148,8 @@ public class ScannerTinyB02 { } @Override - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { - System.err.println("****** DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter); + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final boolean keepAlive, final long timestamp) { + System.err.println("****** DISCOVERING: meta "+currentMeta+", changed["+changedType+", enabled "+changedEnabled+", keepAlive "+keepAlive+"] on "+adapter); System.err.println("Status Adapter:"); System.err.println(adapter.toString()); } diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java index 3e3e3973..402175bd 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import org.tinyb.AdapterSettings; import org.tinyb.BluetoothAdapter; @@ -48,6 +49,7 @@ import org.tinyb.BluetoothType; import org.tinyb.EIRDataTypeSet; import org.tinyb.HCIStatusCode; import org.tinyb.HCIWhitelistConnectType; +import org.tinyb.ScanType; import org.tinyb.AdapterStatusListener; import org.tinyb.TransportType; @@ -74,7 +76,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private final AtomicBoolean isPairable = new AtomicBoolean(false); // AdapterSettings private BluetoothNotification<Boolean> userPoweredNotificationCB = null; - private final AtomicBoolean isDiscovering = new AtomicBoolean(false); // AdapterStatusListener and powerdOff + private final AtomicReference<ScanType> currentMetaScanType = new AtomicReference<ScanType>(ScanType.NONE); // AdapterStatusListener and powerdOff private BluetoothNotification<Boolean> userDiscoveringNotificationCB = null; private final List<BluetoothDevice> discoveredDevices = new ArrayList<BluetoothDevice>(); @@ -122,7 +124,7 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter private final void poweredOff() { isPowered.set(false); - isDiscovering.set(false); + currentMetaScanType.set(ScanType.NONE); } @Override @@ -251,26 +253,31 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter } @Override - public boolean getDiscovering() { - return isDiscovering.get(); + public final ScanType getCurrentScanType() { + return currentMetaScanType.get(); } @Override - public void enableDiscoveringNotifications(final BluetoothNotification<Boolean> callback) { + public final boolean getDiscovering() { + return ScanType.NONE != currentMetaScanType.get(); + } + + @Override + public final void enableDiscoveringNotifications(final BluetoothNotification<Boolean> callback) { synchronized(userCallbackLock) { userDiscoveringNotificationCB = callback; } } @Override - public void disableDiscoveringNotifications() { + public final void disableDiscoveringNotifications() { synchronized(userCallbackLock) { userDiscoveringNotificationCB = null; } } @Override - public boolean getPairable() { return isPairable.get(); } + public final boolean getPairable() { return isPairable.get(); } @Override public void enablePairableNotifications(final BluetoothNotification<Boolean> callback) { @@ -480,18 +487,18 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter } } @Override - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final boolean keepAlive, final long timestamp) { if( DEBUG ) { - System.err.println("Adapter.StatusListener.DISCOVERING: enabled "+enabled+", keepAlive "+keepAlive+" on "+adapter); + System.err.println("Adapter.StatusListener.DISCOVERING: meta "+currentMeta+", changed["+changedType+", enabled "+changedEnabled+", keepAlive "+keepAlive+"] on "+adapter); } - if( !enabled && keepAlive ) { - // Don't update isDiscovering:=false and don't notify user IF keepAlive! - return; - } - if( isDiscovering.compareAndSet(!enabled, enabled) ) { + // meta ignores changes on temp disabled discovery + final boolean has_le_changed = currentMetaScanType.get().hasScanType(ScanType.LE) != currentMeta.hasScanType(ScanType.LE); + currentMetaScanType.set(currentMeta); + + if( changedType.hasScanType(ScanType.LE) && has_le_changed ) { synchronized(userCallbackLock) { if( null != userDiscoveringNotificationCB ) { - userDiscoveringNotificationCB.run(enabled); + userDiscoveringNotificationCB.run(changedEnabled); } } } diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index 05bc0aa7..3eed0121 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -41,10 +41,12 @@ static const std::string _eirDataTypeSetClassName("org/tinyb/EIRDataTypeSet"); static const std::string _eirDataTypeSetClazzCtorArgs("(I)V"); static const std::string _hciStatusCodeClassName("org/tinyb/HCIStatusCode"); static const std::string _hciStatusCodeClazzGetArgs("(B)Lorg/tinyb/HCIStatusCode;"); +static const std::string _scanTypeClassName("org/tinyb/ScanType"); +static const std::string _scanTypeClazzGetArgs("(B)Lorg/tinyb/ScanType;"); static const std::string _deviceClazzCtorArgs("(JLdirect_bt/tinyb/DBTAdapter;Ljava/lang/String;IILjava/lang/String;J)V"); static const std::string _adapterSettingsChangedMethodArgs("(Lorg/tinyb/BluetoothAdapter;Lorg/tinyb/AdapterSettings;Lorg/tinyb/AdapterSettings;Lorg/tinyb/AdapterSettings;J)V"); -static const std::string _discoveringChangedMethodArgs("(Lorg/tinyb/BluetoothAdapter;ZZJ)V"); +static const std::string _discoveringChangedMethodArgs("(Lorg/tinyb/BluetoothAdapter;Lorg/tinyb/ScanType;Lorg/tinyb/ScanType;ZZJ)V"); static const std::string _deviceFoundMethodArgs("(Lorg/tinyb/BluetoothDevice;J)V"); static const std::string _deviceUpdatedMethodArgs("(Lorg/tinyb/BluetoothDevice;Lorg/tinyb/EIRDataTypeSet;J)V"); static const std::string _deviceConnectedMethodArgs("(Lorg/tinyb/BluetoothDevice;SJ)V"); @@ -61,7 +63,8 @@ class JNIAdapterStatusListener : public AdapterStatusListener { public void adapterSettingsChanged(final BluetoothAdapter adapter, final AdapterSettings oldmask, final AdapterSettings newmask, final AdapterSettings changedmask, final long timestamp) { } - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { } + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, + final boolean keepAlive, final long timestamp) { } public void deviceFound(final BluetoothDevice device, final long timestamp) { } public void deviceUpdated(final BluetoothDevice device, final EIRDataTypeSet updateMask, final long timestamp) { } public void deviceConnected(final BluetoothDevice device, final short handle, final long timestamp) { } @@ -77,8 +80,10 @@ class JNIAdapterStatusListener : public AdapterStatusListener { jmethodID adapterSettingsClazzCtor; JNIGlobalRef eirDataTypeSetClazzRef; jmethodID eirDataTypeSetClazzCtor; - JNIGlobalRef hciErrorCodeClazzRef; - jmethodID hciErrorCodeClazzGet; + JNIGlobalRef hciStatusCodeClazzRef; + jmethodID hciStatusCodeClazzGet; + JNIGlobalRef scanTypeClazzRef; + jmethodID scanTypeClazzGet; JNIGlobalRef deviceClazzRef; jmethodID deviceClazzCtor; jfieldID deviceClazzTSLastDiscoveryField; @@ -143,20 +148,36 @@ class JNIAdapterStatusListener : public AdapterStatusListener { throw jau::InternalError("EIRDataType ctor not found: "+_eirDataTypeSetClassName+".<init>"+_eirDataTypeSetClazzCtorArgs, E_FILE_LINE); } - // hciErrorCodeClazzRef, hciErrorCodeClazzGet + // hciStatusCodeClazzRef, hciStatusCodeClazzGet { jclass hciErrorCodeClazz = jau::search_class(env, _hciStatusCodeClassName.c_str()); jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == hciErrorCodeClazz ) { throw jau::InternalError("DBTDevice::java_class not found: "+_hciStatusCodeClassName, E_FILE_LINE); } - hciErrorCodeClazzRef = JNIGlobalRef(hciErrorCodeClazz); + hciStatusCodeClazzRef = JNIGlobalRef(hciErrorCodeClazz); env->DeleteLocalRef(hciErrorCodeClazz); } - hciErrorCodeClazzGet = jau::search_method(env, hciErrorCodeClazzRef.getClass(), "get", _hciStatusCodeClazzGetArgs.c_str(), true); + hciStatusCodeClazzGet = jau::search_method(env, hciStatusCodeClazzRef.getClass(), "get", _hciStatusCodeClazzGetArgs.c_str(), true); jau::java_exception_check_and_throw(env, E_FILE_LINE); - if( nullptr == hciErrorCodeClazzGet ) { - throw jau::InternalError("EIRDataType ctor not found: "+_hciStatusCodeClassName+".get"+_hciStatusCodeClazzGetArgs, E_FILE_LINE); + if( nullptr == hciStatusCodeClazzGet ) { + throw jau::InternalError("Static method not found: "+_hciStatusCodeClassName+".get"+_hciStatusCodeClazzGetArgs, E_FILE_LINE); + } + + // scanTypeClazzRef, scanTypeClazzGet + { + jclass scanTypeClazz = jau::search_class(env, _scanTypeClassName.c_str()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); + if( nullptr == scanTypeClazz ) { + throw jau::InternalError("DBTDevice::java_class not found: "+_scanTypeClassName, E_FILE_LINE); + } + scanTypeClazzRef = JNIGlobalRef(scanTypeClazz); + env->DeleteLocalRef(scanTypeClazz); + } + scanTypeClazzGet = jau::search_method(env, scanTypeClazzRef.getClass(), "get", _scanTypeClazzGetArgs.c_str(), true); + jau::java_exception_check_and_throw(env, E_FILE_LINE); + if( nullptr == scanTypeClazzGet ) { + throw jau::InternalError("Static method not found: "+_scanTypeClassName+".get"+_scanTypeClazzGetArgs, E_FILE_LINE); } // deviceClazzRef, deviceClazzCtor @@ -253,11 +274,20 @@ class JNIAdapterStatusListener : public AdapterStatusListener { env->DeleteLocalRef(adapterSettingChanged); } - void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + void discoveringChanged(DBTAdapter &a, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp) override { JNIEnv *env = *jni_env; (void)a; + + jobject jcurrentMeta = env->CallStaticObjectMethod(scanTypeClazzRef.getClass(), scanTypeClazzGet, (jbyte)number(currentMeta)); + jau::java_exception_check_and_throw(env, E_FILE_LINE); + JNIGlobalRef::check(jcurrentMeta, E_FILE_LINE); + + jobject jchangedType = env->CallStaticObjectMethod(scanTypeClazzRef.getClass(), scanTypeClazzGet, (jbyte)number(changedType)); + jau::java_exception_check_and_throw(env, E_FILE_LINE); + JNIGlobalRef::check(jchangedType, E_FILE_LINE); + env->CallVoidMethod(listenerObjRef.getObject(), mDiscoveringChanged, jau::JavaGlobalObj::GetObject(adapterObjRef), - (jboolean)enabled, (jboolean)keepAlive, (jlong)timestamp); + jcurrentMeta, jchangedType, (jboolean)changedEnabled, (jboolean)keepAlive, (jlong)timestamp); jau::java_exception_check_and_throw(env, E_FILE_LINE); } @@ -355,7 +385,7 @@ class JNIAdapterStatusListener : public AdapterStatusListener { env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp); jau::java_exception_check_and_throw(env, E_FILE_LINE); - jobject hciErrorCode = env->CallStaticObjectMethod(hciErrorCodeClazzRef.getClass(), hciErrorCodeClazzGet, (jbyte)static_cast<uint8_t>(reason)); + jobject hciErrorCode = env->CallStaticObjectMethod(hciStatusCodeClazzRef.getClass(), hciStatusCodeClazzGet, (jbyte)static_cast<uint8_t>(reason)); jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(hciErrorCode, E_FILE_LINE); diff --git a/java/org/tinyb/AdapterStatusListener.java b/java/org/tinyb/AdapterStatusListener.java index f04f5c16..78c307a6 100644 --- a/java/org/tinyb/AdapterStatusListener.java +++ b/java/org/tinyb/AdapterStatusListener.java @@ -72,11 +72,13 @@ public abstract class AdapterStatusListener { /** * {@link BluetoothAdapter}'s discovery state has changed, i.e. enabled or disabled. * @param adapter the adapter which discovering state has changed. - * @param enabled the new discovery state - * @param keepAlive if {@code true}, the discovery will be re-enabled if disabled by the underlying Bluetooth implementation. + * @param currentMeta the current meta {@link ScanType} + * @param changedType denotes the changed {@link ScanType} + * @param changedEnabled denotes whether the changed {@link ScanType} has been enabled or disabled + * @param keepAlive if {@code true}, the denoted changed {@link ScanType} will be re-enabled if disabled by the underlying Bluetooth implementation. * @param timestamp the time in monotonic milliseconds when this event occurred. See {@link BluetoothUtils#getCurrentMilliseconds()}. */ - public void discoveringChanged(final BluetoothAdapter adapter, final boolean enabled, final boolean keepAlive, final long timestamp) { } + public void discoveringChanged(final BluetoothAdapter adapter, final ScanType currentMeta, final ScanType changedType, final boolean changedEnabled, final boolean keepAlive, final long timestamp) { } /** * A {@link BluetoothDevice} has been newly discovered. diff --git a/java/org/tinyb/BluetoothAdapter.java b/java/org/tinyb/BluetoothAdapter.java index 8e4b5b06..95f3e77c 100644 --- a/java/org/tinyb/BluetoothAdapter.java +++ b/java/org/tinyb/BluetoothAdapter.java @@ -380,12 +380,29 @@ public interface BluetoothAdapter extends BluetoothObject public boolean setPairableTimeout(long value); /** + * Returns the current meta discovering {@link ScanType}. + * It can be modified through {@link #startDiscovery(boolean)} and {@link #stopDiscovery()}. + * <p> + * Note that if {@link #startDiscovery(boolean)} has been issued with keepAlive==true, + * the meta {@link ScanType} will still keep the desired {@link ScanType} enabled + * even if it has been temporarily disabled. + * </p> + * @see #startDiscovery(boolean) + * @see #stopDiscovery() + * @since 2.0.0 + */ + public ScanType getCurrentScanType(); + + /** * Returns the meta discovering state (of the adapter). * It can be modified through * start_discovery/stop_discovery functions. * @return The discovering state of the adapter. + * @deprecated since 2.0.0, use {@link #getCurrentScanType()}. + * @see #getCurrentScanType() * @see #startDiscovery(boolean) */ + @Deprecated public boolean getDiscovering(); /** diff --git a/java/org/tinyb/ScanType.java b/java/org/tinyb/ScanType.java new file mode 100644 index 00000000..c240dfad --- /dev/null +++ b/java/org/tinyb/ScanType.java @@ -0,0 +1,72 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.tinyb; + +/** + * Meta ScanType as derived from {@link BTMode} + * with defined value mask consisting of {@link BluetoothAddressType} bits. + * <p> + * This ScanType is natively compatible with DBTManager's implementation + * for start and stop discovery. + * </p> + * @since 2.0.0 + */ +public enum ScanType { + NONE ((byte) 0), + /** BREDR scanning only, 1 << BluetoothAddressType.BDADDR_BREDR */ + BREDR ((byte) 1 /* 1 << BluetoothAddressType.BDADDR_BREDR.value */ ), + /** LE scanning only, ( 1 << BluetoothAddressType.BDADDR_LE_PUBLIC ) | ( 1 << BluetoothAddressType.BDADDR_LE_RANDOM ) */ + LE ((byte) 6 /* ( 1 << BluetoothAddressType.BDADDR_LE_PUBLIC.value ) | ( 1 << BluetoothAddressType.BDADDR_LE_RANDOM.value ) */ ), + /** Dual scanning, BREDR | LE */ + DUAL ((byte) 7 /* BREDR.value | LE.value */ ); + + public final byte value; + + public boolean hasScanType(final ScanType testType) { + return testType.value == ( value & testType.value ); + } + + public static final ScanType changeScanType(final ScanType current, final ScanType changeType, final boolean changeEnable) { + return changeEnable ? ScanType.get( (byte)( current.value | changeType.value ) ) : ScanType.get( (byte)( current.value & ~changeType.value ) ); + } + + /** + * Maps the specified integer value to a constant of {@link ScanType}. + * @param value the integer value to be mapped to a constant of this enum type. + * @return the corresponding constant of this enum type, using {@link #NONE} if not supported. + */ + public static ScanType get(final byte value) { + switch(value) { + case (byte) 1: return BREDR; + case (byte) 6: return LE; + case (byte) 7: return DUAL; + default: return NONE; + } + } + + ScanType(final byte v) { + value = v; + } +} diff --git a/java/tinyb/dbus/DBusAdapter.java b/java/tinyb/dbus/DBusAdapter.java index a92beafa..3d8a1d89 100644 --- a/java/tinyb/dbus/DBusAdapter.java +++ b/java/tinyb/dbus/DBusAdapter.java @@ -43,6 +43,7 @@ import org.tinyb.BluetoothNotification; import org.tinyb.BluetoothType; import org.tinyb.HCIStatusCode; import org.tinyb.HCIWhitelistConnectType; +import org.tinyb.ScanType; import org.tinyb.TransportType; public class DBusAdapter extends DBusObject implements BluetoothAdapter @@ -193,6 +194,10 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter public native boolean setPairableTimeout(long value); @Override + public final ScanType getCurrentScanType() { + return getDiscovering() ? ScanType.LE : ScanType.NONE; + } + @Override public native boolean getDiscovering(); @Override diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 7b5d5dc6..0a20736f 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -740,22 +740,17 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringAny(std::shared_ptr<MgmtEvent> e, const checkDiscoveryState(); - // FIXME: AdapterStatusListener::discoveringChanged(..) - // Method shall include the enabled/disabled ScanType plus the current overall ScanType state. - // For now, we only report LE discovery state. - if( hasScanType(eventScanType, ScanType::LE) ) { // only report LE-scan changes for now - int i=0; - jau::for_each_cow(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { - try { - l->discoveringChanged(*this, eventEnabled, keep_le_scan_alive, event.getTimestamp()); - } catch (std::exception &except) { - ERR_PRINT("DBTAdapter::EventCB:DeviceDiscovering-CBs %d/%zd: %s of %s: Caught exception %s", - i+1, statusListenerList.size(), - l->toString().c_str(), toString().c_str(), except.what()); - } - i++; - }); - } + int i=0; + jau::for_each_cow(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { + try { + l->discoveringChanged(*this, currentMetaScanType, eventScanType, eventEnabled, keep_le_scan_alive, event.getTimestamp()); + } catch (std::exception &except) { + ERR_PRINT("DBTAdapter::EventCB:DeviceDiscovering-CBs %d/%zd: %s of %s: Caught exception %s", + i+1, statusListenerList.size(), + l->toString().c_str(), toString().c_str(), except.what()); + } + i++; + }); if( !hasScanType(currentNativeScanType, ScanType::LE) && keep_le_scan_alive ) { std::thread bg(&DBTAdapter::startDiscoveryBackground, this); // @suppress("Invalid arguments") |