diff options
author | Sven Gothel <[email protected]> | 2020-10-14 19:18:49 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-14 19:18:49 +0200 |
commit | 148b9b3b4f04b5e76da0400dafdbcada06128c7d (patch) | |
tree | 491183bf0cb413a6babfcafb5f705387141e8f3a /examples | |
parent | e524b52cbd30733ae798aa7a764141091c71a909 (diff) |
dbt_scanner10/DBTScanner10: Issue 'startDiscovery(..)' off-thread @ POWERED in AdapterStatusListener::adapterSettingsChanged(..)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 33 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 43 |
2 files changed, 38 insertions, 38 deletions
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index f68b2e58..a30096bc 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -78,6 +78,7 @@ static void processConnectedDevice(std::shared_ptr<DBTDevice> device); static void removeDevice(std::shared_ptr<DBTDevice> device); static void resetAdapter(DBTAdapter *a, int mode); +static bool startDiscovery(DBTAdapter *a, std::string msg); static std::vector<EUI48> devicesInProcessing; static std::recursive_mutex mtx_devicesProcessing; @@ -165,10 +166,8 @@ class MyAdapterStatusListener : public AdapterStatusListener { isAdapterSettingBitSet(changedmask, AdapterSetting::POWERED) && isAdapterSettingBitSet(newmask, AdapterSetting::POWERED) ) { - HCIStatusCode status = a.startDiscovery( true ); - if( HCIStatusCode::SUCCESS != status ) { - fprintf(stderr, "Adapter (powered-on): Start discovery failed: %s", getHCIStatusCodeString(status).c_str()); - } + std::thread sd(::startDiscovery, &a, "powered-on"); // @suppress("Invalid arguments") + sd.detach(); } } @@ -313,8 +312,7 @@ static void connectDiscoveredDevice(std::shared_ptr<DBTDevice> device) { } fprintf(stderr, "****** Connecting Device: End result %s of %s\n", getHCIStatusCodeString(res).c_str(), device->toString().c_str()); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() && HCIStatusCode::SUCCESS != res ) { - const HCIStatusCode r = device->getAdapter().startDiscovery( true ); - fprintf(stderr, "****** Connecting Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); + startDiscovery(&device->getAdapter(), "post-connect"); } } @@ -440,8 +438,7 @@ static void processConnectedDevice(std::shared_ptr<DBTDevice> device) { exit: if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - const HCIStatusCode r = device->getAdapter().startDiscovery( true ); - fprintf(stderr, "****** Processing Device: startDiscovery.0 result %s\n", getHCIStatusCodeString(r).c_str()); + startDiscovery(&device->getAdapter(), "post-processing-1"); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -468,12 +465,11 @@ exit: removeFromDevicesProcessing(device->getAddress()); device->remove(); + if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount % RESET_ADAPTER_EACH_CONN ) { resetAdapter(&device->getAdapter(), 2); - } - if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - const HCIStatusCode r = device->getAdapter().startDiscovery( true ); - fprintf(stderr, "****** Processing Device: startDiscovery.1 result %s\n", getHCIStatusCodeString(r).c_str()); + } else if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { + startDiscovery(&device->getAdapter(), "post-processing-2"); } } @@ -492,8 +488,7 @@ static void removeDevice(std::shared_ptr<DBTDevice> device) { device->remove(); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - const HCIStatusCode r = device->getAdapter().startDiscovery( true ); - fprintf(stderr, "****** Remove Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); + startDiscovery(&device->getAdapter(), "post-remove-device"); } } @@ -503,6 +498,12 @@ static void resetAdapter(DBTAdapter *a, int mode) { fprintf(stderr, "****** Reset Adapter: reset[%d] end: %s, %s\n", mode, getHCIStatusCodeString(res).c_str(), a->toString().c_str()); } +static bool startDiscovery(DBTAdapter *a, std::string msg) { + HCIStatusCode status = a->startDiscovery( true ); + fprintf(stderr, "****** Start discovery (%s) result: %s\n", msg.c_str(), getHCIStatusCodeString(status).c_str()); + return HCIStatusCode::SUCCESS == status; +} + void test(int dev_id) { bool done = false; @@ -533,9 +534,7 @@ void test(int dev_id) { fprintf(stderr, "Added to WHITELIST: res %d, address %s\n", res, it->toString().c_str()); } } else { - HCIStatusCode status = adapter.startDiscovery( true ); - if( HCIStatusCode::SUCCESS != status ) { - fprintf(stderr, "Adapter: Start discovery failed: %s", getHCIStatusCodeString(status).c_str()); + if( !startDiscovery(&adapter, "kick-off") ) { done = true; } } diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index 0a0a1f05..478d7c48 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -122,11 +122,14 @@ public class DBTScanner10 { changedmask.isSet(AdapterSettings.SettingType.POWERED) && newmask.isSet(AdapterSettings.SettingType.POWERED) ) { - // powered on adapter .... - final HCIStatusCode res = adapter.startDiscovery( true ); - if( res != HCIStatusCode.SUCCESS ) { - println("Adapter (powered-on): Start discovery failed: "+res); - } + final Thread startDisoveryTask = new Thread( new Runnable() { + @Override + public void run() { + startDiscovery(adapter, "powered-on"); + } + }, "DBT-StartDiscovery-"+adapter.getAddress()); + startDisoveryTask.setDaemon(true); // detach thread + startDisoveryTask.start(); } } @@ -251,8 +254,7 @@ public class DBTScanner10 { } println("****** Connecting Device Command, res "+res+": End result "+res+" of " + device.toString()); if( !USE_WHITELIST && 0 == devicesInProcessing.size() && HCIStatusCode.SUCCESS != res ) { - final HCIStatusCode r = device.getAdapter().startDiscovery( true ); - println("****** Connecting Device: startDiscovery result "+r); + startDiscovery(device.getAdapter(), "post-connect"); } } @@ -457,8 +459,7 @@ public class DBTScanner10 { } if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final HCIStatusCode r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery.0 result "+r); + startDiscovery(device.getAdapter(), "post-processing-1"); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -485,16 +486,13 @@ public class DBTScanner10 { device.remove(); - if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final HCIStatusCode r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery.1 result "+r); + if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount.get() % RESET_ADAPTER_EACH_CONN ) { + resetAdapter(device.getAdapter(), 2); + } else if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { + startDiscovery(device.getAdapter(), "post-processing-2"); } } - if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount.get() % RESET_ADAPTER_EACH_CONN ) { - resetAdapter(device.getAdapter(), 2); - } - if( 0 < MULTI_MEASUREMENTS.get() ) { MULTI_MEASUREMENTS.decrementAndGet(); println("****** Processing Device: MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS.get()+": "+device.getAddress()); @@ -510,8 +508,7 @@ public class DBTScanner10 { device.remove(); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final HCIStatusCode r = device.getAdapter().startDiscovery( true ); - println("****** Remove Device: startDiscovery result "+r); + startDiscovery(device.getAdapter(), "post-remove-device"); } } @@ -521,6 +518,12 @@ public class DBTScanner10 { println("****** Reset Adapter: reset["+mode+"] end: "+res+", "+adapter.toString()); } + private boolean startDiscovery(final BluetoothAdapter adapter, final String msg) { + final HCIStatusCode status = adapter.startDiscovery( true ); + println("****** Start discovery ("+msg+") result: "+status); + return HCIStatusCode.SUCCESS == status; + } + public void runTest(final BluetoothManager manager) { final BluetoothAdapter adapter; { @@ -563,9 +566,7 @@ public class DBTScanner10 { println("Added to whitelist: res "+res+", address "+addr); } } else { - final HCIStatusCode status = adapter.startDiscovery( true ); - if( HCIStatusCode.SUCCESS != status ) { - println("Adapter start discovery failed: "+status); + if( !startDiscovery(adapter, "kick-off") ) { done = true; } } |