Android Tutorial BLE - Heart Rate Monitor

HeartRateMonitor class searches for peripherals with the heart rate service (0x180D).

Once a peripheral device is found and connected then a notification is set for the heart rate measurement characteristic (0x2A37).

The DataAvailable event is raised whenever the heart rate changes. The heart rate is parsed from the raw data and printed to the logs.

Note that this class is also compatible with B4i.

It starts searching for devices when the HeartRateMonitor class is initialized.

B4A libraries: BLE2 and ByteConverter
B4i libraries: iBLE and iRandomAccessFile
 

Attachments

  • HeartRateMonitor.bas
    1.9 KB · Views: 2,440

Ivan Lo Presti

New Member
Licensed User
Longtime User
Hi,
i just started with BLE. I am trying to figure out a way to write commands to the BLE device.
the example code only shows data read. where could I find a "send"function so i can test my device?
thanks very much.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
In the HeartRateMonitor.bas file there is the subroutine UUID.

In Android it forms the UUID by concatenating the service with other numbers

B4X:
Private Sub UUID(id As String) As String
#if B4A
    Return "0000" & id.ToLowerCase & "-0000-1000-8000-00805f9b34fb"
#else if B4I
    Return id.ToUpperCase
#End If
End Sub

Where do the other numbers, 0000XXXX-0000-1000-8000-00805f9b34fb, come from?

Are these from the UUID of a known device that is being scanning for? Or, some other?

Thanks,
Barry.
 

postasat

Active Member
Licensed User
Longtime User
Hi,
I need to extract from the Heart Rate Measurement the following data:
- RR-Interval

I found this information:
https://www.bluetooth.com/specifica...oth.characteristic.heart_rate_measurement.xml

There is a bit that control if data are present, the bit4 of the flag is about RR-Interval presence.

I found this note about RR:
If RR-Interval values are present in the Heart Rate Measurement characteristic, the Server set bit 4 of the Flags field (RR-Interval bit) to 1 and include one or more RR-Interval values in the Heart Rate Measurement characteristic. Otherwise RRInterval values are not be included and bit 4 of the Flags field is set to 0. For a 23-octet ATT_MTU and the Heart Rate Measurement Value format set to UINT8, the maximum number of RR-Interval Values that can be notified if Energy Expended is present is 8 and the maximum number of RR-Interval Values that can be notified if Energy Expended is not present is 9. For a 23-octet ATT_MTU and the Heart Rate Measurement Value format set to UINT16, when notifying the Heart Rate Measurement characteristic, the maximum number of RR-Interval values that can be contained within a single Heart Rate Measurement characteristic with Energy Expended is 7 and the maximum number of RR-Interval values that can be notified if Energy Expended is not present is 8. If more RR-Interval values are measured since the last notification than fit into one Heart Rate Measurement characteristic, then the remaining RR-Interval values should be included in the next available Heart Rate Measurement characteristic.

(the bit3 of the flag is the presence of energy expended data)

How can I do, to extract them ?
Can you help me with code or implement this function in the class ?

Thank you,
Roberto.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the GetBit sub to get the bit value: https://www.b4x.com/android/forum/threads/math.70956/#post-450781

You need to check whether the energy field is present or not, the length of the rate field (see the example code) and then find the two bytes of RR-Intercal.

ByteConverter.HexFromBytes is useful to understand the message structure.
 

Beja

Expert
Licensed User
Longtime User
HeartRateMonitor class searches for peripherals with the heart rate service (0x180D).
Hi Erel,
Thanks for this very important class.. I am very interested in this and want to incorporate it into a project., but my knowledge of these devices is next to zero.
Is there any popular device or any device known to be compatible with this class? is it a pcb module or a packaged device (in housing box).
It would be great of there is a picture of a compatible device.
Best, -B
 

Sapta

Member
Licensed User
Longtime User
HeartRateMonitor class searches for peripherals with the heart rate service (0x180D).

Once a peripheral device is found and connected then a notification is set for the heart rate measurement characteristic (0x2A37).

The DataAvailable event is raised whenever the heart rate changes. The heart rate is parsed from the raw data and printed to the logs.

Note that this class is also compatible with B4i.

It starts searching for devices when the HeartRateMonitor class is initialized.

B4A libraries: BLE2 and ByteConverter
B4i libraries: iBLE and iRandomAccessFile


how to use this sample?
 

kreativa

Member
Licensed User
Longtime User
Hi everybody,
need help!
I wrote a procedure to read heart rate monitor, and it works!
Now i need to read multiple heart rate. When manager_dataAvaible is called i can read the characteristic with the heart rate measure from all the devices but i don't know the name of the device i'm reading.

How can i do to know the device name in manager_dataavaible?

Thanks!
 

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi everybody,
need help!
I wrote a procedure to read heart rate monitor, and it works!
Now i need to read multiple heart rate. When manager_dataAvaible is called i can read the characteristic with the heart rate measure from all the devices but i don't know the name of the device i'm reading.

How can i do to know the device name in manager_dataavaible?

Thanks!



Can you share ?

Victor
 
Top