Decode BLE Temperature characteristics value to Celsius

103 views Asked by At

I am using a BLE device to read temperature and get it on my system, but I am stuck on how to parse the encoded value returning from temperature characteristics value.

for eg: I am getting BmsBAP/aBwECAR8AAA== in response from the BLE device, and I am supposed to convert it to exactly 36.3 degree Celsius. some more readings are:

Characteristics value: BmsBAP/aBwECAR8AAA== Device Reading: 36.3 C

Characteristics value: BmwBAP/aBwECASEAAA== Device Reading: 36.4 C

Characteristics value: BmYBAP/aBwECAR0AAA== Device Reading: 35.8 C

BLE device used: FORA IR20 Infrared Thermometer

here is a snippet of code which I tried: https://stackblitz.com/edit/node-92jt2g?file=index.ts

So, need help to find out the correlation between the required temperature and the encoded value being sent from the device.

2

There are 2 answers

0
emandt On BEST ANSWER

"BmYBAP/aBwECAR0AAA==" is encoded using Base64 and the hex rappresentation of decoded data is

  • 0x06 0x66 0x01 0x00 0x07 0x01 0x02 0x01 0x1D 0x00 0x00

You're saying that it stays for "35.8°C" and usually raw data is stored without decimal, so it became "358". 358 could be rapresented as

  • 0x166

As you can see "1", "6" and "6" is present in the initial bytes in "0x66" and "0x01" so you can read/reorder initial bytes like this:

  1. "0x06" seems always the same for all and I don't know what it could be. Maybe the measument unit ID
  2. 0x01 + 0x66 = 0x166 which is 358 = 35.8°C

If you do same procedure for the others you will get:

  • "06 6C 01 00 07 01 02 01 21 00 00" for "36.4°C" = 0x01 + 0x6C = 0x16C = 364
  • "06 6B 01 00 07 01 02 01 1F 00 00" for "36.3°C" = 0x01 + 0x6B = 0x16B = 363
0
Abdullah Javed On

By Using ChatGpt: Maybe it will help.

It looks like there are a couple of issues in your code:

  1. The values in vals seem to be Base64-encoded strings, but your base64ToArrayBuffer function is attempting to use the atob function, which is designed for decoding Base64-encoded strings to binary data. Since your values are already Base64-encoded, you should directly decode them using Base64 decoding. Here's the corrected base64ToArrayBuffer function:
function base64ToArrayBuffer(base64) {
  var binaryString = window.atob(base64);
  var bytes = new Uint8Array(binaryString.length);
  for (var i = 0; i < binaryString.length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
  }
  return bytes.buffer;
}
  1. In the loop where you're trying to extract the value using DataView, you are using getUint8(1), but this may not be correct based on the actual format of your data. If the temperature is represented as a floating-point number, you might need to use getFloat32 or getFloat64 depending on the precision. Here's an example assuming the temperature is a 32-bit floating-point number:
vals.forEach((val) => {
  const bs64ArrBuff = base64ToArrayBuffer(val);
  const view = new DataView(bs64ArrBuff);
  
  // Assuming the temperature is a 32-bit floating-point number (4 bytes)
  const temperature = view.getFloat32(0, true); // true for little-endian, adjust based on your device's endianness
  console.log(val, '==>>', temperature);
});

Please adapt the code based on the actual data format used by your BLE device. If the temperature is stored differently (e.g., as an integer or in a different byte position), you may need to adjust the getUint8 or getFloat32 accordingly. Check the device documentation for the correct interpretation of the data.