How to interpret data from BLE heartrate monitor

Hi, trying to set up an app to read heart rate data from my heart rate strap.

I have tried numerous examples like the ones found in HRM heart rate monitor and others, but in all of them bluetooth fails to find my device.

So, I built my own from scratch following parts of different examples and am able to find my device and (crudely) list the services and characteristics.

However, my problem is retrieving data from the device. I have tried registering to receive bytes, floats, integers, strings and shorts. Strings work but i get weird characters, and only using shorts I get what seems to be 16 bit integer values as expected from that.

The data I get is as follows, each line corresponds to a "shorts received" event, you can see sometimes the list is one item long, sometimes two:
13842, 1144,
13842, 1171,
13842, 1119,
13826,
13842, 1136,
13586, 1144,
13842, 1086,
13842, 1113,
13842, 1135,
13842, 1091,
13842, 1133,
13842, 1153,
13842, 1111,
13842, 1143,
13826,
13842, 1149,
13842, 1087,
13842, 1133,
13842, 1137,
13842, 1079

My problem now is to extract HR data from there, I have looked in the Bluetooth heart rate services PDF (attached) but it doesn't seem to correspond, only the first number, "1" indicating it is in 16 bit format, the rest doesn't, ie: the second bit should be 1 or 0, and in "13842" it's a 3.
Plus, according to the PDF it can change from 16 to 8 bit during transmission or from one device to another.
Also looked into some of the examples, but they use the "bytesreceived" or "integersreceived" which do nothing for me. I attach my .aia in case someone wants to have a look (it needs some improvement here and there, but it works :wink: )

So my questions are: how to differentiate if I'm receiving 8 or 16 bit integers, etc, and how to translate the data above to heartrate data?, if someone has experience with this or can interpret from what the PDF says according to the data I pasted above.

HRS_SPEC_V10.pdf (348.9 KB)

BLE_HR.aia (196.3 KB)

Did you search? Use the Magnifying Glass icon.

You might get some help from these community discussions Search results for 'heart rate' - MIT App Inventor Community and see the solutions others in similar situations experienced.

Hi, yes, I did search, both here and in other forums, that's were I got all the examples I have been trying, but many don't work for me and many posts don't get anywhere, and also tried links to the official Bluetooth docs which are broken, like this one: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml

I did find this tutorial late last night, it's for IOs, but it does mention how to interpret some of the binary data values, so I'll try and get some info from there today.

Meanwhile, if anyone has any insight, I'll really appreciate it.

So, I modified my original app to receive ANY type of data (bytes, floats, shorts, integers and strings, all of them signed/unsigned or whatever options I had), and it would categorize the data according to its "received format", so I attach the received list below:

With shorts unsigned and strings as UTF16-true:
shorts received... [15122, 1065]
strings received... ["㬒Щ"]
shorts received... [15122, 1078, 523]
strings received... ["㬒жȋ"]
shorts received... [15122, 1158]
strings received... ["㬒҆"]
shorts received... [15122, 1060]
strings received... ["㬒Ф"]
shorts received... [15122, 1046]
strings received... ["㬒Ж"]
shorts received... [15122, 506]
strings received... ["㬒Ǻ"]
shorts received... [15122, 1141]
strings received... ["㬒ѵ"]
shorts received... [15122, 1033]
strings received... ["㬒Љ"]
shorts received... [15122, 1035]
strings received... ["㬒Ћ"]
shorts received... [15122, 1022]
strings received... ["㬒Ͼ"]

With shorts signed and strings as UTF16-false:
shorts received... [14610, 1027]
strings received... ["㤒Ѓ"]
shorts received... [14610, 1014]
strings received... ["㤒϶"]
shorts received... [14866, 1041]
strings received... ["㨒Б"]
shorts received... [14866, 1037]
strings received... ["㨒Ѝ"]
shorts received... [14866, 1027]
strings received... ["㨒Ѓ"]
shorts received... [14866, 1044]
strings received... ["㨒Д"]
shorts received... [15122, 1064]
strings received... ["㬒Ш"]
shorts received... [15122, 192, 109, 486]
strings received... ["㬒ÀmǦ"]
shorts received... [15120, 478, 184]
strings received... ["㬐Ǟ¸"]
shorts received... [15104]
strings received... ["㬀"]

You can see I am only receiving shorts and strings, but for every short is followed by a string, so I guess its the same data, just interpreted differently. The signed/unsigned should not make any difference since the expected numbers are low (less than 220) and are all positive.

According to Bluetooth docs, it's logical that integers or floats are not received, since they don't correspond to the standard data format for heart rate transmission (but I wanted to try hehe).

Finally, I came across this So, finally came along this: Reading BLE service in Appinventor - #41 by Gordon_Moore, and sort of have come to something that works.

You can see in my list that most data comes in a list, and the first item on the list is always around 13000-15000, and in some cases the list only contains one item, and that item is of such magnitude. If I divide that number by 256 as stated on the link above, I get between 50 and 60 heart beats per minute, which is slightly higher than my resting hear rate since I'm sitting down but still doing some work. So that seems to be ok and I understand the logic behind it.

Now, for the problem of being able to tell whether to use bytes or shorts, the link explains but I think there's a bit of confusion as to which bit to use to tell them apart, but anyway, if the sensor decided to send bytes, since numbers in this case are under 255, there's no difference between receiving 8 or 16 bits, right?

Anyway, I'm open to thoughts or comments, and attach my last version which can be used to connect and receive all kind of data to be interpreted (you will need to activate the blocks corresponding to floats and integers though :wink: )

BLE_connect_v3.aia (200.8 KB)