Search in sources :

Example 16 with BluetoothBytesParser

use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.

the class BluetoothStandardWeightProfile method writeBirthday.

protected void writeBirthday() {
    BluetoothBytesParser parser = new BluetoothBytesParser();
    parser.setDateTime(dateToCalender(this.selectedUser.getBirthday()));
    writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_DATE_OF_BIRTH, Arrays.copyOfRange(parser.getValue(), 0, 3));
}
Also used : BluetoothBytesParser(com.welie.blessed.BluetoothBytesParser)

Example 17 with BluetoothBytesParser

use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.

the class BluetoothBeurerBF105 method writeActivityLevel.

@Override
protected void writeActivityLevel() {
    BluetoothBytesParser parser = new BluetoothBytesParser();
    int activityLevel = this.selectedUser.getActivityLevel().toInt() + 1;
    Timber.d(String.format("activityLevel: %d", activityLevel));
    parser.setIntValue(activityLevel, FORMAT_UINT8);
    writeBytes(SERVICE_BF105_CUSTOM, CHARACTERISTIC_ACTIVITY_LEVEL, parser.getValue());
}
Also used : BluetoothBytesParser(com.welie.blessed.BluetoothBytesParser)

Example 18 with BluetoothBytesParser

use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.

the class BluetoothStandardWeightProfile method onBluetoothNotify.

@Override
public void onBluetoothNotify(UUID characteristic, byte[] value) {
    BluetoothBytesParser parser = new BluetoothBytesParser(value);
    if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME)) {
        Date currentTime = parser.getDateTime();
        Timber.d(String.format("Received device time: %s", currentTime));
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_WEIGHT_MEASUREMENT)) {
        handleWeightMeasurement(value);
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_BODY_COMPOSITION_MEASUREMENT)) {
        handleBodyCompositionMeasurement(value);
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL)) {
        int batteryLevel = parser.getIntValue(FORMAT_UINT8);
        Timber.d(String.format("Received battery level %d%%", batteryLevel));
        if (batteryLevel <= 10) {
            sendMessage(R.string.info_scale_low_battery, batteryLevel);
        }
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_MANUFACTURER_NAME_STRING)) {
        String manufacturer = parser.getStringValue(0);
        Timber.d(String.format("Received manufacturer: %s", manufacturer));
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_MODEL_NUMBER_STRING)) {
        String modelNumber = parser.getStringValue(0);
        Timber.d(String.format("Received modelnumber: %s", modelNumber));
    } else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT)) {
        handleUserControlPointNotify(value);
    } else {
        Timber.d(String.format("Notification from unhandled characteristic: %s, value: [%s]", characteristic.toString(), byteInHex(value)));
    }
}
Also used : BluetoothBytesParser(com.welie.blessed.BluetoothBytesParser) Date(java.util.Date)

Example 19 with BluetoothBytesParser

use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.

the class BluetoothStandardWeightProfile method weightMeasurementToScaleMeasurement.

protected ScaleMeasurement weightMeasurementToScaleMeasurement(byte[] value) {
    String prefix = "weightMeasurementToScaleMeasurement() ";
    Timber.d(String.format(prefix + "value: [%s]", byteInHex(value)));
    BluetoothBytesParser parser = new BluetoothBytesParser(value);
    final int flags = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT8);
    boolean isKg = (flags & 0x01) == 0;
    final boolean timestampPresent = (flags & 0x02) > 0;
    final boolean userIDPresent = (flags & 0x04) > 0;
    final boolean bmiAndHeightPresent = (flags & 0x08) > 0;
    Timber.d(String.format(prefix + "flags: 0x%02x ", flags) + "[" + (isKg ? "SI" : "Imperial") + (timestampPresent ? ", timestamp" : "") + (userIDPresent ? ", userID" : "") + (bmiAndHeightPresent ? ", bmiAndHeight" : "") + "], " + String.format("reserved flags: 0x%02x ", flags & 0xf0));
    ScaleMeasurement scaleMeasurement = new ScaleMeasurement();
    // Determine the right weight multiplier
    float weightMultiplier = isKg ? 0.005f : 0.01f;
    // Get weight
    float weightValue = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * weightMultiplier;
    Timber.d(prefix + "weight: " + weightValue);
    scaleMeasurement.setWeight(weightValue);
    if (timestampPresent) {
        Date timestamp = parser.getDateTime();
        Timber.d(prefix + "timestamp: " + timestamp.toString());
        scaleMeasurement.setDateTime(timestamp);
    }
    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);
        }
        if (registerNewUser) {
            Timber.d(String.format(prefix + "Setting initial weight for user %s to: %s and registerNewUser to false", userID, weightValue));
            if (selectedUser.getId() == userID) {
                this.selectedUser.setInitialWeight(weightValue);
                OpenScale.getInstance().updateScaleUser(selectedUser);
            }
            registerNewUser = false;
        }
    }
    if (bmiAndHeightPresent) {
        float BMI = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
        Timber.d(prefix + "BMI: " + BMI);
        float heightInMeters = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.001f;
        Timber.d(prefix + "heightInMeters: " + heightInMeters);
    }
    Timber.d(String.format("Got weight: %s", weightValue));
    return scaleMeasurement;
}
Also used : BluetoothBytesParser(com.welie.blessed.BluetoothBytesParser) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Date(java.util.Date)

Example 20 with BluetoothBytesParser

use of com.welie.blessed.BluetoothBytesParser in project openScale by oliexdev.

the class BluetoothStandardWeightProfile method writeCurrentTime.

protected void writeCurrentTime() {
    BluetoothBytesParser parser = new BluetoothBytesParser();
    parser.setCurrentTime(Calendar.getInstance());
    writeBytes(BluetoothGattUuid.SERVICE_CURRENT_TIME, BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME, parser.getValue());
}
Also used : BluetoothBytesParser(com.welie.blessed.BluetoothBytesParser)

Aggregations

BluetoothBytesParser (com.welie.blessed.BluetoothBytesParser)22 Date (java.util.Date)3 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)2 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)2 Converters (com.health.openscale.core.utils.Converters)1 GregorianCalendar (java.util.GregorianCalendar)1