1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-29 19:20:36 +02:00

Add support for other 1byone variant reported in #159

This commit is contained in:
Erik Johansson
2018-07-06 00:50:07 +02:00
parent d510c32f4c
commit 269cbae1cc
2 changed files with 41 additions and 2 deletions

View File

@@ -101,6 +101,10 @@ public abstract class BluetoothCommunication {
return bluetoothGatt.getServices(); 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. * Register a callback Bluetooth handler that notify any BT_STATUS_CODE changes for GUI/CORE.
* *

View File

@@ -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_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 = 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 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 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"); private final UUID WEIGHT_MEASUREMENT_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
@@ -53,7 +55,17 @@ public class BluetoothOneByone extends BluetoothCommunication {
switch (stateNr) { switch (stateNr) {
case 0: case 0:
lastWeight = 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; break;
case 1: case 1:
ScaleUser currentUser = OpenScale.getInstance().getSelectedScaleUser(); ScaleUser currentUser = OpenScale.getInstance().getSelectedScaleUser();
@@ -94,11 +106,17 @@ public class BluetoothOneByone extends BluetoothCommunication {
@Override @Override
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) { public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
final byte[] data = gattCharacteristic.getValue(); final byte[] data = gattCharacteristic.getValue();
if (data == null) {
return;
}
// if data is valid data // if data is valid data
if (data != null && data.length == 20) { if (data.length == 20) {
parseBytes(data); parseBytes(data);
} }
else if (data.length == 11) {
parseBytesOld(data);
}
} }
private void parseBytes(byte[] weightBytes) { private void parseBytes(byte[] weightBytes) {
@@ -117,4 +135,21 @@ public class BluetoothOneByone extends BluetoothCommunication {
addScaleData(scaleBtData); 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);
}
}
} }