Android Question Getting Heart Rate from BLE Watch turning to be a difficult task.

GJREDITOR

Member
I wanted to connect to the heart rate service of a watch , but with BLE2 library, it couldn't connect so I used the modified BLE library from here:
I was able to connect to the watch and I can see the Heart rate service in the log.
However when I try to read the Heart Rate Service with
manager.ReadData(HEART_RATE_SERVICE), I get error.
I am able to get the data in "nrfconnect" android App.
Please advice what can I do to get the heart rate service. Thank you
The relevant code is give below:
Heart rate service declared in Class_Globals:
Sub Class_Globals
    Private Root As B4XView
    ................
    Private HEART_RATE_SERVICE As String = "180D"
    Private HEART_RATE_MEASUREMENT As String = "2A37"
    Private firstRead As Boolean
    Private bc As ByteConverter
End Sub

Device found code:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
'STYLE CALL is the internal name of the Watch
If Name.ToLowerCase.Contains("style") Then
    ConnectedName = Name
    manager.StopScan
    Log("connecting")
        #if B4A
    manager.Connect3(Id,True,2)
    #else if B4I
    manager.Connect(Id)
    #end if
    End If
End Sub

This is the Error sub:
Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    StateChanged
    firstRead = True
    Log(services)
    manager.ReadData(HEART_RATE_SERVICE)' Error java.lang.RuntimeException: Service not found
    at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:418)
    
End Sub

The relevant log is given below:

Is the error because of using modified BLE library? If so how to correct it? Thanks
 

GJREDITOR

Member
Try replacing with that
B4X:
Private HEART_RATE_SERVICE As String = "0000180d-0000-1000-8000-00805f9b34fb"
Thank you for pointing it out. However, I already use the following sub by Erel to change the UUID :
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

Today I changed the entire project and used the Heart Rate Module from this thread and I was able to get the heart rate from the watch after changing the code slightly :
B4X:
Sub Class_Globals
    Private manager As BleManager3
.......
End Sub

code change in Heart Rate Monitor Module:
Private Sub Manager_DeviceFound (Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)
    Log($"****************
Name: ${Name}
DeviceId: ${DeviceId}"$)
If Name.ToLowerCase.Contains("style") Then
    manager.StopScan
    Log($"Connecting to: ${DeviceId}"$)
    'manager.Connect(DeviceId)
    manager.Connect3(DeviceId,True,2)
End If
End Sub
Thanks Mariano for taking the time to check out my earlier code.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…