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.
(ArrayList) [00001801-0000-1000-8000-00805f9b34fb, 00001800-0000-1000-8000-00805f9b34fb, 6e400001-b5a3-f393-e0a9-e50e24dcca9e, 00001802-0000-1000-8000-00805f9b34fb, 0000d0ff-3c17-d293-8e48-14fe2e4da212, 0000180f-0000-1000-8000-00805f9b34fb, 0000180a-0000-1000-8000-00805f9b34fb, 0000180d-0000-1000-8000-00805f9b34fb, 00006287-3c17-d293-8e48-14fe2e4da212]
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:
discoverServices() - device: CB:56:00:0F:3E:99
onSearchComplete() = Device=CB:56:00:0F:3E:99 Status=0
Connected
(ArrayList) [00001801-0000-1000-8000-00805f9b34fb, 00001800-0000-1000-8000-00805f9b34fb, 6e400001-b5a3-f393-e0a9-e50e24dcca9e, 00001802-0000-1000-8000-00805f9b34fb, 0000d0ff-3c17-d293-8e48-14fe2e4da212, 0000180f-0000-1000-8000-00805f9b34fb, 0000180a-0000-1000-8000-00805f9b34fb, 0000180d-0000-1000-8000-00805f9b34fb, 00006287-3c17-d293-8e48-14fe2e4da212]
Error occurred on line: 207 (B4XMainPage)
java.lang.RuntimeException: Service not found
at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:418)
at anywheresoftware.b4a.objects.BleManager2.ReadData2(BleManager2.java:294)
at anywheresoftware.b4a.objects.BleManager2.ReadData(BleManager2.java:284)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:144)

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
Top