use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.
the class BluetoothStandardWeightProfile method bodyCompositionMeasurementToScaleMeasurement.
protected ScaleMeasurement bodyCompositionMeasurementToScaleMeasurement(byte[] value) {
String prefix = "bodyCompositionMeasurementToScaleMeasurement() ";
Timber.d(String.format(prefix + "value: [%s]", byteInHex(value)));
BluetoothBytesParser parser = new BluetoothBytesParser(value);
final int flags = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16);
boolean isKg = (flags & 0x0001) == 0;
float massMultiplier = (float) (isKg ? 0.005 : 0.01);
boolean timestampPresent = (flags & 0x0002) > 0;
boolean userIDPresent = (flags & 0x0004) > 0;
boolean bmrPresent = (flags & 0x0008) > 0;
boolean musclePercentagePresent = (flags & 0x0010) > 0;
boolean muscleMassPresent = (flags & 0x0020) > 0;
boolean fatFreeMassPresent = (flags & 0x0040) > 0;
boolean softLeanMassPresent = (flags & 0x0080) > 0;
boolean bodyWaterMassPresent = (flags & 0x0100) > 0;
boolean impedancePresent = (flags & 0x0200) > 0;
boolean weightPresent = (flags & 0x0400) > 0;
boolean heightPresent = (flags & 0x0800) > 0;
boolean multiPacketMeasurement = (flags & 0x1000) > 0;
Timber.d(String.format(prefix + "flags: 0x%02x ", flags) + "[" + (isKg ? "SI" : "Imperial") + (timestampPresent ? ", timestamp" : "") + (userIDPresent ? ", userID" : "") + (bmrPresent ? ", bmr" : "") + (musclePercentagePresent ? ", musclePercentage" : "") + (muscleMassPresent ? ", muscleMass" : "") + (fatFreeMassPresent ? ", fatFreeMass" : "") + (softLeanMassPresent ? ", softLeanMass" : "") + (bodyWaterMassPresent ? ", bodyWaterMass" : "") + (impedancePresent ? ", impedance" : "") + (weightPresent ? ", weight" : "") + (heightPresent ? ", height" : "") + (multiPacketMeasurement ? ", multiPacketMeasurement" : "") + "], " + String.format("reserved flags: 0x%04x ", flags & 0xe000));
ScaleMeasurement scaleMeasurement = new ScaleMeasurement();
float bodyFatPercentage = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
Timber.d(prefix + "bodyFatPercentage: " + bodyFatPercentage);
scaleMeasurement.setFat(bodyFatPercentage);
// Read timestamp if present
if (timestampPresent) {
Date timestamp = parser.getDateTime();
Timber.d(prefix + "timestamp: " + timestamp.toString());
scaleMeasurement.setDateTime(timestamp);
}
// Read userID if present
if (userIDPresent) {
int scaleUserIndex = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT8);
int userID = getUserIdFromScaleIndex(scaleUserIndex);
Timber.d(String.format(prefix + "scale user index: %d (app user id: %d)", scaleUserIndex, userID));
if (userID != -1) {
scaleMeasurement.setUserId(userID);
}
}
// Read bmr if present
if (bmrPresent) {
int bmrInJoules = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16);
int bmrInKcal = Math.round(((bmrInJoules / 4.1868f) * 10.0f) / 10.0f);
Timber.d(prefix + "bmrInJoules: " + bmrInJoules + " bmrInKcal: " + bmrInKcal);
}
// Read musclePercentage if present
if (musclePercentagePresent) {
float musclePercentage = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
Timber.d(prefix + "musclePercentage: " + musclePercentage);
scaleMeasurement.setMuscle(musclePercentage);
}
// Read muscleMass if present
if (muscleMassPresent) {
float muscleMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
Timber.d(prefix + "muscleMass: " + muscleMass);
}
// Read fatFreeMassPresent if present
if (fatFreeMassPresent) {
float fatFreeMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
Timber.d(prefix + "fatFreeMass: " + fatFreeMass);
}
// Read softleanMass if present
float softLeanMass = 0.0f;
if (softLeanMassPresent) {
softLeanMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
Timber.d(prefix + "softLeanMass: " + softLeanMass);
}
// Read bodyWaterMass if present
if (bodyWaterMassPresent) {
float bodyWaterMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
Timber.d(prefix + "bodyWaterMass: " + bodyWaterMass);
scaleMeasurement.setWater(bodyWaterMass);
}
// Read impedance if present
if (impedancePresent) {
float impedance = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
Timber.d(prefix + "impedance: " + impedance);
}
// Read weight if present
float weightValue = 0.0f;
if (weightPresent) {
weightValue = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
Timber.d(prefix + "weightValue: " + weightValue);
scaleMeasurement.setWeight(weightValue);
} else {
if (previousMeasurement != null) {
weightValue = previousMeasurement.getWeight();
if (weightValue > 0) {
weightPresent = true;
}
}
}
// calculate lean body mass and bone mass
if (weightPresent && softLeanMassPresent) {
float fatMass = weightValue * bodyFatPercentage / 100.0f;
float leanBodyMass = weightValue - fatMass;
float boneMass = leanBodyMass - softLeanMass;
scaleMeasurement.setLbm(leanBodyMass);
scaleMeasurement.setBone(boneMass);
}
// Read height if present
if (heightPresent) {
float heightValue = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16);
Timber.d(prefix + "heightValue: " + heightValue);
}
if (multiPacketMeasurement) {
Timber.e(prefix + "multiPacketMeasurement not supported!");
}
Timber.d(String.format("Got body composition: %s", byteInHex(value)));
return scaleMeasurement;
}
use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.
the class BluetoothStandardWeightProfile method registerUser.
protected void registerUser(int consentCode) {
BluetoothBytesParser parser = new BluetoothBytesParser(new byte[] { 0, 0, 0 });
parser.setIntValue(UDS_CP_REGISTER_NEW_USER, FORMAT_UINT8, 0);
parser.setIntValue(consentCode, FORMAT_UINT16, 1);
Timber.d(String.format("registerUser consentCode: %d", consentCode));
writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT, parser.getValue());
}
Aggregations