summaryrefslogtreecommitdiffstats
path: root/examples/java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-09-24 11:40:55 +0200
committerSven Gothel <[email protected]>2020-09-24 11:40:55 +0200
commit53eef8bf45c0500c68af83a5c57f6b348d995c5c (patch)
treee63cf9c1b3291309a06cd36b290dc8f67df507ba /examples/java
parent0dff786ec6b19a0c2e79a6dfac2a36ff9fb1e8eb (diff)
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"
Diffstat (limited to 'examples/java')
-rw-r--r--examples/java/ScannerTinyB10.java48
1 files changed, 36 insertions, 12 deletions
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<String> whitelist = new ArrayList<String>();
@@ -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 <adapter-index>] [-dev_id <adapter-index>] [-btmode LE|BREDR|DUAL] "+
"[-bluetoothManager <BluetoothManager-Implementation-Class-Name>] "+
- "[-disconnect] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-silent_gatt]"+
+ "[-disconnect] [-enableGATTPing] [-count <number>] [-single] (-char <uuid>)* [-show_update_events] [-silent_gatt]"+
"(-mac <device_address>)* (-wl <device_address>)* "+
"[-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);