Android Question Extract RR-interval from BLE heartbelt data

postasat

Active Member
Licensed User
Longtime User
Hi,
I need an example to extract RR-interval from BLE data.

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)

In my app I use this code to extract bpm data from a Polar BLE heartbelt.

B4X:
Sub BLEDataAvailable (Service As String, Characteristics As Map)
   
    Dim Conv As ByteConverter
    Dim keyread As String
    Dim bpm_temp As String

    For i=0 To Characteristics.size-1
        keyread = Characteristics.GetKeyAt(i)

        'MEASURE
        If keyread = "00002a37-0000-1000-8000-00805f9b34fb" Then
            testoconvhrm = Conv.HexFromBytes(Characteristics.GetValueAt(i))
            If testoconvhrm = "" Then
                Log("NO BEAT")
            Else   
                byte2 = testoconvhrm.CharAt(2)
                byte3 = testoconvhrm.CharAt(3)
                bpm_temp = byte2 & byte3
                beat = Bit.ParseInt(bpm_temp, 16)
            End If
        End If   
    Next

End Sub

Please somebody can help me to modify this code to check presence and extract RR-interval data?

Thank you.
 

postasat

Active Member
Licensed User
Longtime User
Hi,
I tried the example, I can see and use the heart rate data but my skill is not enough to modify and extract what I need.
I hope somebody can help me to modify HeartRateMonitor class to extract RR-interval from BLE data.
Thank you.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with:
B4X:
Dim key As String = "..."
If Characteristics.ContainsKey(key) Then
 Dim data() As Byte = Characteristics.Get(key)
 If GetBit(data(0), 4) = 1 Then
   Dim raf As RandomAccessFile
   raf.Initialize3(data, True) 'also try with False as I don't know which endianess is used
   Dim HRMValueSize, EnergyExpended As Int
   If GetBit(data(0), 0) = 0 Then HRMValueSize = 1 Else HRMValueSize  = 2
   If GetBit(data(0), 3) = 1 Then EnergyExpended = 2
   Dim rr As Short = raf.ReadShort(1 + HRMValueSize + EnergyExpended) 
   Log(rr)
 End If
End If

GetBit: https://www.b4x.com/android/forum/threads/math.70956/#post-450781
 
Upvote 0

postasat

Active Member
Licensed User
Longtime User
Hi Erle,

I put the following code inside BLE Dataavailable, when I receive the first hrm data, I have this error and it stop.

B4X:
Dim key As String = "00002a37-0000-1000-8000-00805f9b34fb"
            If Characteristics.ContainsKey(key) Then
             Dim data() As Byte = Characteristics.Get(key)
             If GetBit(data(0), 4) = 1 Then
               Dim raf As RandomAccessFile
               raf.Initialize3(data, True) 'also try with False as I don't know which endianess is used
               Dim HRMValueSize, EnergyExpended As Int
               If GetBit(data(0), 0) = 0 Then HRMValueSize = 1 Else HRMValueSize  = 2
               If GetBit(data(0), 3) = 1 Then EnergyExpended = 2
               Dim rr As Short = raf.ReadShort(1 + HRMValueSize + EnergyExpended)
               Log(rr)
             End If
            End If

An error occurred:
(Line: 1918) If GetBit(data(0), 4) = 1 Then
java.lang.RuntimeException: Cannot parse: 1 as boolean

The Data() byte received, before call Getbit, is the following:
Data
0----- 6 (0x6)
1----- 78 (0x4E)

I don't know where is the problem.
Can you help me ?

Thank you.
 
Upvote 0
Top