'Using react native ble plx to get data from smart kitchen scale. Managed to get value (It fluctuates when a weight is applied) but it is weird
I am using the react native ble plx module to connect to an escali smart kitchen scale, and am trying to get the value to show on my mobile application. I managed to get the value, but it is in a weird format. Upon using base64 to decode, I got back alt-code style values. How do I get the weight value (1,2,3,etc...) and unit (g/oz) from the value I got? I have tried comparing the output values from various weights and have managed to find some similarities and differences, however this does not help me in getting the value from it.
My Code:
manager.connectToDevice(item.id, { autoConnect: false }).then((item) => {
item.discoverAllServicesAndCharacteristics().then(() => {
item.services().then((service) => {
for (var x = 0; x < service.length; x++) {
item.characteristicsForService(service[x].uuid).then((result) => {
for (var x = 0; x < result.length; x++) {
if (result[x].isNotifiable === true) {
item.monitorCharacteristicForService(result[x].serviceUUID, result[x].uuid, async (error: BleError | null, characteristic: Characteristic | null) => {
// error handling
if (error || !characteristic) {
console.log(error);
return
}
if (characteristic) {
var raw = characteristic.value;
var decodeVal = base64.decode(raw);
console.log(`${raw}: ${decodeVal}`);
setWeight(decodeVal);
if (raw.includes('AAAA', 4)) {
setWeightUnit('g');
} else {
setWeightUnit('oz');
}
}
})
}
}
})
}
})
})
})
My output:
LOG DAAAAAAAlwA=:
LOG DAAAAAAAFwA=:
↨
LOG DPAAAAAAFwA=:
ð↨
LOG DNUEAAAAFwA=:
Õ♦↨
LOG DNwGAAAAFwA=:
Ü♠↨
LOG DB8HAAAAFwA=:
▼↨
My services and characteristics: (i just pasted the main part)
"id": 2, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 1, "serviceUUID": "00001800-0000-1000-8000-00805f9b34fb", "uuid": "00002a00-0000-1000-8000-00805f9b34fb", "value": null}
"id": 3, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 1, "serviceUUID": "00001800-0000-1000-8000-00805f9b34fb", "uuid": "00002a01-0000-1000-8000-00805f9b34fb", "value": null}
"id": 4, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": true, "isWritableWithoutResponse": false, "serviceID": 1, "serviceUUID": "00001800-0000-1000-8000-00805f9b34fb", "uuid": "00002a02-0000-1000-8000-00805f9b34fb", "value": null}
"id": 5, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": true, "isWritableWithoutResponse": false, "serviceID": 1, "serviceUUID": "00001800-0000-1000-8000-00805f9b34fb", "uuid": "00002a03-0000-1000-8000-00805f9b34fb", "value": null}
"id": 6, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 1, "serviceUUID": "00001800-0000-1000-8000-00805f9b34fb", "uuid": "00002a04-0000-1000-8000-00805f9b34fb", "value": null}
"id": 8, "isIndicatable": true, "isNotifiable": false, "isNotifying": false, "isReadable": false, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 7, "serviceUUID": "00001801-0000-1000-8000-00805f9b34fb", "uuid": "00002a05-0000-1000-8000-00805f9b34fb", "value": null}
"id": 11, "isIndicatable": true, "isNotifiable": false, "isNotifying": false, "isReadable": false, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 10, "serviceUUID": "0000780a-0000-1000-8000-00805f9b34fb", "uuid": "00008aa1-0000-1000-8000-00805f9b34fb", "value": null}
"id": 14, "isIndicatable": false, "isNotifiable": true, "isNotifying": false, "isReadable": false, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 10, "serviceUUID": "0000780a-0000-1000-8000-00805f9b34fb", "uuid": "00008aa2-0000-1000-8000-00805f9b34fb", "value": null}
"id": 16, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 10, "serviceUUID": "0000780a-0000-1000-8000-00805f9b34fb", "uuid": "00008aa0-0000-1000-8000-00805f9b34fb", "value": null}
"id": 18, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a23-0000-1000-8000-00805f9b34fb", "value": null}
"id": 19, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a24-0000-1000-8000-00805f9b34fb", "value": null}
"id": 20, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a25-0000-1000-8000-00805f9b34fb", "value": null}
"id": 21, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a26-0000-1000-8000-00805f9b34fb", "value": null}
"id": 22, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a27-0000-1000-8000-00805f9b34fb", "value": null}
"id": 23, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a28-0000-1000-8000-00805f9b34fb", "value": null}
"id": 24, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a29-0000-1000-8000-00805f9b34fb", "value": null}
"id": 25, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a2a-0000-1000-8000-00805f9b34fb", "value": null}
"id": 26, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": true, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 17, "serviceUUID": "0000180a-0000-1000-8000-00805f9b34fb", "uuid": "00002a50-0000-1000-8000-00805f9b34fb", "value": null}
Converted to hexadecimal:
(0 grams) => DAAAAAAAlwA= [0c 00 00 00 00 00 97 00]
(318 grams) => DD4BAAAAFwA= [0c 3e 01 00 00 00 17 00]
(156 grams) => DJwAAAAAFwA= [0c 9c 00 00 00 00 17 00]
(0 oz) => DQAA8AAAlwA= [0d 00 00 f0 00 00 97 00]
(11.2 oz) => DQBw8AAAFwA= [0d 00 70 f0 00 00 17 00]
(5.5 oz) => DQA38AAAFwA= [0d 00 37 f0 00 00 17 00]
Would appreciate any hints or help!
Solution 1:[1]
Whenever you have an unknown BLE protocol you need to find a pattern in the received values. Knowing the expected values is key.
Take a look at the values in grams:
(0 grams) => [0c 00 00 00 00 00 97 00]
(318 grams) => [0c 3e 01 00 00 00 17 00]
(156 grams) => [0c 9c 00 00 00 00 17 00]
The first value stays the same, the second and third change. For 0 grams it is actually 00 00 which was expected, let's take a look at 318g:
If we convert 3e 01 to decimal we receive a value of 15873, not what we expected. But sometimes devices store their values in different byte order, so let's try to reverse the order. Converting 01 3e results in the expected value of 318.
The first byte is set to 0c if grams are measured and 0d if it's in oz. So that might be the flag to tell you what's actually measured.
The value for oz measurements also makes sense: the third byte contains the value (for these small amounts, might be different if larger values are sent). For example 70 in hex is 112 in decimal, just add a decimal point (by dividing by 10) and you get the expected value of 11.2.
That's just a starting point, you might be able to figure out the other values as well
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Kotzjan |
