diff options
author | Sven Gothel <[email protected]> | 2020-04-21 13:05:53 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-04-21 13:05:53 +0200 |
commit | 2bacdad45d6c7a315937c4c3723fba54fb83b631 (patch) | |
tree | 912cc4c8440a00aee8ab5cbe8a8c1d5c6e07a2cb /examples | |
parent | d123b2363692d4e95340aa63bf5246cd293b4f30 (diff) |
Refine: DBT API, HCISession/DBTAdapter lifecycle, API doc and C++ and C++/JNI exception handling
- HCISession: Handle multiple connections
- DBTDevice holds le_conn_handle and provides the le_disconnect as well
- HCISession maintains a vector of connected devices
- HCISession/DBTAdapter: Cleanup shutdown and refine lifecycle
- DBTDevice/DBTAdapter: Drop explicit HCISession argument,
simply use the attached HCISession of DBTAdapter.
- Refine API doc in general
+++
- Device Java/JNI: Add a few more methods to test connect/disconnect.
+++
Refine C++ and C++/JNI exception handling:
- Use new java_exception_check_and_throw(..):
Throw C++ exception on pending Java exception, retrieving toString() message.
- Use new java_exception_check(..):
Return immediately from JNI on pending Java exception.
- Replace macro CATCH_EXCEPTION_AND_RAISE_JAVA(..)
with new rethrow_and_raise_java_exception(..), re-trhowing exception
and raising detailed Java exception.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/direct_bt_scanner/dbt_scanner.cpp | 35 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 40 |
2 files changed, 45 insertions, 30 deletions
diff --git a/examples/direct_bt_scanner/dbt_scanner.cpp b/examples/direct_bt_scanner/dbt_scanner.cpp index 81611565..00d53432 100644 --- a/examples/direct_bt_scanner/dbt_scanner.cpp +++ b/examples/direct_bt_scanner/dbt_scanner.cpp @@ -109,17 +109,23 @@ int main(int argc, char *argv[]) */ bool doHCI_LEConnect = true; + bool keepDiscoveryAlive = false; + for(int i=1; i<argc; i++) { if( !strcmp("-wait", argv[i]) ) { waitForEnter = true; } else if( !strcmp("-skipLEConnect", argv[i]) ) { doHCI_LEConnect = false; + } else if( !strcmp("-keepDiscoveryAlive", argv[i]) ) { + keepDiscoveryAlive = true; } else if( !strcmp("-mac", argv[i]) && argc > (i+1) ) { std::string macstr = std::string(argv[++i]); waitForDevice = EUI48(macstr); - fprintf(stderr, "waitForDevice: %s\n", waitForDevice.toString().c_str()); } } + fprintf(stderr, "doHCI_LEConnect %d\n", doHCI_LEConnect); + fprintf(stderr, "keepDiscoveryAlive %d\n", keepDiscoveryAlive); + fprintf(stderr, "waitForDevice: %s\n", waitForDevice.toString().c_str()); if( waitForEnter ) { fprintf(stderr, "Press ENTER to continue\n"); @@ -144,19 +150,30 @@ int main(int argc, char *argv[]) std::shared_ptr<direct_bt::HCISession> session = adapter.open(); - while( ok && !done && nullptr != session ) { - ok = adapter.startDiscovery(*session); + if( keepDiscoveryAlive ) { + ok = adapter.startDiscovery(); if( !ok) { perror("Adapter start discovery failed"); - goto out; } + } - const int deviceCount = adapter.discoverDevices(*session, 1, waitForDevice); + while( ok && !done && nullptr != session ) { + if( !keepDiscoveryAlive ) { + ok = adapter.startDiscovery(); + if( !ok) { + perror("Adapter start discovery failed"); + goto out; + } + } + + const int deviceCount = adapter.discoverDevices(1, waitForDevice); if( 0 > deviceCount ) { perror("Adapter discovery failed"); ok = false; } - adapter.stopDiscovery(*session); + if( !keepDiscoveryAlive ) { + adapter.stopDiscovery(); + } if( ok && 0 < deviceCount ) { const uint64_t t1 = direct_bt::getCurrentMilliseconds(); @@ -179,7 +196,7 @@ int main(int argc, char *argv[]) // uint16_t hciLEConnHandle; if( doHCI_LEConnect ) { - hciLEConnHandle = device->le_connect(*session); + hciLEConnHandle = device->le_connect(); if( 0 == hciLEConnHandle ) { fprintf(stderr, "HCI LE Connection: Failed %s\n", device->toString().c_str()); } else { @@ -288,9 +305,7 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "GATT connect failed: %s\n", gatt.getStateString().c_str()); } - if( 0 != hciLEConnHandle ) { - session->disconnect(0); // FIXME: hci_le_disconnect: Input/output error - } + device->le_disconnect(); // OK if not connected } // if( 2000 > lup ) } // for(auto it = discoveredDevices.begin(); it != discoveredDevices.end(); it++) fprintf(stderr, "Connection: Got %d devices, tried connected to %d with %d succeeded\n", i, j, k); diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index f6cd5a87..b76d0fe9 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -124,13 +124,12 @@ public class ScannerTinyB01 { System.err.println(adapter.toString()); } }; + adapter.removeDevices(); adapter.setDeviceDiscoveryListener(deviceDiscListener); - do { final long t0 = BluetoothUtils.getCurrentMilliseconds(); - adapter.removeDevices(); final boolean discoveryStarted = adapter.startDiscovery(); System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode); @@ -170,45 +169,42 @@ public class ScannerTinyB01 { } System.err.println("Found device in "+(t1-t0)+" ms: "); printDevice(sensor); - System.err.println("ScannerTinyB01 01 stopDiscovery: "+adapter); - adapter.stopDiscovery(); - System.err.println("ScannerTinyB01 02 close: "+adapter); - adapter.close(); - System.err.println("ScannerTinyB01 03 ...: "+adapter); + adapter.stopDiscovery(); // FIXME ???? - if(false) { final BooleanNotification connectedNotification = new BooleanNotification("Connected", t1); final BooleanNotification servicesResolvedNotification = new BooleanNotification("ServicesResolved", t1); sensor.enableConnectedNotifications(connectedNotification); sensor.enableServicesResolvedNotifications(servicesResolvedNotification); - final long t2; + final long t2 = BluetoothUtils.getCurrentMilliseconds(); + final long t3; if ( sensor.connect() ) { - t2 = BluetoothUtils.getCurrentMilliseconds(); - System.err.println("Sensor connected in "+(t2-t1)+" ms"); + t3 = BluetoothUtils.getCurrentMilliseconds(); + System.err.println("Sensor connected: "+(t3-t2)+" ms, total "+(t3-t0)+" ms"); System.err.println("Sensor connectedNotification: "+connectedNotification.getValue()); } else { - t2=0; - System.out.println("Could not connect device."); + t3 = BluetoothUtils.getCurrentMilliseconds(); + System.out.println("Could not connect device: "+(t3-t2)+" ms, total "+(t3-t0)+" ms"); System.exit(-1); } + if( false ) { synchronized( servicesResolvedNotification ) { while( !servicesResolvedNotification.getValue() ) { final long tn = BluetoothUtils.getCurrentMilliseconds(); - if( tn - t2 > 20000 ) { + if( tn - t3 > 20000 ) { break; // 20s TO } servicesResolvedNotification.wait(); } } - final long t3; + final long t4; if ( servicesResolvedNotification.getValue() ) { - t3 = BluetoothUtils.getCurrentMilliseconds(); - System.err.println("Sensor servicesResolved in "+(t3-t2)+" ms, total "+(t3-t1)+" ms"); + t4 = BluetoothUtils.getCurrentMilliseconds(); + System.err.println("Sensor servicesResolved: "+(t4-t3)+" ms, total "+(t4-t0)+" ms"); } else { - t3=0; - System.out.println("Could not connect device."); + t4 = BluetoothUtils.getCurrentMilliseconds(); + System.out.println("Could not connect device: "+(t4-t3)+" ms, total "+(t4-t0)+" ms"); System.exit(-1); } // Will shut down everything .. ?? @@ -220,11 +216,15 @@ public class ScannerTinyB01 { System.exit(1); } printAllServiceInfo(allBluetoothServices); + } sensor.disconnect(); - } System.err.println("ScannerTinyB01 04 ...: "+adapter); } while( forever ); + System.err.println("ScannerTinyB01 01 stopDiscovery: "+adapter); + adapter.stopDiscovery(); + System.err.println("ScannerTinyB01 02 close: "+adapter); + adapter.close(); System.err.println("ScannerTinyB01 05"); manager.shutdown(); System.err.println("ScannerTinyB01 XX"); |