From c7056c880c856faa92924c652844ead074bae1fa Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Wed, 18 Apr 2018 22:18:29 +0200 Subject: [PATCH] Move xorChecksum method to base class --- .../core/bluetooth/BluetoothCommunication.java | 8 ++++++++ .../core/bluetooth/BluetoothYunmaiSE_Mini.java | 16 ++++------------ 2 files changed, 12 insertions(+), 12 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 359b775b..03abeae4 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 @@ -312,6 +312,14 @@ public abstract class BluetoothCommunication { return stringBuilder.toString(); } + protected byte xorChecksum(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/BluetoothYunmaiSE_Mini.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java index 1a36a4a7..99e1053e 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java @@ -66,7 +66,8 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { userId[0], userId[1], (byte) selectedUser.getBodyHeight(), sex, (byte) selectedUser.getAge(), (byte) 0x55, (byte) 0x5a, (byte) 0x00, (byte)0x00, display_unit, (byte) 0x03, (byte) 0x00}; - user_add_or_query[17] = xor_checksum(user_add_or_query); + user_add_or_query[user_add_or_query.length - 1] = + xorChecksum(user_add_or_query, 0, user_add_or_query.length - 1); writeBytes(WEIGHT_CMD_SERVICE, WEIGHT_CMD_CHARACTERISTIC, user_add_or_query); break; case 1: @@ -75,8 +76,9 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { byte[] set_time = new byte[]{(byte)0x0d, (byte) 0x0d, (byte) 0x11, unixTime[0], unixTime[1], unixTime[2], unixTime[3], (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00}; + set_time[set_time.length - 1] = + xorChecksum(set_time, 0, set_time.length - 1); - set_time[12] = xor_checksum(set_time); writeBytes(WEIGHT_CMD_SERVICE, WEIGHT_CMD_CHARACTERISTIC, set_time); break; case 2: @@ -153,14 +155,4 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { return uniqueNumber + userId; } - - private byte xor_checksum(byte[] data) { - byte checksum = 0x00; - - for (int i=0; i