From 65bc1f02d99b833e231b0eb4859299d3f09a1640 Mon Sep 17 00:00:00 2001 From: Marco Gittler Date: Tue, 27 Nov 2018 19:49:53 +0100 Subject: [PATCH 1/2] much better BT connection dont send user data again if scale got it save weight only after 2500ms or if all data completed --- .../core/bluetooth/BluetoothSenssun.java | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java index 45a6e229..b9cacee7 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java @@ -35,8 +35,10 @@ public class BluetoothSenssun extends BluetoothCommunication { private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC = BluetoothGattUuid.fromShortCode(0xfff1); // read, notify private final UUID CMD_MEASUREMENT_CHARACTERISTIC = BluetoothGattUuid.fromShortCode(0xfff2); // write only - private int gotData; - private int FatMus = 0; + + private boolean scaleGotUserData; + long firstFixWeight = -1 ; + private byte WeightFatMus = 0; private ScaleMeasurement measurement; public BluetoothSenssun(Context context) { @@ -48,7 +50,16 @@ public class BluetoothSenssun extends BluetoothCommunication { return "Senssun"; } + @Override + protected boolean doScanWhileConnecting() { + // Medisana seems to have problem connecting if scan is running (see #278 and #353) + return false; + } + private void sendUserData(){ + if ( scaleGotUserData ){ + return; + } final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser(); byte gender = selectedUser.getGender().isMale() ? (byte)0xf1 : (byte)0x01; @@ -74,7 +85,9 @@ public class BluetoothSenssun extends BluetoothCommunication { setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, BluetoothGattUuid.DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION); sendUserData(); - gotData = 0; + firstFixWeight = -1; + WeightFatMus = 0; + scaleGotUserData = false; break; default: // Finish init if everything is done @@ -100,17 +113,16 @@ public class BluetoothSenssun extends BluetoothCommunication { // The first notification only includes weight and all other fields are // either 0x00 (user info) or 0xff (fat, water, etc.) - if (data != null) { + if (data != null && !isBitSet(WeightFatMus,3) ) { //only if not saved parseBytes(data); - if (measurement != null && measurement.getWeight() != 0.0 && gotData == 0) { - Timber.d("meas: %s", measurement); + Timber.d("WFM %02X %d ", WeightFatMus, ( System.currentTimeMillis() - firstFixWeight )); + if ( isBitSet(WeightFatMus,2) && firstFixWeight > 0 ) { + if ( ( ( System.currentTimeMillis() - firstFixWeight ) > 2500 && WeightFatMus == (1<<2) )//wait 1.5 seconds for Data + || WeightFatMus == 0x07 ) { // got all Data to save + addScaleData(measurement); - gotData = 1; - } - if (measurement != null && measurement.getWeight() != 0.0 && FatMus == 0x03 && gotData != 2) { - Timber.d("meas: %s", measurement); - addScaleData(measurement); - gotData = 2; + WeightFatMus |= 1 <<3; + } } } } @@ -122,12 +134,23 @@ public class BluetoothSenssun extends BluetoothCommunication { int type = weightBytes[6] & 0xff; Timber.d("type %02X", type); switch (type) { + case 0x00: + if ( weightBytes[2] == (byte)0x10 ){ + scaleGotUserData = true; + } + break; case 0xa0: sendUserData(); break; case 0xaa: float weight = Converters.fromUnsignedInt16Be(weightBytes, 2) / 10.0f; // kg measurement.setWeight(weight); + + if (!isBitSet(WeightFatMus,2)){ + WeightFatMus |= 1 << 2 ; + firstFixWeight = System.currentTimeMillis() ; + } + sendUserData(); break; case 0xb0: @@ -135,14 +158,14 @@ public class BluetoothSenssun extends BluetoothCommunication { float water = Converters.fromUnsignedInt16Be(weightBytes, 4) / 10.0f; // % measurement.setFat(fat); measurement.setWater(water); - FatMus |= 0x2; + WeightFatMus |= 1 << 1; break; case 0xc0: float bone = Converters.fromUnsignedInt16Le(weightBytes, 4) / 10.0f; // kg float muscle = Converters.fromUnsignedInt16Be(weightBytes, 2) / 10.0f; // % measurement.setMuscle(muscle); measurement.setBone(bone); - FatMus |= 0x1; + WeightFatMus |= 1; break; case 0xd0: float calorie = Converters.fromUnsignedInt16Be(weightBytes, 2); @@ -155,8 +178,6 @@ public class BluetoothSenssun extends BluetoothCommunication { //date break; } - - //measurement.setDateTime(lastWeighted); measurement.setDateTime(new Date()); } } From 34baeb22961a0f44b050d9edb2616678b12f3de6 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Tue, 27 Nov 2018 20:13:55 +0100 Subject: [PATCH 2/2] Fix comment and some whitespace --- .../core/bluetooth/BluetoothSenssun.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java index b9cacee7..07f1ff63 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSenssun.java @@ -35,7 +35,6 @@ public class BluetoothSenssun extends BluetoothCommunication { private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC = BluetoothGattUuid.fromShortCode(0xfff1); // read, notify private final UUID CMD_MEASUREMENT_CHARACTERISTIC = BluetoothGattUuid.fromShortCode(0xfff2); // write only - private boolean scaleGotUserData; long firstFixWeight = -1 ; private byte WeightFatMus = 0; @@ -52,12 +51,12 @@ public class BluetoothSenssun extends BluetoothCommunication { @Override protected boolean doScanWhileConnecting() { - // Medisana seems to have problem connecting if scan is running (see #278 and #353) + // Senssun seems to have problem connecting if scan is running (see ##309) return false; } - private void sendUserData(){ - if ( scaleGotUserData ){ + private void sendUserData() { + if (scaleGotUserData) { return; } final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser(); @@ -70,7 +69,7 @@ public class BluetoothSenssun extends BluetoothCommunication { byte cmdByte[] = {(byte)0xa5, (byte)0x10, gender, age, height, (byte)0, (byte)0x0, (byte)0x0d2, (byte)0x00}; byte verify = 0; - for(int i = 1; i < cmdByte.length - 2; i++) { + for (int i = 1; i < cmdByte.length - 2; i++) { verify = (byte) (verify + cmdByte[i]); } cmdByte[cmdByte.length - 2] = verify; @@ -113,16 +112,15 @@ public class BluetoothSenssun extends BluetoothCommunication { // The first notification only includes weight and all other fields are // either 0x00 (user info) or 0xff (fat, water, etc.) - if (data != null && !isBitSet(WeightFatMus,3) ) { //only if not saved + if (data != null && !isBitSet(WeightFatMus, 3)) { //only if not saved parseBytes(data); Timber.d("WFM %02X %d ", WeightFatMus, ( System.currentTimeMillis() - firstFixWeight )); - if ( isBitSet(WeightFatMus,2) && firstFixWeight > 0 ) { - if ( ( ( System.currentTimeMillis() - firstFixWeight ) > 2500 && WeightFatMus == (1<<2) )//wait 1.5 seconds for Data - || WeightFatMus == 0x07 ) { // got all Data to save - - addScaleData(measurement); - WeightFatMus |= 1 <<3; - } + if (isBitSet(WeightFatMus, 2) && firstFixWeight > 0) { + if (((System.currentTimeMillis() - firstFixWeight) > 2500 && WeightFatMus == (1 << 2)) //wait 1.5 seconds for Data + || WeightFatMus == 0x07) { // got all Data to save + addScaleData(measurement); + WeightFatMus |= 1 << 3; + } } } } @@ -135,8 +133,8 @@ public class BluetoothSenssun extends BluetoothCommunication { Timber.d("type %02X", type); switch (type) { case 0x00: - if ( weightBytes[2] == (byte)0x10 ){ - scaleGotUserData = true; + if (weightBytes[2] == (byte)0x10) { + scaleGotUserData = true; } break; case 0xa0: @@ -146,9 +144,9 @@ public class BluetoothSenssun extends BluetoothCommunication { float weight = Converters.fromUnsignedInt16Be(weightBytes, 2) / 10.0f; // kg measurement.setWeight(weight); - if (!isBitSet(WeightFatMus,2)){ - WeightFatMus |= 1 << 2 ; - firstFixWeight = System.currentTimeMillis() ; + if (!isBitSet(WeightFatMus, 2)) { + WeightFatMus |= 1 << 2 ; + firstFixWeight = System.currentTimeMillis() ; } sendUserData();