1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-30 19:49:59 +02:00

BluetoothStandardWeightProfile: merge previous and new measurement if new measurement lacks user id (and timestamp);

Bluetooth scales usually implement both "Weight Scale Feature" and
"Body Composition Feature". It seems that scale first transmits weight
measurement (with user index and timestamp) and later transmits body
composition measurement (without user index and timestamp).
If previous measurement contains user index and new measurements does not
then merge them and store as one.
NOTE: On scales that implement only Weight Scale feature (or only Body
Composition feature) this commit will cause last measurement transmitted
to be delayed until disconnect.
This commit is contained in:
Krisjans Blukis
2021-07-27 19:24:02 +03:00
parent c95987a6ad
commit 05816ce0cf
2 changed files with 52 additions and 13 deletions

View File

@@ -92,17 +92,6 @@ public class BluetoothBeurerBF600 extends BluetoothStandardWeightProfile {
BluetoothGattUuidBF600.CHARACTERISTIC_BEURER_BF600_TAKE_MEASUREMENT, parser.getValue()); BluetoothGattUuidBF600.CHARACTERISTIC_BEURER_BF600_TAKE_MEASUREMENT, parser.getValue());
} }
@Override
protected void handleWeightMeasurement(byte[] value) {
scaleMeasurement = weightMeasurementToScaleMeasurement(value);
}
@Override
protected void handleBodyCompositionMeasurement(byte[] value) {
scaleMeasurement.merge(bodyCompositionMeasurementToScaleMeasurement(value));
addScaleMeasurement(scaleMeasurement);
}
@Override @Override
protected void setNotifyVendorSpecificUserList() { protected void setNotifyVendorSpecificUserList() {
setNotificationOn(BluetoothGattUuidBF600.SERVICE_BEURER_CUSTOM_BF600, setNotificationOn(BluetoothGattUuidBF600.SERVICE_BEURER_CUSTOM_BF600,

View File

@@ -61,12 +61,14 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
SharedPreferences prefs; SharedPreferences prefs;
protected boolean registerNewUser; protected boolean registerNewUser;
ScaleUser selectedUser; ScaleUser selectedUser;
ScaleMeasurement previousMeasurement;
public BluetoothStandardWeightProfile(Context context) { public BluetoothStandardWeightProfile(Context context) {
super(context); super(context);
this.prefs = PreferenceManager.getDefaultSharedPreferences(context); this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.selectedUser = OpenScale.getInstance().getSelectedScaleUser(); this.selectedUser = OpenScale.getInstance().getSelectedScaleUser();
this.registerNewUser = false; this.registerNewUser = false;
previousMeasurement = null;
} }
@Override @Override
@@ -343,7 +345,7 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
} }
protected void handleWeightMeasurement(byte[] value) { protected void handleWeightMeasurement(byte[] value) {
addScaleMeasurement(weightMeasurementToScaleMeasurement(value)); mergeWithPreviousScaleMeasurement(weightMeasurementToScaleMeasurement(value));
} }
protected ScaleMeasurement bodyCompositionMeasurementToScaleMeasurement(byte[] value) { protected ScaleMeasurement bodyCompositionMeasurementToScaleMeasurement(byte[] value) {
@@ -472,7 +474,55 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
} }
protected void handleBodyCompositionMeasurement(byte[] value) { protected void handleBodyCompositionMeasurement(byte[] value) {
addScaleMeasurement(bodyCompositionMeasurementToScaleMeasurement(value)); mergeWithPreviousScaleMeasurement(bodyCompositionMeasurementToScaleMeasurement(value));
}
/**
* Bluetooth scales usually implement both "Weight Scale Feature" and "Body Composition Feature".
* It seems that scale first transmits weight measurement (with user index and timestamp) and
* later transmits body composition measurement (without user index and timestamp).
* If previous measurement contains user index and new measurements does not then merge them and
* store as one.
* disconnect() function must store previousMeasurement to openScale db (if present).
*
* @param newMeasurement the scale data that should be merged with previous measurement or
* stored as previous measurement.
*/
protected void mergeWithPreviousScaleMeasurement(ScaleMeasurement newMeasurement) {
if (previousMeasurement == null) {
if (newMeasurement.getUserId() == -1) {
addScaleMeasurement(newMeasurement);
}
else {
previousMeasurement = newMeasurement;
}
}
else {
if ((newMeasurement.getUserId() == -1) && (previousMeasurement.getUserId() != -1)) {
previousMeasurement.merge(newMeasurement);
addScaleMeasurement(previousMeasurement);
previousMeasurement = null;
}
else {
addScaleMeasurement(previousMeasurement);
if (newMeasurement.getUserId() == -1) {
addScaleMeasurement(newMeasurement);
previousMeasurement = null;
}
else {
previousMeasurement = newMeasurement;
}
}
}
}
@Override
public void disconnect() {
if (previousMeasurement != null) {
addScaleMeasurement(previousMeasurement);
previousMeasurement = null;
}
super.disconnect();
} }
protected void setNotifyVendorSpecificUserList() { protected void setNotifyVendorSpecificUserList() {