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

The following examples show how to use com.welie.blessed.BluetoothBytesParser#getStringValue() . 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: 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 2
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)));
    }
}