summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-02-11 17:38:22 +0200
committerPetre Eftime <[email protected]>2016-02-11 17:40:41 +0200
commit9e1a0a24cd4f46463dd67aaee17384e258f36c0a (patch)
tree3f546b5fa690612bff37b235d6b404df6e65d7dc
parent5010c46a910b1e298e6992f5074afcd3433a6ed9 (diff)
Reorganize java example
Signed-off-by: Petre Eftime <[email protected]>
-rw-r--r--examples/java/CMakeLists.txt6
-rw-r--r--examples/java/HelloTinyB.java144
-rw-r--r--examples/java/Hellotinyb.java63
3 files changed, 147 insertions, 66 deletions
diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt
index 4aff2496..33d7a175 100644
--- a/examples/java/CMakeLists.txt
+++ b/examples/java/CMakeLists.txt
@@ -2,9 +2,9 @@ find_package(Java REQUIRED)
include(UseJava)
-add_jar(Hellotinyb SOURCES Hellotinyb.java INCLUDE_JARS "${CMAKE_CURRENT_BINARY_DIR}/../../java/tinyb.jar" ENTRY_POINT Hellotinyb)
+add_jar(HelloTinyB SOURCES HelloTinyB.java INCLUDE_JARS "${CMAKE_CURRENT_BINARY_DIR}/../../java/tinyb.jar" ENTRY_POINT HelloTinyB)
-add_custom_command(TARGET Hellotinyb
+add_custom_command(TARGET HelloTinyB
POST_BUILD
- COMMAND cp "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/Hellotinyb.dir/Hellotinyb.class" "${CMAKE_CURRENT_BINARY_DIR}"
+ COMMAND cp "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/HelloTinyB.dir/HelloTinyB.class" "${CMAKE_CURRENT_BINARY_DIR}"
)
diff --git a/examples/java/HelloTinyB.java b/examples/java/HelloTinyB.java
new file mode 100644
index 00000000..84656581
--- /dev/null
+++ b/examples/java/HelloTinyB.java
@@ -0,0 +1,144 @@
+import tinyb.*;
+import java.util.*;
+
+public class HelloTinyB {
+ private static final float SCALE_LSB = 0.03125f;
+
+ static void printDevice(BluetoothDevice device) {
+ System.out.print("Address = " + device.getAddress());
+ System.out.print("Name = " + device.getName());
+ System.out.print("Connected = " + device.getConnected());
+ System.out.println("");
+ }
+
+ /*
+ * After discovery is started, new devices will be detected. We can
+ * get a list of all devices through the manager's getDevices
+ * method.
+ * We can the look through the list of devices to find the device
+ * with the MAC which we provided as a parameter.
+ * We continue looking until we find it, or we try 15 times (1 minutes).
+ */
+ static BluetoothDevice getDevice(String address) throws InterruptedException {
+ BluetoothManager manager = BluetoothManager.getBluetoothManager();
+ BluetoothDevice sensor = null;
+ for (int i = 0; i < 15; ++i) {
+ List<BluetoothDevice> list = manager.getDevices();
+
+ for (BluetoothDevice device : list) {
+ printDevice(device);
+ /*
+ * Here we check if the address matches.
+ */
+ if (device.getAddress().equals(address))
+ sensor = device;
+ }
+
+ if (sensor != null) {
+ return sensor;
+ }
+ Thread.sleep(4000);
+ }
+ return null;
+ }
+
+ /*
+ * Our device should expose a temperature service, which has a UUID
+ * we can find out from the data sheet.
+ * The service description of the SensorTag can be found here:
+ * http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
+ * The service we are looking for has the short UUID AA00 which we insert into
+ * the TI Base UUID: f000XXXX-0451-4000-b000-000000000000
+ */
+ static BluetoothGattService getService (BluetoothDevice device, String UUID) throws InterruptedException {
+ System.out.println("Services exposed by device:");
+ BluetoothGattService tempService = null;
+ List<BluetoothGattService> bluetoothServices = null;
+ do {
+ bluetoothServices = device.getServices();
+
+ for (BluetoothGattService service : bluetoothServices) {
+ System.out.println("UUID: " + service.getUuid());
+ if (service.getUuid().equals(UUID))
+ tempService = service;
+ }
+ Thread.sleep(4000);
+ } while (bluetoothServices != null && bluetoothServices.isEmpty());
+ return tempService;
+ }
+
+ /*
+ * This program connects to a TI SensorTag 2.0 and reads the temperature
+ * characteristic exposed by the device over Bluetooth Low Energy.
+ * The parameter provided to the program should be the MAC address of
+ * the device.
+ *
+ * A wiki describing the sensor is found here:
+ * http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User's_Guide
+ *
+ * The API used in this example is based on TinyB v0.3, which only supports
+ * polling, but v0.4 will introduce a simplied API for discovering devices
+ * and services.
+ */
+ public static void main(String[] args) throws InterruptedException {
+
+ if (args.length < 1) {
+ System.err.println("Run with <device_address> argument");
+ System.exit(-1);
+ }
+
+ /*
+ * To start looking of the device, we first must initialize the TinyB
+ * library. The way of interacting with the library is through
+ * the BluetoothManager. There can be only one BluetoothManager at one
+ * time, and the reference to it is obtained through the
+ * getBluetoothManager method.
+ */
+ BluetoothManager manager = BluetoothManager.getBluetoothManager();
+
+ /*
+ * The manager will try to initialize a BluetoothAdapter if any adapter
+ * is present in the system. To initialize discovery we can call
+ * startDiscovery, which will put the default adapter in discovery mode.
+ */
+ boolean discoveryStarted = manager.startDiscovery();
+
+ System.out.println("The discovery started: " + (discoveryStarted ? "true" : "false"));
+ BluetoothDevice sensor = getDevice(args[0]);
+
+ /*
+ * After we find the device we can stop looking for other devices.
+ */
+ manager.stopDiscovery();
+
+ if (sensor == null) {
+ System.err.println("No sensor found with the provided address.");
+ System.exit(-1);
+ }
+
+ System.out.print("Found device: ");
+ printDevice(sensor);
+
+ if (sensor.connect())
+ System.out.println("Sensor with the provided address connected");
+ else {
+ System.out.println("Could not connect device.");
+ System.exit(-1);
+ }
+
+ BluetoothGattService tempService =
+ getService(sensor, "f000aa00-0451-4000-b000-000000000000");
+
+ if (tempService == null) {
+ /*
+ * This device does not have the temperature service we are looking for.
+ */
+ sensor.disconnect();
+ System.exit(-1);
+ }
+
+ List<BluetoothGattCharacteristic> temperatureCharacteristics =
+ tempService.getCharacteristics();
+
+ }
+}
diff --git a/examples/java/Hellotinyb.java b/examples/java/Hellotinyb.java
deleted file mode 100644
index a4e5ea7d..00000000
--- a/examples/java/Hellotinyb.java
+++ /dev/null
@@ -1,63 +0,0 @@
-import tinyb.*;
-import java.util.*;
-
-public class Hellotinyb {
- private static final float SCALE_LSB = 0.03125f;
-
- 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());
-
- sensor.disconnect();
- }
-}