Java Code Examples for com.welie.blessed.BluetoothBytesParser#getDateTime()

The following examples show how to use com.welie.blessed.BluetoothBytesParser#getDateTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemperatureMeasurement.java    From blessed-android with MIT License 6 votes vote down vote up
public TemperatureMeasurement(byte[] byteArray) {
    BluetoothBytesParser parser = new BluetoothBytesParser(byteArray);

    // Parse flag byte
    final int flags = parser.getIntValue(FORMAT_UINT8);
    unit = ((flags & 0x01) > 0 ? TemperatureUnit.Fahrenheit : TemperatureUnit.Celsius);
    final boolean timestampPresent = (flags & 0x02) > 0;
    final boolean typePresent = (flags & 0x04) > 0;

    // Get temperature value
    temperatureValue = parser.getFloatValue(FORMAT_FLOAT);

    // Get timestamp
    if(timestampPresent) {
        timestamp = parser.getDateTime();
    }

    // Get temperature type
    if(typePresent) {
        int typeValue = parser.getIntValue(FORMAT_UINT8);
        type = TemperatureType.fromValue(typeValue);
    }
}
 
Example 2
Source File: BloodPressureMeasurement.java    From blessed-android with MIT License 5 votes vote down vote up
public BloodPressureMeasurement(byte[] value) {
    BluetoothBytesParser parser = new BluetoothBytesParser(value);

    // Parse the flags
    int flags = parser.getIntValue(FORMAT_UINT8);
    isMMHG = !((flags & 0x01) > 0);
    boolean timestampPresent = (flags & 0x02) > 0;
    boolean pulseRatePresent = (flags & 0x04) > 0;
    boolean userIdPresent = (flags & 0x08) > 0;
    boolean measurementStatusPresent = (flags & 0x10) > 0;

    // Get systolic, diastolic and mean arterial pressure
    systolic = parser.getFloatValue(FORMAT_SFLOAT);
    diastolic = parser.getFloatValue(FORMAT_SFLOAT);
    meanArterialPressure = parser.getFloatValue(FORMAT_SFLOAT);

    // Read timestamp
    if (timestampPresent) {
        timestamp = parser.getDateTime();
    } else {
        timestamp = Calendar.getInstance().getTime();
    }

    // Read pulse rate
    if (pulseRatePresent) {
        pulseRate = parser.getFloatValue(FORMAT_SFLOAT);
    }

    // Read userId
    if (userIdPresent) {
        userID = parser.getIntValue(FORMAT_UINT8);
    }
}
 
Example 3
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private void handleWeightMeasurement(byte[] 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;

    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;
    scaleMeasurement.setWeight(weightValue);

    if(timestampPresent) {
        Date timestamp = parser.getDateTime();
        scaleMeasurement.setDateTime(timestamp);
    }

    if(userIDPresent) {
        int userID = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT8);
        Timber.d(String.format("User id: %i", userID));
    }

    if(bmiAndHeightPresent) {
        float BMI = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
        float heightInMeters = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.001f;
    }

    Timber.d(String.format("Got weight: %s", weightValue));
    addScaleMeasurement(scaleMeasurement);
}
 
Example 4
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
@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));
    }
    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)) {
        if(value[0]==UDS_CP_RESPONSE) {
            switch (value[1]) {
                case UDS_CP_REGISTER_NEW_USER:
                    if (value[2] == UDS_CP_RESP_VALUE_SUCCESS) {
                        int userIndex = value[3];
                        Timber.d(String.format("Created user %d", userIndex));
                    } else {
                        Timber.e("ERROR: could not register new user");
                    }
                    break;
                case UDS_CP_CONSENT:
                    if (value[2] == UDS_CP_RESP_VALUE_SUCCESS) {
                        Timber.d("Success user consent");
                    } else if (value[2] == UDS_CP_RESP_USER_NOT_AUTHORIZED) {
                        Timber.e("Not authorized");
                    }
                    break;
                default:
                    Timber.e("Unhandled response");
                    break;
            }
        }
    } else {
        Timber.d(String.format("Got data: <%s>", byteInHex(value)));
    }
}
 
Example 5
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
private void handleBodyCompositionMeasurement(byte[] 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;

    ScaleMeasurement scaleMeasurement = new ScaleMeasurement();

    float bodyFatPercentage = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
    scaleMeasurement.setFat(bodyFatPercentage);

    // Read timestamp if present
    if (timestampPresent) {
        Date timestamp = parser.getDateTime();
        scaleMeasurement.setDateTime(timestamp);
    }

    // Read userID if present
    if (userIDPresent) {
        int userID = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT8);
        Timber.d(String.format("user id: %i", 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);
    }

    // Read musclePercentage if present
    if (musclePercentagePresent) {
        float musclePercentage = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
        scaleMeasurement.setMuscle(musclePercentage);
    }

    // Read muscleMass if present
    if (muscleMassPresent) {
        float muscleMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
    }

    // Read fatFreeMassPresent if present
    if (fatFreeMassPresent) {
        float fatFreeMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
    }

    // Read softleanMass if present
    if (softLeanMassPresent) {
        float softLeanMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
    }

    // Read bodyWaterMass if present
    if (bodyWaterMassPresent) {
        float bodyWaterMass = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
        scaleMeasurement.setWater(bodyWaterMass);
    }

    // Read impedance if present
    if (impedancePresent) {
        float impedance = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * 0.1f;
    }

    // Read weight if present
    if (weightPresent) {
        float weightValue = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16) * massMultiplier;
        scaleMeasurement.setWeight(weightValue);
    }

    // Read height if present
    if (heightPresent) {
        float heightValue = parser.getIntValue(BluetoothBytesParser.FORMAT_UINT16);
    }

    Timber.d(String.format("Got body composition: %s", byteInHex(value)));
    addScaleMeasurement(scaleMeasurement);
}