diff options
author | Andrei Vasiliu <[email protected]> | 2016-01-08 10:32:48 +0200 |
---|---|---|
committer | Andrei Vasiliu <[email protected]> | 2016-01-12 00:09:46 +0200 |
commit | 8d94e75c413833b526e9b13e25a15a6919737f75 (patch) | |
tree | e2c003966c5e31685ce30894fbf8187ff847134a /examples | |
parent | a97afed4dfbdac2e364f6bd50ad6a86dfe5417c7 (diff) |
Java: Simple example for Java API
Signed-off-by: Andrei Vasiliu <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Hellotinyb.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/Hellotinyb.java b/examples/Hellotinyb.java new file mode 100644 index 00000000..755970e3 --- /dev/null +++ b/examples/Hellotinyb.java @@ -0,0 +1,69 @@ +import java.util.*; + +public class Hellotinyb { + private static final float SCALE_LSB = 0.03125f; + + static { + try { + System.loadLibrary("javatinyb"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load." + e); + System.exit(-1); + } + } + + public static void main(String[] args) throws InterruptedException { + BluetoothManager manager = BluetoothManager.getBluetoothManager(); + + if (args.length < 1) { + System.err.println("Run with <device_address> argument"); + System.exit(-1); + } + + boolean discoveryStarted = manager.startDiscovery(); + + System.out.println("The discovery started: " + (discoveryStarted ? "true" : "false")); + BluetoothDevice sensor = null; + for (int i = 0; i < 15; ++i) { + List<BluetoothDevice> list = manager.getDevices(); + + for (BluetoothDevice device : list) { + System.out.println("Address = " + device.getAddress()); + System.out.println("Name = " + device.getName()); + System.out.println("Connected = " + device.getConnected()); + + if (device.getAddress().equals(args[0])) { + sensor = device; + } + } + if (sensor != null) { + break; + } + System.out.println(""); + Thread.sleep(4000); + } + + Thread.sleep(4000); + manager.stopDiscovery(); + + if (sensor == null) { + System.out.println("No sensor found with the provided address."); + System.exit(-1); + } + + Thread.sleep(4000); + sensor.connect(); + System.out.println("Sensor with the provided address connected"); + + List<BluetoothGattService> bluetoothServices = null; + + do { + Thread.sleep(4000); + bluetoothServices = sensor.getServices(); + + for (BluetoothGattService service : bluetoothServices) { + System.out.println("UUID: " + service.getUuid()); + } + } while (bluetoothServices.isEmpty()); + } +} |