diff options
author | Sven Gothel <[email protected]> | 2020-10-13 02:19:02 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-13 02:19:02 +0200 |
commit | f3acf0e335bf65e67aa5b4d292bb5446ffbde271 (patch) | |
tree | a206c9b4aa0d550fe0d73f9d7e7d24e5680ad6d0 /examples | |
parent | d5adce54eedbbe17f47db839f47280ba8fcf1f49 (diff) |
AdapterStatusListener[1] and DBTAdapter[2] API Refinement: [1] non const DBTAdapter, [2] use HCIStatusCode for start/stopDiscovery
and well as [2] return boolean for set[Discoverable|Bondable|Powered](..).
AdapterStatusListener needs to pass non-const DBTAdapter to allow actionable modification of adapter state,
e.g. start/stopDiscovery.
HCIStatusCode is desired for start/stopDiscovery, allowing user to read actual HCI status code on failure.
boolean return value for set[Discoverable|Bondable|Powered](..) reflecting general success or failure
on these basic commands.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner00/dbt_scanner00.cpp | 6 | ||||
-rw-r--r-- | examples/direct_bt_scanner01/dbt_scanner01.cpp | 6 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 29 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 43 | ||||
-rw-r--r-- | examples/java/ScannerTinyB00.java | 4 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 6 |
6 files changed, 53 insertions, 41 deletions
diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index df37a103..0940de7e 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -51,7 +51,7 @@ std::mutex mtxDeviceFound; std::condition_variable cvDeviceFound; class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), @@ -62,7 +62,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + 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)timestamp; } @@ -204,7 +204,7 @@ int main(int argc, char *argv[]) const int64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { - ok = adapter.startDiscovery(true /* keepAlive */); + ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); if( !ok) { perror("Adapter start discovery failed"); goto out; diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index db7fd78c..a63deb25 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -48,7 +48,7 @@ std::mutex mtxDeviceFound; std::condition_variable cvDeviceFound; class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n", getAdapterSettingsString(oldmask).c_str(), @@ -59,7 +59,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + 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)timestamp; } @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) const int64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { - ok = adapter.startDiscovery(true /* keepAlive */); + ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); if( !ok) { perror("Adapter start discovery failed"); goto out; diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index f8bbbc51..a7e0b505 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -77,7 +77,7 @@ static void connectDiscoveredDevice(std::shared_ptr<DBTDevice> device); static void processConnectedDevice(std::shared_ptr<DBTDevice> device); static void removeDevice(std::shared_ptr<DBTDevice> device); -static void resetAdapter(DBTAdapter *a); +static void resetAdapter(DBTAdapter *a, int mode); static std::vector<EUI48> devicesInProcessing; static std::recursive_mutex mtx_devicesProcessing; @@ -147,7 +147,7 @@ static size_t getDeviceProcessingCount() { class MyAdapterStatusListener : public AdapterStatusListener { - void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask, + void adapterSettingsChanged(DBTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp) override { const bool initialSetting = AdapterSetting::NONE == oldmask; if( initialSetting ) { @@ -162,7 +162,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { (void)timestamp; } - void discoveringChanged(DBTAdapter const &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { + 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)timestamp; } @@ -303,7 +303,8 @@ 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 ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Connecting Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); } } @@ -429,7 +430,8 @@ static void processConnectedDevice(std::shared_ptr<DBTDevice> device) { exit: if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Processing Device: startDiscovery.0 result %s\n", getHCIStatusCodeString(r).c_str()); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -457,7 +459,8 @@ exit: device->remove(); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Processing Device: startDiscovery.1 result %s\n", getHCIStatusCodeString(r).c_str()); } } @@ -476,14 +479,15 @@ static void removeDevice(std::shared_ptr<DBTDevice> device) { device->remove(); if( !USE_WHITELIST && 0 == getDeviceProcessingCount() ) { - device->getAdapter().startDiscovery( true ); + const HCIStatusCode r = device->getAdapter().startDiscovery( true ); + fprintf(stderr, "****** Remove Device: startDiscovery result %s\n", getHCIStatusCodeString(r).c_str()); } } -static void resetAdapter(DBTAdapter *a) { - fprintf(stderr, "****** Reset Adapter: reset start: %s\n", a->toString().c_str()); +static void resetAdapter(DBTAdapter *a, int mode) { + fprintf(stderr, "****** Reset Adapter: reset[%d] start: %s\n", mode, a->toString().c_str()); HCIStatusCode res = a->reset(); - fprintf(stderr, "****** Reset Adapter: reset end: %s, %s\n", getHCIStatusCodeString(res).c_str(), a->toString().c_str()); + fprintf(stderr, "****** Reset Adapter: reset[%d] end: %s, %s\n", mode, getHCIStatusCodeString(res).c_str(), a->toString().c_str()); } void test(int dev_id) { @@ -516,8 +520,9 @@ void test(int dev_id) { fprintf(stderr, "Added to WHITELIST: res %d, address %s\n", res, it->toString().c_str()); } } else { - if( !adapter.startDiscovery( true ) ) { - perror("Adapter start discovery failed"); + HCIStatusCode status = adapter.startDiscovery( true ); + if( HCIStatusCode::SUCCESS != status ) { + fprintf(stderr, "Adapter: Start discovery failed: %s", getHCIStatusCodeString(status).c_str()); done = true; } } diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index a7027008..75a35f75 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -122,8 +122,9 @@ public class DBTScanner10 { newmask.isSet(AdapterSettings.SettingType.POWERED) ) { // powered on adapter .... - if( !adapter.startDiscovery( true ) ) { - println("Adapter (powered-on): Start discovery failed"); + final HCIStatusCode res = adapter.startDiscovery( true ); + if( res != HCIStatusCode.SUCCESS ) { + println("Adapter (powered-on): Start discovery failed: "+res); } } } @@ -186,7 +187,8 @@ public class DBTScanner10 { ) ) { - println("****** CONNECTED-0: Processing "+device.toString()); + connectionCount.incrementAndGet(); + println("****** CONNECTED-0: Processing["+connectionCount.get()+"] "+device.toString()); { final long td = BluetoothUtils.getCurrentMilliseconds() - timestamp_t0; // adapter-init -> now println("PERF: adapter-init -> CONNECTED-0 " + td + " ms"); @@ -221,11 +223,11 @@ public class DBTScanner10 { } else { devicesInProcessing.remove(device.getAddress()); } - if( 0 == connectionCount.addAndGet(1) % RESET_ADAPTER_EACH_CONN ) { + if( 0 < RESET_ADAPTER_EACH_CONN && 0 == connectionCount.get() % RESET_ADAPTER_EACH_CONN ) { final Thread adapterResetTask = new Thread( new Runnable() { @Override public void run() { - resetAdapter(device.getAdapter()); + resetAdapter(device.getAdapter(), 1); } }, "DBT-Reset-"+device.getAdapter().getAddress()); adapterResetTask.setDaemon(true); // detach thread @@ -237,7 +239,7 @@ public class DBTScanner10 { private void connectDiscoveredDevice(final BluetoothDevice device) { println("****** Connecting Device: Start " + device.toString()); { - final boolean r = device.getAdapter().stopDiscovery(); + final HCIStatusCode r = device.getAdapter().stopDiscovery(); println("****** Connecting Device: stopDiscovery result "+r); } HCIStatusCode res; @@ -248,7 +250,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 boolean r = device.getAdapter().startDiscovery( true ); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); println("****** Connecting Device: startDiscovery result "+r); } } @@ -290,7 +292,7 @@ public class DBTScanner10 { println("****** Processing Device: Start " + device.toString()); { // make sure for pending connections on failed connect*(..) command - final boolean r = device.getAdapter().stopDiscovery(); + final HCIStatusCode r = device.getAdapter().stopDiscovery(); println("****** Processing Device: stopDiscovery result "+r); } @@ -454,8 +456,8 @@ public class DBTScanner10 { } if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery result "+r); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); + println("****** Processing Device: startDiscovery.0 result "+r); } if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { @@ -483,11 +485,15 @@ public class DBTScanner10 { device.remove(); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); - println("****** Processing Device: startDiscovery result "+r); + 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); + } + if( 0 < MULTI_MEASUREMENTS.get() ) { MULTI_MEASUREMENTS.decrementAndGet(); println("****** Processing Device: MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS.get()+": "+device.getAddress()); @@ -503,15 +509,15 @@ public class DBTScanner10 { device.remove(); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { - final boolean r = device.getAdapter().startDiscovery( true ); + final HCIStatusCode r = device.getAdapter().startDiscovery( true ); println("****** Remove Device: startDiscovery result "+r); } } - private void resetAdapter(final BluetoothAdapter adapter) { - println("****** Reset Adapter: reset start: "+adapter.toString()); + private void resetAdapter(final BluetoothAdapter adapter, final int mode) { + println("****** Reset Adapter: reset["+mode+"] start: "+adapter.toString()); final HCIStatusCode res = adapter.reset(); - println("****** Reset Adapter: reset end: "+res+", "+adapter.toString()); + println("****** Reset Adapter: reset["+mode+"] end: "+res+", "+adapter.toString()); } public void runTest(final BluetoothManager manager) { @@ -556,8 +562,9 @@ public class DBTScanner10 { println("Added to whitelist: res "+res+", address "+addr); } } else { - if( !adapter.startDiscovery( true ) ) { - println("Adapter start discovery failed"); + final HCIStatusCode status = adapter.startDiscovery( true ); + if( HCIStatusCode.SUCCESS != status ) { + println("Adapter start discovery failed: "+status); done = true; } } diff --git a/examples/java/ScannerTinyB00.java b/examples/java/ScannerTinyB00.java index 9b49fda5..fe7f2475 100644 --- a/examples/java/ScannerTinyB00.java +++ b/examples/java/ScannerTinyB00.java @@ -137,9 +137,9 @@ public class ScannerTinyB00 { if( useAdapter ) { adapter.removeDevices(); } - final boolean discoveryStarted = useAdapter ? adapter.startDiscovery(true) : manager.startDiscovery(); + final HCIStatusCode discoveryStatus = useAdapter ? adapter.startDiscovery(true) : manager.startDiscovery(true); - System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode+", useAdapter "+useAdapter); + System.err.println("The discovery started: " + discoveryStatus + " for mac "+mac+", mode "+mode+", useAdapter "+useAdapter); BluetoothDevice sensor = null; if( 0 == mode ) { diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index d62aee88..d2a03ffb 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -225,10 +225,10 @@ public class ScannerTinyB01 { final long t0 = BluetoothUtils.getCurrentMilliseconds(); - final boolean discoveryStarted = adapter.startDiscovery(true); + final HCIStatusCode discoveryStatus = adapter.startDiscovery(true); - System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+waitForDevice+", mode "+mode); - if( !discoveryStarted ) { + System.err.println("The discovery started: " + discoveryStatus + " for mac "+waitForDevice+", mode "+mode); + if( HCIStatusCode.SUCCESS != discoveryStatus ) { break; } BluetoothDevice sensor = null; |