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

Enhance merging logic for Bluetooth standard weight profile

This commit is contained in:
oliexdev
2025-08-15 09:12:52 +02:00
parent bedf32c46b
commit 3459ac1833

View File

@@ -505,9 +505,11 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
} }
else { else {
if (previousMeasurement != null) { if (previousMeasurement != null) {
weightValue = previousMeasurement.getWeight(); float w = previousMeasurement.getWeight();
if (weightValue > 0) { if (w > 0) {
weightPresent = true; weightPresent = true;
weightValue = w;
scaleMeasurement.setWeight(w);
} }
} }
} }
@@ -554,29 +556,56 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
if (previousMeasurement == null) { if (previousMeasurement == null) {
if (newMeasurement.getUserId() == -1) { if (newMeasurement.getUserId() == -1) {
addScaleMeasurement(newMeasurement); addScaleMeasurement(newMeasurement);
} } else {
else {
previousMeasurement = newMeasurement; previousMeasurement = newMeasurement;
} }
return;
}
if (newMeasurement.getUserId() == -1 && previousMeasurement.getUserId() != -1) {
newMeasurement.setUserId(previousMeasurement.getUserId());
newMeasurement.setDateTime(previousMeasurement.getDateTime());
if (newMeasurement.getWeight() <= 0 && previousMeasurement.getWeight() > 0) {
newMeasurement.setWeight(previousMeasurement.getWeight());
}
LogManager.d(TAG, "merge: storing MERGED measurement (weight+bodycomp) for userId=" + newMeasurement.getUserId());
addScaleMeasurement(newMeasurement);
previousMeasurement = null;
return;
} }
else { else {
if ((newMeasurement.getUserId() == -1) && (previousMeasurement.getUserId() != -1)) { // Both measurements have a userId
// previousMeasurement.merge(newMeasurement); --> change to start if (previousMeasurement.getUserId() != -1 &&
newMeasurement.setUserId(previousMeasurement.getUserId()); newMeasurement.getUserId() != -1 &&
newMeasurement.setDateTime(previousMeasurement.getDateTime()); previousMeasurement.getUserId() == newMeasurement.getUserId()) {
// change end
addScaleMeasurement(previousMeasurement); // Optional: If the BodyComp packet has no timestamp, use the one from the previous measurement
if (newMeasurement.getDateTime() == null && previousMeasurement.getDateTime() != null) {
newMeasurement.setDateTime(previousMeasurement.getDateTime());
}
// If BodyComp packet has no weight (or 0), take it from the previous weight measurement
if ((newMeasurement.getWeight() <= 0) && (previousMeasurement.getWeight() > 0)) {
newMeasurement.setWeight(previousMeasurement.getWeight());
}
// Body composition fields (fat, muscle, water, lbm, bone, etc.) are already set in newMeasurement
// If you want to carry over additional fields from the previous measurement, add that here.
LogManager.d(TAG, "merge: storing MERGED measurement (both had userId) for userId=" + newMeasurement.getUserId());
addScaleMeasurement(newMeasurement);
previousMeasurement = null; previousMeasurement = null;
return;
} }
else {
addScaleMeasurement(previousMeasurement); // Fallback: no merge store the previous measurement, and keep the new one as previousMeasurement
if (newMeasurement.getUserId() == -1) { addScaleMeasurement(previousMeasurement);
addScaleMeasurement(newMeasurement); if (newMeasurement.getUserId() == -1) {
previousMeasurement = null; addScaleMeasurement(newMeasurement);
} previousMeasurement = null;
else { } else {
previousMeasurement = newMeasurement; previousMeasurement = newMeasurement;
}
} }
} }
} }