From 269cbae1ccf0a38acdcbafba48575d6f58ec51f0 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Fri, 6 Jul 2018 00:50:07 +0200 Subject: [PATCH] Add support for other 1byone variant reported in #159 --- .../bluetooth/BluetoothCommunication.java | 4 ++ .../core/bluetooth/BluetoothOneByone.java | 39 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index 774fe77a..f992f6c7 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -101,6 +101,10 @@ public abstract class BluetoothCommunication { return bluetoothGatt.getServices(); } + protected boolean hasBluetoothGattService(UUID service) { + return bluetoothGatt != null && bluetoothGatt.getService(service) != null; + } + /** * Register a callback Bluetooth handler that notify any BT_STATUS_CODE changes for GUI/CORE. * diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java index 55215fae..329395b4 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java @@ -33,6 +33,8 @@ public class BluetoothOneByone extends BluetoothCommunication { private final UUID WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION = UUID.fromString("0000181B-0000-1000-8000-00805f9b34fb"); private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION = UUID.fromString("00002A9C-0000-1000-8000-00805f9b34fb"); // read, indication + private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION_OLD = UUID.fromString("0000fff4-0000-1000-8000-00805f9b34fb"); // notify + private final UUID WEIGHT_MEASUREMENT_SERVICE = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"); private final UUID CMD_MEASUREMENT_CHARACTERISTIC = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb"); // write only private final UUID WEIGHT_MEASUREMENT_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); @@ -53,7 +55,17 @@ public class BluetoothOneByone extends BluetoothCommunication { switch (stateNr) { case 0: lastWeight = 0; - setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG); + + if (hasBluetoothGattService(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION)) { + setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, + WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, + WEIGHT_MEASUREMENT_CONFIG); + } + else { + setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, + WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION_OLD, + WEIGHT_MEASUREMENT_CONFIG); + } break; case 1: ScaleUser currentUser = OpenScale.getInstance().getSelectedScaleUser(); @@ -94,11 +106,17 @@ public class BluetoothOneByone extends BluetoothCommunication { @Override public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) { final byte[] data = gattCharacteristic.getValue(); + if (data == null) { + return; + } // if data is valid data - if (data != null && data.length == 20) { + if (data.length == 20) { parseBytes(data); } + else if (data.length == 11) { + parseBytesOld(data); + } } private void parseBytes(byte[] weightBytes) { @@ -117,4 +135,21 @@ public class BluetoothOneByone extends BluetoothCommunication { addScaleData(scaleBtData); } } + + private void parseBytesOld(byte[] weightBytes) { + float weight = Converters.fromUnsignedInt16Be(weightBytes, 3) / 100.0f; + boolean done = (weightBytes[9] & 0xff) == 0; + + Timber.d("weight: %.2f%s", weight, done ? " (done)" : ""); + + // This check should be a bit more elaborate, but it works for now... + if (done && weight != lastWeight) { + lastWeight = weight; + + ScaleMeasurement scaleBtData = new ScaleMeasurement(); + scaleBtData.setWeight(weight); + + addScaleData(scaleBtData); + } + } }