com.welie.blessed.BluetoothBytesParser Java Examples

The following examples show how to use com.welie.blessed.BluetoothBytesParser. 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: HeartRateMeasurement.java    From blessed-android with MIT License 5 votes vote down vote up
public HeartRateMeasurement(byte[] value) {
    BluetoothBytesParser parser = new BluetoothBytesParser(value);

    // Parse the flags
    int flags = parser.getIntValue(FORMAT_UINT8);
    final int unit = flags & 0x01;
    final int sensorContactStatus = (flags & 0x06) >> 1;
    final boolean energyExpenditurePresent = (flags & 0x08) > 0;
    final boolean rrIntervalPresent = (flags & 0x10) > 0;

    // Parse heart rate
    this.pulse = (unit == 0) ? parser.getIntValue(FORMAT_UINT8) : parser.getIntValue(FORMAT_UINT16);
}
 
Example #4
Source File: PUtil.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public Object parseBytes(byte[] bytes, String type) {
    BluetoothBytesParser parser = new BluetoothBytesParser(bytes);

    switch (type) {
        case "uint8":
            return parser.getIntValue(BluetoothBytesParser.FORMAT_UINT8);
        case "string":
            return parser.getStringValue(0);
        default:
            return null;
    }
}
 
Example #5
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 #6
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private 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());
}
 
Example #7
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private void setUser(int userIndex, int consentCode) {
    BluetoothBytesParser parser = new BluetoothBytesParser(new byte[]{0,0,0,0});
    parser.setIntValue(UDS_CP_CONSENT,FORMAT_UINT8,0);
    parser.setIntValue(userIndex, FORMAT_UINT8,1);
    parser.setIntValue(consentCode, FORMAT_UINT16,2);
    Timber.d(String.format("setUser userIndex: %d, consentCode: %d", userIndex, consentCode));
    writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT, parser.getValue());
}
 
Example #8
Source File: BluetoothHandler.java    From blessed-android with MIT License 4 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothPeripheral peripheral) {
    Timber.i("discovered services");

    // Request a new connection priority
    peripheral.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);

    // Read manufacturer and model number from the Device Information Service
    if(peripheral.getService(DIS_SERVICE_UUID) != null) {
        peripheral.readCharacteristic(peripheral.getCharacteristic(DIS_SERVICE_UUID, MANUFACTURER_NAME_CHARACTERISTIC_UUID));
        peripheral.readCharacteristic(peripheral.getCharacteristic(DIS_SERVICE_UUID, MODEL_NUMBER_CHARACTERISTIC_UUID));
    }

    // Turn on notifications for Current Time Service
    if(peripheral.getService(CTS_SERVICE_UUID) != null) {
        BluetoothGattCharacteristic currentTimeCharacteristic = peripheral.getCharacteristic(CTS_SERVICE_UUID, CURRENT_TIME_CHARACTERISTIC_UUID);
        peripheral.setNotify(currentTimeCharacteristic, true);

        // If it has the write property we write the current time
        if((currentTimeCharacteristic.getProperties() & PROPERTY_WRITE) > 0) {
            // Write the current time unless it is an Omron device
            if(!(peripheral.getName().contains("BLEsmart_"))) {
                BluetoothBytesParser parser = new BluetoothBytesParser();
                parser.setCurrentTime(Calendar.getInstance());
                peripheral.writeCharacteristic(currentTimeCharacteristic, parser.getValue(), WRITE_TYPE_DEFAULT);
            }
        }
    }

    // Turn on notifications for Battery Service
    if(peripheral.getService(BTS_SERVICE_UUID) != null) {
        peripheral.setNotify(peripheral.getCharacteristic(BTS_SERVICE_UUID, BATTERY_LEVEL_CHARACTERISTIC_UUID), true);
    }

    // Turn on notifications for Blood Pressure Service
    if(peripheral.getService(BLP_SERVICE_UUID) != null) {
        peripheral.setNotify(peripheral.getCharacteristic(BLP_SERVICE_UUID, BLOOD_PRESSURE_MEASUREMENT_CHARACTERISTIC_UUID), true);
    }

    // Turn on notification for Health Thermometer Service
    if(peripheral.getService(HTS_SERVICE_UUID) != null) {
        peripheral.setNotify(peripheral.getCharacteristic(HTS_SERVICE_UUID, TEMPERATURE_MEASUREMENT_CHARACTERISTIC_UUID), true);
    }

    // Turn on notification for Heart Rate  Service
    if(peripheral.getService(HRS_SERVICE_UUID) != null) {
        peripheral.setNotify(peripheral.getCharacteristic(HRS_SERVICE_UUID, HEARTRATE_MEASUREMENT_CHARACTERISTIC_UUID), true);
    }
}
 
Example #9
Source File: BluetoothStandardWeightProfile.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean onNextStep(int stepNr) {

    switch (stepNr) {
        case 0:
            // Read manufacturer and model number from the Device Information Service
            readBytes(BluetoothGattUuid.SERVICE_DEVICE_INFORMATION, BluetoothGattUuid.CHARACTERISTIC_MANUFACTURER_NAME_STRING);
            readBytes(BluetoothGattUuid.SERVICE_DEVICE_INFORMATION, BluetoothGattUuid.CHARACTERISTIC_MODEL_NUMBER_STRING);
            break;
        case 1:
            // Write the current time
            BluetoothBytesParser parser = new BluetoothBytesParser();
            parser.setCurrentTime(Calendar.getInstance());
            writeBytes(BluetoothGattUuid.SERVICE_CURRENT_TIME, BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME, parser.getValue());
            break;
        case 2:
            // Turn on notification for Weight Service
            setNotificationOn(BluetoothGattUuid.SERVICE_WEIGHT_SCALE, BluetoothGattUuid.CHARACTERISTIC_WEIGHT_MEASUREMENT);
            break;
        case 3:
            // Turn on notification for Body Composition Service
            setNotificationOn(BluetoothGattUuid.SERVICE_BODY_COMPOSITION, BluetoothGattUuid.CHARACTERISTIC_BODY_COMPOSITION_MEASUREMENT);
            break;
        case 4:
            // Turn on notification for User Data Service
            setNotificationOn(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_CHANGE_INCREMENT);
            setNotificationOn(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT);
            break;
        case 5:
            // Turn on notifications for Battery Service
            setNotificationOn(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL);
            break;
        case 6:
            final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
            registerUser(CURRENT_USER_CONSENT);
            setUser(selectedUser.getId(), CURRENT_USER_CONSENT);
            break;
        default:
            return false;
    }

    return true;
}
 
Example #10
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 #11
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);
}
 
Example #12
Source File: BluetoothSoehnle.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean onNextStep(int stepNr) {
    switch (stepNr) {
        case 0:
            List<ScaleUser> openScaleUserList = OpenScale.getInstance().getScaleUserList();

            int index = -1;

            // check if an openScale user is stored as a Soehnle user otherwise do a factory reset
            for (ScaleUser openScaleUser : openScaleUserList) {
                index = getSoehnleUserIndex(openScaleUser.getId());
                if (index != -1) {
                    break;
                }
            }

            if (index == -1) {
                invokeScaleFactoryReset();
            }
            break;
        case 1:
            setNotificationOn(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL);
            readBytes(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL);
            break;
        case 2:
            // Write the current time
            BluetoothBytesParser parser = new BluetoothBytesParser();
            parser.setCurrentTime(Calendar.getInstance());
            writeBytes(BluetoothGattUuid.SERVICE_CURRENT_TIME, BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME, parser.getValue());
            break;
        case 3:
            // Turn on notification for User Data Service
            setNotificationOn(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT);
            break;
        case 4:
            int openScaleUserId = OpenScale.getInstance().getSelectedScaleUserId();
            int soehnleUserIndex = getSoehnleUserIndex(openScaleUserId);

            if (soehnleUserIndex == -1) {
                // create new user
                Timber.d("create new Soehnle scale user");
                writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT, new byte[]{(byte)0x01, (byte)0x00, (byte)0x00});
            } else {
                // select user
                Timber.d("select Soehnle scale user with index " + soehnleUserIndex);
                writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT, new byte[]{(byte) 0x02, (byte) soehnleUserIndex, (byte) 0x00, (byte) 0x00});
            }
            break;
        case 5:
            // set age
            writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_AGE, new byte[]{(byte)OpenScale.getInstance().getSelectedScaleUser().getAge()});
            break;
        case 6:
            // set gender
            writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_GENDER, new byte[]{OpenScale.getInstance().getSelectedScaleUser().getGender().isMale() ? (byte)0x00 : (byte)0x01});
            break;
        case 7:
            // set height
            writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_HEIGHT, Converters.toInt16Le((int)OpenScale.getInstance().getSelectedScaleUser().getBodyHeight()));
            break;
        case 8:
            setNotificationOn(WEIGHT_CUSTOM_SERVICE, WEIGHT_CUSTOM_A_CHARACTERISTIC);
            setNotificationOn(WEIGHT_CUSTOM_SERVICE, WEIGHT_CUSTOM_B_CHARACTERISTIC);
            //writeBytes(WEIGHT_CUSTOM_SERVICE, WEIGHT_CUSTOM_CMD_CHARACTERISTIC, new byte[] {(byte)0x0c, (byte)0xff});
            break;
        case 9:
            for (int i=1; i<8; i++) {
                // get history data for soehnle user index i
                writeBytes(WEIGHT_CUSTOM_SERVICE, WEIGHT_CUSTOM_CMD_CHARACTERISTIC, new byte[]{(byte) 0x09, (byte) i});
            }
            break;
        default:
            return false;
    }

    return true;
}