Search in sources :

Example 1 with MiScaleLib

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

the class BluetoothMiScale2 method parseBytes.

private void parseBytes(byte[] data) {
    try {
        final byte ctrlByte0 = data[0];
        final byte ctrlByte1 = data[1];
        final boolean isWeightRemoved = isBitSet(ctrlByte1, 7);
        final boolean isDateInvalid = isBitSet(ctrlByte1, 6);
        final boolean isStabilized = isBitSet(ctrlByte1, 5);
        final boolean isLBSUnit = isBitSet(ctrlByte0, 0);
        final boolean isCattyUnit = isBitSet(ctrlByte1, 6);
        final boolean isImpedance = isBitSet(ctrlByte1, 1);
        if (isStabilized && !isWeightRemoved && !isDateInvalid) {
            final int year = ((data[3] & 0xFF) << 8) | (data[2] & 0xFF);
            final int month = (int) data[4];
            final int day = (int) data[5];
            final int hours = (int) data[6];
            final int min = (int) data[7];
            final int sec = (int) data[8];
            float weight;
            float impedance = 0.0f;
            if (isLBSUnit || isCattyUnit) {
                weight = (float) (((data[12] & 0xFF) << 8) | (data[11] & 0xFF)) / 100.0f;
            } else {
                weight = (float) (((data[12] & 0xFF) << 8) | (data[11] & 0xFF)) / 200.0f;
            }
            if (isImpedance) {
                impedance = ((data[10] & 0xFF) << 8) | (data[9] & 0xFF);
                Timber.d("impedance value is " + impedance);
            }
            String date_string = year + "/" + month + "/" + day + "/" + hours + "/" + min;
            Date date_time = new SimpleDateFormat("yyyy/MM/dd/HH/mm").parse(date_string);
            // Is the year plausible? Check if the year is in the range of 20 years...
            if (validateDate(date_time, 20)) {
                final ScaleUser scaleUser = OpenScale.getInstance().getSelectedScaleUser();
                ScaleMeasurement scaleBtData = new ScaleMeasurement();
                scaleBtData.setWeight(Converters.toKilogram(weight, scaleUser.getScaleUnit()));
                scaleBtData.setDateTime(date_time);
                int sex;
                if (scaleUser.getGender() == Converters.Gender.MALE) {
                    sex = 1;
                } else {
                    sex = 0;
                }
                if (impedance != 0.0f) {
                    MiScaleLib miScaleLib = new MiScaleLib(sex, scaleUser.getAge(), scaleUser.getBodyHeight());
                    scaleBtData.setWater(miScaleLib.getWater(weight, impedance));
                    scaleBtData.setVisceralFat(miScaleLib.getVisceralFat(weight));
                    scaleBtData.setFat(miScaleLib.getBodyFat(weight, impedance));
                    // convert muscle in kg to percent
                    scaleBtData.setMuscle((100.0f / scaleBtData.getWeight()) * miScaleLib.getMuscle(weight, impedance));
                    scaleBtData.setBone(miScaleLib.getBoneMass(weight, impedance));
                } else {
                    Timber.d("Impedance value is zero");
                }
                addScaleMeasurement(scaleBtData);
            } else {
                Timber.e("Invalid Mi scale weight year %d", year);
            }
        }
    } catch (ParseException e) {
        setBluetoothStatus(UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")");
    }
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) MiScaleLib(com.health.openscale.core.bluetooth.lib.MiScaleLib) Date(java.util.Date)

Aggregations

MiScaleLib (com.health.openscale.core.bluetooth.lib.MiScaleLib)1 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)1 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1