aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-02-15 15:31:52 +0200
committerPetre Eftime <[email protected]>2016-02-15 15:31:52 +0200
commit90088035d6380533fabf151114ba2c403f85b795 (patch)
treef21eb3a940e012811f439d5da828daf576769d81
parent925652c13037838dbd9b756344340db77c9ab669 (diff)
java: Update HelloTinyB sample to read temp from TI Sensor Tagv0.3.2
Signed-off-by: Petre Eftime <[email protected]>
-rw-r--r--examples/java/HelloTinyB.java177
1 files changed, 118 insertions, 59 deletions
diff --git a/examples/java/HelloTinyB.java b/examples/java/HelloTinyB.java
index 84656581..2a2fbb26 100644
--- a/examples/java/HelloTinyB.java
+++ b/examples/java/HelloTinyB.java
@@ -3,54 +3,54 @@ import java.util.*;
public class HelloTinyB {
private static final float SCALE_LSB = 0.03125f;
+ static boolean running = true;
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("");
+ System.out.print(" Name = " + device.getName());
+ System.out.print(" Connected = " + device.getConnected());
+ System.out.println();
+ }
+
+ static float convertCelsius(int raw) {
+ return raw / 128f;
}
/*
- * 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).
+ * 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;
- }
+ for (int i = 0; (i < 15) && running; ++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);
+ if (sensor != null) {
+ return sensor;
}
- return null;
+ 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
+ * 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 {
+ static BluetoothGattService getService(BluetoothDevice device, String UUID) throws InterruptedException {
System.out.println("Services exposed by device:");
BluetoothGattService tempService = null;
List<BluetoothGattService> bluetoothServices = null;
@@ -59,26 +59,32 @@ public class HelloTinyB {
for (BluetoothGattService service : bluetoothServices) {
System.out.println("UUID: " + service.getUuid());
- if (service.getUuid().equals(UUID))
- tempService = service;
+ if (service.getUuid().equals(UUID))
+ tempService = service;
}
Thread.sleep(4000);
- } while (bluetoothServices != null && bluetoothServices.isEmpty());
+ } while (bluetoothServices != null && bluetoothServices.isEmpty() && running);
return tempService;
}
+ static BluetoothGattCharacteristic getCharacteristic(BluetoothGattService service, String UUID) {
+ List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
+
+ for (BluetoothGattCharacteristic characteristic : characteristics) {
+ if (characteristic.getUuid().equals(UUID))
+ return characteristic;
+ }
+ return null;
+ }
+
/*
- * 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.
+ * 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
+ * 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.
+ * 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 {
@@ -87,19 +93,28 @@ public class HelloTinyB {
System.exit(-1);
}
+ final Thread mainThread = Thread.currentThread();
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ public void run() {
+ running = false;
+ try {
+ mainThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
/*
- * 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.
+ * 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.
+ * 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();
@@ -126,19 +141,63 @@ public class HelloTinyB {
System.exit(-1);
}
- BluetoothGattService tempService =
- getService(sensor, "f000aa00-0451-4000-b000-000000000000");
+ BluetoothGattService tempService = getService(sensor, "f000aa00-0451-4000-b000-000000000000");
if (tempService == null) {
- /*
- * This device does not have the temperature service we are looking for.
- */
+ System.err.println("This device does not have the temperature service we are looking for.");
sensor.disconnect();
System.exit(-1);
}
+ System.out.println("Found service " + tempService.getUuid());
- List<BluetoothGattCharacteristic> temperatureCharacteristics =
- tempService.getCharacteristics();
+ BluetoothGattCharacteristic tempValue = getCharacteristic(tempService, "f000aa01-0451-4000-b000-000000000000");
+ BluetoothGattCharacteristic tempConfig = getCharacteristic(tempService, "f000aa02-0451-4000-b000-000000000000");
+ BluetoothGattCharacteristic tempPeriod = getCharacteristic(tempService, "f000aa03-0451-4000-b000-000000000000");
+
+ if (tempValue == null || tempConfig == null || tempPeriod == null) {
+ System.err.println("Could not find the correct characteristics.");
+ sensor.disconnect();
+ System.exit(-1);
+ }
+
+ System.out.println("Found the temperature characteristics");
+
+ /*
+ * Turn on the Temperature Service by writing 1 in the configuration characteristic, as mentioned in the PDF
+ * mentioned above. We could also modify the update interval, by writing in the period characteristic, but the
+ * default 1s is good enough for our purposes.
+ */
+ byte[] config = { 0x01 };
+ tempConfig.writeValue(config);
+
+ /*
+ * Each second read the value characteristic and display it in a human readable format.
+ */
+ while (running) {
+ byte[] tempRaw = tempValue.readValue();
+ System.out.print("Temp raw = {");
+ for (byte b : tempRaw) {
+ System.out.print(String.format("%02x,", b));
+ }
+ System.out.print("}");
+
+ /*
+ * The temperature service returns the data in an encoded format which can be found in the wiki. Convert the
+ * raw temperature format to celsius and print it. Conversion for object temperature depends on ambient
+ * according to wiki, but assume result is good enough for our purposes without conversion.
+ */
+ int objectTempRaw = tempRaw[0] + (tempRaw[1] << 8);
+ int ambientTempRaw = tempRaw[2] + (tempRaw[3] << 8);
+
+ float objectTempCelsius = convertCelsius(objectTempRaw);
+ float ambientTempCelsius = convertCelsius(ambientTempRaw);
+
+ System.out.println(
+ String.format(" Temp: Object = %fC, Ambient = %fC", objectTempCelsius, ambientTempCelsius));
+
+ Thread.sleep(1000);
+ }
+ sensor.disconnect();
}
}