From 53eef8bf45c0500c68af83a5c57f6b348d995c5c Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 24 Sep 2020 11:40:55 +0200 Subject: dbt_scanner10 + ScannerTinyB10: Make gatt-ping optional and always cleanup on disconnect event This is possible due to commit a2096bf3baf8fb5fc8ed37ce4bdb1ec1112b6e8d "LE connection parameters for higher transmission robusteness w/o using a ping-gatt link-loss check" --- examples/java/ScannerTinyB10.java | 48 +++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'examples/java') diff --git a/examples/java/ScannerTinyB10.java b/examples/java/ScannerTinyB10.java index 55e71af3..82d41a89 100644 --- a/examples/java/ScannerTinyB10.java +++ b/examples/java/ScannerTinyB10.java @@ -74,6 +74,7 @@ public class ScannerTinyB10 { int MULTI_MEASUREMENTS = 8; boolean KEEP_CONNECTED = true; + boolean GATT_PING_ENABLED = false; boolean REMOVE_DEVICE = true; boolean USE_WHITELIST = false; final List whitelist = new ArrayList(); @@ -199,6 +200,19 @@ public class ScannerTinyB10 { @Override public void deviceDisconnected(final BluetoothDevice device, final HCIStatusCode reason, final short handle, final long timestamp) { println("****** DISCONNECTED: Reason "+reason+", old handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter()); + + if( REMOVE_DEVICE ) { + final Thread deviceRemoverProcessingTask = new Thread( new Runnable() { + @Override + public void run() { + removeDevice(device); + } + }, "DBT-Remove-"+device.getAddress()); + deviceRemoverProcessingTask.setDaemon(true); // detach thread + deviceRemoverProcessingTask.start(); + } else { + devicesInProcessing.remove(device.getAddress()); + } } }; @@ -430,13 +444,12 @@ public class ScannerTinyB10 { t.printStackTrace(); } - devicesInProcessing.remove(device.getAddress()); if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { final boolean r = device.getAdapter().startDiscovery( true ); println("****** Processing Device: startDiscovery result "+r); } - if( KEEP_CONNECTED && success ) { + if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) { while( device.pingGATT() ) { println("****** Processing Device: pingGATT OK: "+device.getAddress()); try { @@ -445,21 +458,15 @@ public class ScannerTinyB10 { e.printStackTrace(); } } - println("****** Processing Device: pingGATT failed: "+device.getAddress()); - } - - if( REMOVE_DEVICE ) { - println("****** Processing Device: removing: "+device.getAddress()); - device.remove(); - } else { - println("****** Processing Device: disconnecting: "+device.getAddress()); - device.disconnect(); // will implicitly purge the GATT data, including GATTCharacteristic listener. + println("****** Processing Device: pingGATT failed, waiting for disconnect: "+device.getAddress()); + // Even w/ GATT_PING_ENABLED, we utilize disconnect event to clean up -> remove } if( 0 < MULTI_MEASUREMENTS ) { MULTI_MEASUREMENTS--; println("****** Processing Device: MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS+": "+device.getAddress()); } + println("****** Processing Device: End: Success " + success + " on " + device.toString() + "; devInProc "+devicesInProcessing.size()); if( success ) { @@ -467,6 +474,20 @@ public class ScannerTinyB10 { } } + private void removeDevice(final BluetoothDevice device) { + println("****** Remove Device: removing: "+device.getAddress()); + device.getAdapter().stopDiscovery(); + + devicesInProcessing.remove(device.getAddress()); + + device.remove(); + + if( !USE_WHITELIST && 0 == devicesInProcessing.size() ) { + final boolean r = device.getAdapter().startDiscovery( true ); + println("****** Remove Device: startDiscovery result "+r); + } + } + public ScannerTinyB10(final String bluetoothManagerClazzName) { BluetoothManager _manager = null; final BluetoothFactory.ImplementationIdentifier implID = BluetoothFactory.getImplementationIdentifier(bluetoothManagerClazzName); @@ -630,6 +651,8 @@ public class ScannerTinyB10 { test.charIdentifierList.add(args[++i]); } else if( arg.equals("-disconnect") ) { test.KEEP_CONNECTED = false; + } else if( arg.equals("-enableGATTPing") ) { + test.GATT_PING_ENABLED = true; } else if( arg.equals("-keepDevice") ) { test.REMOVE_DEVICE = false; } else if( arg.equals("-count") && args.length > (i+1) ) { @@ -640,7 +663,7 @@ public class ScannerTinyB10 { } println("Run with '[-default_dev_id ] [-dev_id ] [-btmode LE|BREDR|DUAL] "+ "[-bluetoothManager ] "+ - "[-disconnect] [-count ] [-single] (-char )* [-show_update_events] [-silent_gatt]"+ + "[-disconnect] [-enableGATTPing] [-count ] [-single] (-char )* [-show_update_events] [-silent_gatt]"+ "(-mac )* (-wl )* "+ "[-verbose] [-debug] "+ "[-dbt_verbose true|false] "+ @@ -655,6 +678,7 @@ public class ScannerTinyB10 { println("BluetoothManager "+bluetoothManagerClazzName); println("MULTI_MEASUREMENTS "+test.MULTI_MEASUREMENTS); println("KEEP_CONNECTED "+test.KEEP_CONNECTED); + println("GATT_PING_ENABLED "+test.GATT_PING_ENABLED); println("REMOVE_DEVICE "+test.REMOVE_DEVICE); println("USE_WHITELIST "+test.USE_WHITELIST); println("SHOW_UPDATE_EVENTS "+test.SHOW_UPDATE_EVENTS); -- cgit v1.2.3