How to encode Floats in BLE advertisements ? #2237
Replies: 5 comments
-
Posted at 2017-06-20 by @gfwilliams I think what you're doing is right (you can also use DataView - which could be a little easier). The issue is with the EspruinoHub's decoding. It's doing something pretty simple right now - 8 bit is interpreted as-is, and 16 bit is the temperature x100. I'm not sure if it's spec compliant but it is what the nRF Connect app seems to do. https://github.com/espruino/EspruinoHub/blob/master/lib/attributes.js#L32 I'm not 100% sure the docs you found are for the UUID 0x1809 that's being used though. They seem to be for 0x2A1C. Having said that there don't appear to be any docs for the 0x1809 UUID when used in advertising (in fact it's almost certainly non-standard). ... so I'd be tempted to add a decoder in EspruinoHub for the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-06-20 by Thinkscape
GATT docs list that as:
It matches your example in docs. From what I can tell
Hmm... it seems that you're bitshifting the second value ( How would you encode a 16 bit float to this that would work on puck (i'm assuming its BE)?
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-06-20 by @gfwilliams The bit shift is just a way to turn the two 8 bit values into one 8 bit one. You're doing basically the right thing but
The The other option you have is
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-06-22 by Thinkscape Thanks mate! Works like a charm. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-06-22 by @gfwilliams Thanks! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-06-20 by Thinkscape
Hey there!
I'm using
NRF.setAdvertising()
to advertise temperature readings from Puck.js.The examples in docs sadly only mention byte arrays, integers and string fragments as supported data:
What I'm trying to do is advertise the floating point temperature without rounding. Puck currently reports temperature with
0.25
accuracy, but I'm struggling with how to encode it in a way, that would make EspruinoHub understand it.The official BLE GATT spec for thermometer actually supports extra flags in the first byte and temp. reading (default is Celsius) as a Float32.
The node on puck doesn't sadly provide
Buffer
so I tried encoding withTypedArrays
like so:The resulting array is something like:
[0, 0, 0, 236, 65]
for temperature of around 21 deg, or[0, 0, 236, 65]
if you don't include the "flags" byte and just have BE 32-bit float.However, when I receive it in EspruinoHUB, the debug shows me:
It seems to struggle to decode those Float32 byte arrays.
When I sent something like
[236, 65]
(without the GATT flags and trimmed to just 2 bytes) then it shows me:I have no idea how it got
168.75
from those bytes.Any idea what EspruinoHub/Noble/bleno expect as a format for floats?
Have anyone succeeded in encoding floats for BLE Adv. ?
Beta Was this translation helpful? Give feedback.
All reactions