B4J Question Bleak - Bluetooth BLE - Heart Rate (Solved)

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi Team, I'm testing the Bleak - Bluetooth BLE example with a sensor, SDK here
the code below reads the data, but the output is incorrect, no idea how to do in B4J
B4X:
[CODE=b4x]
24:AC:AC:01:41:03, Name=, Services=[0000180d-0000-1000-8000-00805f9b34fb, 0000feee-0000-1000-8000-00805f9b34fb], ServiceData={}
24:AC:AC:01:41:03, Name=Polar H10 0141033A, Services=[0000180d-0000-1000-8000-00805f9b34fb, 0000feee-0000-1000-8000-00805f9b34fb], ServiceData={}
adding 24:AC:AC:01:41:03
Connecting to: 24:AC:AC:01:41:03
connected!
Service: 00001800-0000-1000-8000-00805f9b34fb
Service: 00001801-0000-1000-8000-00805f9b34fb
Service: 0000180d-0000-1000-8000-00805f9b34fb
Service: 0000180a-0000-1000-8000-00805f9b34fb
Service: 0000180f-0000-1000-8000-00805f9b34fb
Service: 6217ff4b-fb31-1140-ad5a-a45545d7ecf3
Service: fb005c80-02e7-f387-1cad-8acd2d8df0c8
Service: 0000feee-0000-1000-8000-00805f9b34fb
Notification
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: T�
Heart rate: 21520
Parsed Heart Rate: 21520
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: U��
Heart rate: 21776
Parsed Heart Rate: 21776
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: U�
Heart rate: 21776
Parsed Heart Rate: 21776
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: T
Heart rate: 21520
Parsed Heart Rate: 21520
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: T��
Heart rate: 21520
Parsed Heart Rate: 21520
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: S�
Heart rate: 21264
Parsed Heart Rate: 21264
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: S
Heart rate: 21264
Parsed Heart Rate: 21264


B4X:
Private Sub BLE_CharNotify (Notification As BleakNotification)
    Log($"Notification received: ${Notification.CharacteristicUUID}"$)
    Log("Data: " & BytesToString(Notification.Value, 0, Notification.Value.Length, "ascii"))
    Dim heartRate As Int = ParseHeartRate(Notification.Value)
    Log("Parsed Heart Rate: " & heartRate)
    For Each cp As ClientPage In Clients
        If cp.mClient.mDevice.DeviceId = Notification.ClientUUID Then
            cp.NotificationEvent(Notification)
        End If
    Next
End Sub
Private Sub ParseHeartRate(Value() As Byte) As Int
    If Value.Length >= 2 Then
        Dim heartRate As Int
        heartRate = Bit.Or(Bit.ShiftLeft(Value(1), 8), Value(0))
        Log("Heart rate: " & heartRate)
        Return heartRate
    End If
    Return 0
End Sub

thanks in advance
 

f0raster0

Well-Known Member
Licensed User
Longtime User
What is the output of:
B4X:
Dim bc As ByteConverter
Log(bc.HexFromBytes(Value))
Parsed Heart Rate: 20496
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: P/
Heart rate: 20496
10502F03
Parsed Heart Rate: 20496
Notification received: 00002a37-0000-1000-8000-00805f9b34fb
Data: O
Heart rate: 20240
104F14031C03
Parsed Heart Rate: 20240
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tried it with the code from: https://www.b4x.com/android/forum/threads/ble-heart-rate-monitor.64102/#content

Looks fine:
B4X:
Dim b() As Byte = bc.HexToBytes("104F14031C03")
Dim hrLength As Int = 1 + Bit.UnsignedShiftRight(b(0), 7)
Dim rate As Int
If hrLength = 1 Then
    rate = Bit.And(0xff, b(1))
Else
    Dim r() As Byte = Array As Byte(b(1), b(2))
    rate = bc.IntsFromBytes(r)(0)
End If
Log(rate)
Output is 79.
 
Upvote 0
Solution

f0raster0

Well-Known Member
Licensed User
Longtime User
Tried it with the code from: https://www.b4x.com/android/forum/threads/ble-heart-rate-monitor.64102/#content

Looks fine:
B4X:
Dim b() As Byte = bc.HexToBytes("104F14031C03")
Dim hrLength As Int = 1 + Bit.UnsignedShiftRight(b(0), 7)
Dim rate As Int
If hrLength = 1 Then
    rate = Bit.And(0xff, b(1))
Else
    Dim r() As Byte = Array As Byte(b(1), b(2))
    rate = bc.IntsFromBytes(r)(0)
End If
Log(rate)
Output is 79.
Yes, I use that for iOS and Android and had no luck with B4J yesterday. Today, I tried again, and it worked! o_O
 
Upvote 0
Top