summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-11 11:39:28 +0200
committerSven Gothel <[email protected]>2020-05-11 11:39:28 +0200
commit604e03053942521c1e104b0c473864ff4139b939 (patch)
tree4dbda3faae7ba83dc4fb0e92aeb6be240edd59b2
parenta783fb4776e059e78bc5665590a1641767ac0522 (diff)
Catching device loop exception - need to shutdown device while VM running!
****** Loop 3 Caught: Inconsistent discovered device count: Native 3, callback 1 java.lang.InternalError: Inconsistent discovered device count: Native 3, callback 1 at direct_bt.tinyb.DBTAdapter.removeDevices(DBTAdapter.java:280) at direct_bt.tinyb.DBTAdapter.startDiscovery(DBTAdapter.java:249) at ScannerTinyB01.main(ScannerTinyB01.java:194) ... Exception thrown due to discoveredDevices differences of native / java side. We may resolve this issue or allow such case. However, we need to properly shutdown the native direct_bt before the VM exits - otherwise we won't be able handle the lost Java references. TODO: Add a java shutdown hook!
-rw-r--r--examples/java/ScannerTinyB01.java170
1 files changed, 87 insertions, 83 deletions
diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java
index 7e1c18f4..ada30693 100644
--- a/examples/java/ScannerTinyB01.java
+++ b/examples/java/ScannerTinyB01.java
@@ -184,102 +184,106 @@ public class ScannerTinyB01 {
});
int loop = 0;
- do {
- loop++;
- System.err.println("****** Loop "+loop);
+ try {
+ do {
+ loop++;
+ System.err.println("****** Loop "+loop);
- final long t0 = BluetoothUtils.getCurrentMilliseconds();
+ final long t0 = BluetoothUtils.getCurrentMilliseconds();
- final boolean discoveryStarted = adapter.startDiscovery();
+ final boolean discoveryStarted = adapter.startDiscovery();
- System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode);
- if( !discoveryStarted ) {
- break;
- }
- BluetoothDevice sensor = null;
+ System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode);
+ if( !discoveryStarted ) {
+ break;
+ }
+ BluetoothDevice sensor = null;
- if( 0 == mode ) {
- synchronized(matchingDiscoveredDeviceBucket) {
- while( null == matchingDiscoveredDeviceBucket[0] ) {
- matchingDiscoveredDeviceBucket.wait(TO_DISCOVER);
+ if( 0 == mode ) {
+ synchronized(matchingDiscoveredDeviceBucket) {
+ while( null == matchingDiscoveredDeviceBucket[0] ) {
+ matchingDiscoveredDeviceBucket.wait(TO_DISCOVER);
+ }
+ sensor = matchingDiscoveredDeviceBucket[0];
+ matchingDiscoveredDeviceBucket[0] = null;
}
- sensor = matchingDiscoveredDeviceBucket[0];
- matchingDiscoveredDeviceBucket[0] = null;
- }
- } else if( 1 == mode ) {
- sensor = adapter.find(null, mac, TO_DISCOVER);
- } else {
- boolean timeout = false;
- while( null == sensor && !timeout ) {
- final List<BluetoothDevice> devices = adapter.getDevices();
- for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext() && !timeout; ) {
- final BluetoothDevice d = id.next();
- if(d.getAddress().equals(mac)) {
- sensor = d;
- break;
+ } else if( 1 == mode ) {
+ sensor = adapter.find(null, mac, TO_DISCOVER);
+ } else {
+ boolean timeout = false;
+ while( null == sensor && !timeout ) {
+ final List<BluetoothDevice> devices = adapter.getDevices();
+ for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext() && !timeout; ) {
+ final BluetoothDevice d = id.next();
+ if(d.getAddress().equals(mac)) {
+ sensor = d;
+ break;
+ }
+ final long tn = BluetoothUtils.getCurrentMilliseconds();
+ timeout = ( tn - t0 ) > TO_DISCOVER;
}
- final long tn = BluetoothUtils.getCurrentMilliseconds();
- timeout = ( tn - t0 ) > TO_DISCOVER;
}
}
- }
- final long t1 = BluetoothUtils.getCurrentMilliseconds();
- if (sensor == null) {
- System.err.println("No sensor found within "+(t1-t0)+" ms");
- System.exit(-1);
- }
- System.err.println("Found device in "+(t1-t0)+" ms: ");
- printDevice(sensor);
- adapter.stopDiscovery();
-
- final BooleanNotification connectedNotification = new BooleanNotification("Connected", t1);
- final BooleanNotification servicesResolvedNotification = new BooleanNotification("ServicesResolved", t1);
- sensor.enableConnectedNotifications(connectedNotification);
- sensor.enableServicesResolvedNotifications(servicesResolvedNotification);
-
- final long t2 = BluetoothUtils.getCurrentMilliseconds();
- final long t3;
- if ( sensor.connect() ) {
- t3 = BluetoothUtils.getCurrentMilliseconds();
- System.err.println("Sensor connected: "+(t3-t2)+" ms, total "+(t3-t0)+" ms");
- System.err.println("Sensor connectedNotification: "+connectedNotification.getValue());
- } else {
- t3 = BluetoothUtils.getCurrentMilliseconds();
- System.out.println("Could not connect device: "+(t3-t2)+" ms, total "+(t3-t0)+" ms");
- System.exit(-1);
- }
+ final long t1 = BluetoothUtils.getCurrentMilliseconds();
+ if (sensor == null) {
+ System.err.println("No sensor found within "+(t1-t0)+" ms");
+ System.exit(-1);
+ }
+ System.err.println("Found device in "+(t1-t0)+" ms: ");
+ printDevice(sensor);
+ adapter.stopDiscovery();
+
+ final BooleanNotification connectedNotification = new BooleanNotification("Connected", t1);
+ final BooleanNotification servicesResolvedNotification = new BooleanNotification("ServicesResolved", t1);
+ sensor.enableConnectedNotifications(connectedNotification);
+ sensor.enableServicesResolvedNotifications(servicesResolvedNotification);
+
+ final long t2 = BluetoothUtils.getCurrentMilliseconds();
+ final long t3;
+ if ( sensor.connect() ) {
+ t3 = BluetoothUtils.getCurrentMilliseconds();
+ System.err.println("Sensor connected: "+(t3-t2)+" ms, total "+(t3-t0)+" ms");
+ System.err.println("Sensor connectedNotification: "+connectedNotification.getValue());
+ } else {
+ t3 = BluetoothUtils.getCurrentMilliseconds();
+ System.out.println("Could not connect device: "+(t3-t2)+" ms, total "+(t3-t0)+" ms");
+ System.exit(-1);
+ }
- synchronized( servicesResolvedNotification ) {
- while( !servicesResolvedNotification.getValue() ) {
- final long tn = BluetoothUtils.getCurrentMilliseconds();
- if( tn - t3 > 20000 ) {
- break; // 20s TO
+ synchronized( servicesResolvedNotification ) {
+ while( !servicesResolvedNotification.getValue() ) {
+ final long tn = BluetoothUtils.getCurrentMilliseconds();
+ if( tn - t3 > 20000 ) {
+ break; // 20s TO
+ }
+ servicesResolvedNotification.wait();
}
- servicesResolvedNotification.wait();
}
- }
- final long t4;
- if ( servicesResolvedNotification.getValue() ) {
- t4 = BluetoothUtils.getCurrentMilliseconds();
- System.err.println("Sensor servicesResolved: "+(t4-t3)+" ms, total "+(t4-t0)+" ms");
- } else {
- t4 = BluetoothUtils.getCurrentMilliseconds();
- System.out.println("Could not connect device: "+(t4-t3)+" ms, total "+(t4-t0)+" ms");
- System.exit(-1);
- }
+ final long t4;
+ if ( servicesResolvedNotification.getValue() ) {
+ t4 = BluetoothUtils.getCurrentMilliseconds();
+ System.err.println("Sensor servicesResolved: "+(t4-t3)+" ms, total "+(t4-t0)+" ms");
+ } else {
+ t4 = BluetoothUtils.getCurrentMilliseconds();
+ System.out.println("Could not connect device: "+(t4-t3)+" ms, total "+(t4-t0)+" ms");
+ System.exit(-1);
+ }
- final List<BluetoothGattService> allBluetoothServices = sensor.getServices();
- if ( null == allBluetoothServices || allBluetoothServices.isEmpty() ) {
- System.err.println("No BluetoothGattService found!");
- } else {
- printAllServiceInfo(allBluetoothServices);
- }
+ final List<BluetoothGattService> allBluetoothServices = sensor.getServices();
+ if ( null == allBluetoothServices || allBluetoothServices.isEmpty() ) {
+ System.err.println("No BluetoothGattService found!");
+ } else {
+ printAllServiceInfo(allBluetoothServices);
+ }
- sensor.disconnect();
- sensor.remove();
- System.err.println("ScannerTinyB01 04 ...: "+adapter);
- } while( forever );
- System.err.println("ScannerTinyB01 01 stopDiscovery: "+adapter);
+ sensor.disconnect();
+ sensor.remove();
+ System.err.println("ScannerTinyB01 04 ...: "+adapter);
+ } while( forever );
+ } catch (final Throwable t) {
+ System.err.println("Caught: "+t.getMessage());
+ t.printStackTrace();
+ }
System.err.println("ScannerTinyB01 02 clear listener etc .. ");
adapter.removeStatusListener(statusListener);