From a66658c8296d19b7b7188f4f2ea3cd06bbe483a5 Mon Sep 17 00:00:00 2001 From: srwareham Date: Fri, 8 Mar 2019 08:37:21 -0500 Subject: [PATCH] Fix force QN-Scale to KG (#429) added unit support for QN scale --- .../core/bluetooth/BluetoothCommunication.java | 8 ++++++++ .../core/bluetooth/BluetoothQNScale.java | 15 +++++++++++++-- 2 files changed, 21 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 12063d1f..c57a313d 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 @@ -426,6 +426,14 @@ public abstract class BluetoothCommunication { return checksum; } + protected byte sumChecksum(byte[] data, int offset, int length) { + byte checksum = 0; + for (int i = offset; i < offset + length; ++i) { + checksum += data[i]; + } + return checksum; + } + /** * Test in a byte if a bit is set (1) or not (0) * diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothQNScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothQNScale.java index 1db66cf2..b8bfb628 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothQNScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothQNScale.java @@ -95,8 +95,19 @@ public class BluetoothQNScale extends BluetoothCommunication { setIndicationOn(CUSTOM2_MEASUREMENT_CHARACTERISTIC); break; case 2: - // write magicnumber 0x130915011000000042 to 0xffe3 - byte[] ffe3magicBytes = new byte[]{(byte) 0x13, (byte) 0x09, (byte) 0x15, (byte) 0x01, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x42}; + final ScaleUser scaleUser = OpenScale.getInstance().getSelectedScaleUser(); + final Converters.WeightUnit scaleUserWeightUnit = scaleUser.getScaleUnit(); + // Value of 0x01 = KG. 0x02 = LB. Requests with stones unit are sent as LB, with post-processing in vendor app. + byte weightUnitByte = (byte) 0x01; + // Default weight unit KG. If user config set to LB or ST, scale will show LB units, consistent with vendor app + if (scaleUserWeightUnit == Converters.WeightUnit.LB || scaleUserWeightUnit == Converters.WeightUnit.ST){ + weightUnitByte = (byte) 0x02; + } + // write magicnumber 0x130915[WEIGHT_BYTE]10000000[CHECK_SUM] to 0xffe3 + // 0x01 weight byte = KG. 0x02 weight byte = LB. + byte[] ffe3magicBytes = new byte[]{(byte) 0x13, (byte) 0x09, (byte) 0x15, weightUnitByte, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; + // Set last byte to be checksum + ffe3magicBytes[ffe3magicBytes.length -1] = sumChecksum(ffe3magicBytes, 0, ffe3magicBytes.length - 1); writeBytes(CUSTOM3_MEASUREMENT_CHARACTERISTIC, ffe3magicBytes); break; case 3: