Search in sources :

Example 1 with OneByoneLib

use of com.health.openscale.core.bluetooth.lib.OneByoneLib in project openScale by oliexdev.

the class BluetoothOneByone method parseBytes.

private void parseBytes(byte[] weightBytes) {
    float weight = Converters.fromUnsignedInt16Le(weightBytes, 3) / 100.0f;
    float impedanceValue = ((float) (((weightBytes[2] & 0xFF) << 8) + (weightBytes[1] & 0xFF))) * 0.1f;
    boolean impedancePresent = (weightBytes[9] != 1) && (impedanceValue != 0);
    boolean dateTimePresent = weightBytes.length >= 18;
    if (!impedancePresent || (!dateTimePresent && historicMeasurement)) {
        // unwanted, no impedance or historic measurement w/o time-stamp
        return;
    }
    Calendar dateTime = Calendar.getInstance();
    if (dateTimePresent) {
        // 18-byte or longer measurements contain date and time, used in history
        dateTime.set(Converters.fromUnsignedInt16Be(weightBytes, 11), weightBytes[13] - 1, weightBytes[14], weightBytes[15], weightBytes[16], weightBytes[17]);
    }
    final ScaleUser scaleUser = OpenScale.getInstance().getSelectedScaleUser();
    Timber.d("received bytes [%s]", byteInHex(weightBytes));
    Timber.d("received decoded bytes [weight: %.2f, impedanceValue: %f]", weight, impedanceValue);
    Timber.d("user [%s]", scaleUser);
    int sex = 0, peopleType = 0;
    if (scaleUser.getGender() == Converters.Gender.MALE) {
        sex = 1;
    } else {
        sex = 0;
    }
    switch(scaleUser.getActivityLevel()) {
        case SEDENTARY:
            peopleType = 0;
            break;
        case MILD:
            peopleType = 0;
            break;
        case MODERATE:
            peopleType = 1;
            break;
        case HEAVY:
            peopleType = 2;
            break;
        case EXTREME:
            peopleType = 2;
            break;
    }
    OneByoneLib oneByoneLib = new OneByoneLib(sex, scaleUser.getAge(), scaleUser.getBodyHeight(), peopleType);
    ScaleMeasurement scaleBtData = new ScaleMeasurement();
    scaleBtData.setWeight(weight);
    try {
        dateTime.setLenient(false);
        scaleBtData.setDateTime(dateTime.getTime());
        scaleBtData.setFat(oneByoneLib.getBodyFat(weight, impedanceValue));
        scaleBtData.setWater(oneByoneLib.getWater(scaleBtData.getFat()));
        scaleBtData.setBone(oneByoneLib.getBoneMass(weight, impedanceValue));
        scaleBtData.setVisceralFat(oneByoneLib.getVisceralFat(weight));
        scaleBtData.setMuscle(oneByoneLib.getMuscle(weight, impedanceValue));
        scaleBtData.setLbm(oneByoneLib.getLBM(weight, scaleBtData.getFat()));
        Timber.d("scale measurement [%s]", scaleBtData);
        if (dateTime.getTimeInMillis() - lastDateTime.getTimeInMillis() < DATE_TIME_THRESHOLD) {
            // don't save measurements too close to each other
            return;
        }
        lastDateTime = dateTime;
        addScaleMeasurement(scaleBtData);
    } catch (IllegalArgumentException e) {
        if (historicMeasurement) {
            Timber.d("invalid time-stamp: year %d, month %d, day %d, hour %d, minute %d, second %d", Converters.fromUnsignedInt16Be(weightBytes, 11), weightBytes[13], weightBytes[14], weightBytes[15], weightBytes[16], weightBytes[17]);
            // discard historic measurement with invalid time-stamp
            return;
        }
    }
}
Also used : OneByoneLib(com.health.openscale.core.bluetooth.lib.OneByoneLib) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Calendar(java.util.Calendar)

Aggregations

OneByoneLib (com.health.openscale.core.bluetooth.lib.OneByoneLib)1 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)1 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)1 Calendar (java.util.Calendar)1