diff options
author | Sven Gothel <[email protected]> | 2020-07-04 01:20:39 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-04 01:20:39 +0200 |
commit | a1193c9d0ee566e23348dab03d358c53086e6d76 (patch) | |
tree | 69d5339af9735bdca88b2ff06a6b34fe084d2c60 /examples | |
parent | dd1b3f480eeb69a97a90ea7edc3281ca669334d4 (diff) |
org.tinyb.BluetoothFactory: More versatile ImplementationIdentifier and getBluetoothManager(..) using fq BluetoothManager implementation class name
Diffstat (limited to 'examples')
-rw-r--r-- | examples/java/ScannerTinyB00.java | 43 | ||||
-rw-r--r-- | examples/java/ScannerTinyB01.java | 44 | ||||
-rw-r--r-- | examples/java/ScannerTinyB10.java | 19 |
3 files changed, 81 insertions, 25 deletions
diff --git a/examples/java/ScannerTinyB00.java b/examples/java/ScannerTinyB00.java index e5cea322..7a45729a 100644 --- a/examples/java/ScannerTinyB00.java +++ b/examples/java/ScannerTinyB00.java @@ -46,7 +46,8 @@ public class ScannerTinyB00 { static long TO_DISCOVER = 60000; public static void main(final String[] args) throws InterruptedException { - int factory = 0; + String bluetoothManagerClazzName = BluetoothFactory.DirectBTImplementationID.BluetoothManagerClassName; + int dev_id = 0; // default String mac = null; int mode = 0; boolean forever = false; @@ -54,29 +55,44 @@ public class ScannerTinyB00 { for(int i=0; i< args.length; i++) { final String arg = args[i]; - if( arg.equals("-mac") ) { + if( arg.equals("-dev_id") && args.length > (i+1) ) { + dev_id = Integer.valueOf(args[++i]).intValue(); + } else if( arg.equals("-mac") ) { mac = args[++i]; } else if( arg.equals("-mode") ) { mode = Integer.valueOf(args[++i]).intValue(); - } else if( arg.equals("-factory") ) { - factory = Integer.valueOf(args[++i]).intValue(); + } else if( arg.equals("-bluetoothManager") && args.length > (i+1) ) { + bluetoothManagerClazzName = args[++i]; } else if( arg.equals("-forever") ) { forever = true; } } + System.err.println("BluetoothManager "+bluetoothManagerClazzName); + System.err.println("dev_id "+dev_id); + System.err.println("device: "+mac); + System.err.println("mode "+mode); + if ( null == mac ) { - System.err.println("Run with '-mac <device_address> [-mode <mode>] [-factory <BluetoothManager-Factory-Implementation-Class>]'"); + System.err.println("Run with '-mac <device_address> [-mode <mode>] [-bluetoothManager <BluetoothManager-Implementation-Class-Name>]'"); System.exit(-1); } - final BluetoothFactory.ImplementationIdentifier implID = 0 == factory ? BluetoothFactory.DirectBTImplementationID : BluetoothFactory.DBusImplementationID; final boolean useAdapter = mode/10 > 0; mode = mode %10; + final boolean isDirectBT; final BluetoothManager manager; { BluetoothManager _manager = null; + final BluetoothFactory.ImplementationIdentifier implID = BluetoothFactory.getImplementationIdentifier(bluetoothManagerClazzName); + if( null == implID ) { + System.err.println("Unable to find BluetoothManager "+bluetoothManagerClazzName); + System.exit(-1); + } + isDirectBT = BluetoothFactory.DirectBTImplementationID.equals(implID); + System.err.println("Using BluetoothManager "+bluetoothManagerClazzName); + System.err.println("Using Implementation "+implID+", isDirectBT "+isDirectBT); try { _manager = BluetoothFactory.getBluetoothManager( implID ); } catch (BluetoothException | NoSuchMethodException | SecurityException @@ -88,7 +104,18 @@ public class ScannerTinyB00 { } manager = _manager; } - final BluetoothAdapter adapter = manager.getDefaultAdapter(); + final BluetoothAdapter adapter; + { + final List<BluetoothAdapter> adapters = manager.getAdapters(); + for(int i=0; i < adapters.size(); i++) { + System.err.println("Adapter["+i+"]: "+adapters.get(i)); + } + if( adapters.size() <= dev_id ) { + System.err.println("No adapter dev_id "+dev_id+" available, adapter count "+adapters.size()); + System.exit(-1); + } + adapter = adapters.get(dev_id); + } do { final long t0 = System.currentTimeMillis();; @@ -163,7 +190,7 @@ public class ScannerTinyB00 { System.err.println("Sensor servicesResolved in "+(t3-t2)+" ms, total "+(t3-t1)+" ms"); } else { t3=0; - System.out.println("Could not connect device."); + System.out.println("Sensor service not resolved: "+(t3-t2)+" ms, total "+(t3-t1)+" ms"); System.exit(-1); } diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index 829d67ec..e807b58d 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -50,8 +50,8 @@ public class ScannerTinyB01 { /** 10,000 milliseconds */ static long TO_DISCOVER = 10000; - /** 500 milliseconds */ - static long TO_CONNECT = 500; + /** 20000 milliseconds */ + static long TO_CONNECT_AND_RESOLVE = 20000; static final String EUI48_ANY_DEVICE = "00:00:00:00:00:00"; static String waitForDevice = EUI48_ANY_DEVICE; @@ -59,7 +59,7 @@ public class ScannerTinyB01 { public static void main(final String[] args) throws InterruptedException { final boolean waitForEnter=false; long t0_discovery = TO_DISCOVER; - int factory = 0; + String bluetoothManagerClazzName = BluetoothFactory.DirectBTImplementationID.BluetoothManagerClassName; int dev_id = 0; // default int mode = 0; int max_loops = 1; @@ -74,8 +74,8 @@ public class ScannerTinyB01 { waitForDevice = args[++i]; } else if( arg.equals("-mode") && args.length > (i+1) ) { mode = Integer.valueOf(args[++i]).intValue(); - } else if( arg.equals("-factory") && args.length > (i+1) ) { - factory = Integer.valueOf(args[++i]).intValue(); + } else if( arg.equals("-bluetoothManager") && args.length > (i+1) ) { + bluetoothManagerClazzName = args[++i]; } else if( arg.equals("-t0_discovery") && args.length > (i+1) ) { t0_discovery = Long.valueOf(args[++i]).longValue(); } else if( arg.equals("-forever") ) { @@ -85,11 +85,13 @@ public class ScannerTinyB01 { } } - System.err.println("Run with '[-dev_id <adapter-index>] [-mac <device_address>] [-mode <mode>] [-factory <BluetoothManager-Factory-Implementation-Class>]'"); + System.err.println("Run with '[-dev_id <adapter-index>] [-mac <device_address>] [-mode <mode>] [-bluetoothManager <BluetoothManager-Implementation-Class-Name>]'"); } + System.err.println("BluetoothManager "+bluetoothManagerClazzName); System.err.println("dev_id "+dev_id); System.err.println("waitForDevice: "+waitForDevice); + System.err.println("mode "+mode); if( waitForEnter ) { System.err.println("Press ENTER to continue\n"); @@ -97,10 +99,18 @@ public class ScannerTinyB01 { } catch(final Exception e) { } } - final BluetoothFactory.ImplementationIdentifier implID = 0 == factory ? BluetoothFactory.DirectBTImplementationID : BluetoothFactory.DBusImplementationID; + final boolean isDirectBT; final BluetoothManager manager; { BluetoothManager _manager = null; + final BluetoothFactory.ImplementationIdentifier implID = BluetoothFactory.getImplementationIdentifier(bluetoothManagerClazzName); + if( null == implID ) { + System.err.println("Unable to find BluetoothManager "+bluetoothManagerClazzName); + System.exit(-1); + } + isDirectBT = BluetoothFactory.DirectBTImplementationID.equals(implID); + System.err.println("Using BluetoothManager "+bluetoothManagerClazzName); + System.err.println("Using Implementation "+implID+", isDirectBT "+isDirectBT); try { _manager = BluetoothFactory.getBluetoothManager( implID ); } catch (BluetoothException | NoSuchMethodException | SecurityException @@ -280,7 +290,7 @@ public class ScannerTinyB01 { synchronized( servicesResolvedNotification ) { while( !servicesResolvedNotification.getValue() ) { final long tn = BluetoothUtils.getCurrentMilliseconds(); - if( tn - t3 > TO_CONNECT ) { + if( tn - t3 > TO_CONNECT_AND_RESOLVE ) { break; } servicesResolvedNotification.wait(100); @@ -292,7 +302,7 @@ public class ScannerTinyB01 { 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.out.println("Sensor service not resolved: "+(t4-t3)+" ms, total "+(t4-t0)+" ms"); System.exit(-1); } @@ -300,8 +310,13 @@ public class ScannerTinyB01 { if ( null == primServices || primServices.isEmpty() ) { System.err.println("No BluetoothGattService found!"); } else { - final boolean addedCharacteristicListenerRes = - BluetoothGattService.addCharacteristicListenerToAll(sensor, primServices, myCharacteristicListener); + final boolean addedCharacteristicListenerRes; + if( isDirectBT ) { + addedCharacteristicListenerRes = + BluetoothGattService.addCharacteristicListenerToAll(sensor, primServices, myCharacteristicListener); + } else { + addedCharacteristicListenerRes = false; + } System.err.println("Added GATTCharacteristicListener: "+addedCharacteristicListenerRes); int i=0, j=0; @@ -324,7 +339,12 @@ public class ScannerTinyB01 { } Thread.sleep(1000); // FIXME: Wait for notifications - final boolean remRes = BluetoothGattService.removeCharacteristicListenerFromAll(sensor, primServices, myCharacteristicListener); + final boolean remRes; + if( isDirectBT ) { + remRes = BluetoothGattService.removeCharacteristicListenerFromAll(sensor, primServices, myCharacteristicListener); + } else { + remRes = false; + } System.err.println("Removed GATTCharacteristicListener: "+remRes); } sensor.disconnect(); diff --git a/examples/java/ScannerTinyB10.java b/examples/java/ScannerTinyB10.java index 1c1e2ad1..1d0c35c4 100644 --- a/examples/java/ScannerTinyB10.java +++ b/examples/java/ScannerTinyB10.java @@ -64,7 +64,7 @@ public class ScannerTinyB10 { boolean SHOW_UPDATE_EVENTS = false; - int factory = 0; + String bluetoothManagerClazzName = BluetoothFactory.DirectBTImplementationID.BluetoothManagerClassName; int dev_id = 0; // default @@ -296,10 +296,18 @@ public class ScannerTinyB10 { } public void runTest() { - final BluetoothFactory.ImplementationIdentifier implID = 0 == factory ? BluetoothFactory.DirectBTImplementationID : BluetoothFactory.DBusImplementationID; + final boolean isDirectBT; final BluetoothManager manager; { BluetoothManager _manager = null; + final BluetoothFactory.ImplementationIdentifier implID = BluetoothFactory.getImplementationIdentifier(bluetoothManagerClazzName); + if( null == implID ) { + System.err.println("Unable to find BluetoothManager "+bluetoothManagerClazzName); + System.exit(-1); + } + isDirectBT = BluetoothFactory.DirectBTImplementationID.equals(implID); + System.err.println("Using BluetoothManager "+bluetoothManagerClazzName); + System.err.println("Using Implementation "+implID+", isDirectBT "+isDirectBT); try { _manager = BluetoothFactory.getBluetoothManager( implID ); } catch (BluetoothException | NoSuchMethodException | SecurityException @@ -405,14 +413,15 @@ public class ScannerTinyB10 { } else if( arg.equals("-debug") ) { System.setProperty("org.tinyb.verbose", "true"); System.setProperty("org.tinyb.debug", "true"); - } else if( arg.equals("-factory") && args.length > (i+1) ) { - test.factory = Integer.valueOf(args[++i]).intValue(); + } else if( arg.equals("-bluetoothManager") && args.length > (i+1) ) { + test.bluetoothManagerClazzName = args[++i]; } } - System.err.println("Run with '[-dev_id <adapter-index>] (-mac <device_address>)* [-disconnect] [-count <number>] [-single] (-wl <device_address>)* [-show_update_events] [-factory <BluetoothManager-Factory-Implementation-Class>]'"); + System.err.println("Run with '[-dev_id <adapter-index>] (-mac <device_address>)* [-disconnect] [-count <number>] [-single] (-wl <device_address>)* [-show_update_events] [-bluetoothManager <BluetoothManager-Implementation-Class-Name>]'"); } + System.err.println("BluetoothManager "+test.bluetoothManagerClazzName); System.err.println("MULTI_MEASUREMENTS "+test.MULTI_MEASUREMENTS); System.err.println("KEEP_CONNECTED "+test.KEEP_CONNECTED); System.err.println("REMOVE_DEVICE "+test.REMOVE_DEVICE); |