Android Question Newbie issue with java.lang.RuntimeException: Service not found working with BLE

tseyfarth

Member
Licensed User
Longtime User
Hello all,

Can anyone tell me what this error means and how to fix it?

Working with the BLEExample, slightly modified, I get the error below every time I try to run the following code:

B4X:
Public Sub SendMessage
    'Log( "SendMessge " &     msg(0) )

    Log("Connected = " & connected )
    If Not(connected) Then Return

    Dim s As String = "Hello!"
    Dim msg() As Byte
    msg = ByteConvert.StringToBytes(s, "utf8")
    
    '  34:81:F4:31:62:61
    Log("The value of S = " & S )
    manager.WriteData(serviceId, charId, msg)   <<<<====  Errors here...
End Sub



Error occurred on line: 424 (B4XMainPage)
java.lang.RuntimeException: Service not found
at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:418)
at anywheresoftware.b4a.objects.BleManager2.WriteData(BleManager2.java:371)
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:146)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.b4xmainpage._sendmessage(b4xmainpage.java:669)
at b4a.example.b4xmainpage._manager_dataavailable(b4xmainpage.java:660)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)


Any help would be appreciated!
Tim
 

emexes

Expert
Licensed User
Hmmm.... Any other ideas?

Hey, here's one: try doing a .ReadData on that service first, before the write.

Perhaps that's when BLE2 or Android collates its list of characteristics for the service.

And until that's done, BLE2 or Android is telling us that we're writing to a characteristic that it doesn't know about because it can't find the characteristic in its (currently empty) list.
 
Upvote 0

emexes

Expert
Licensed User
I don't know if this is true for all BLE devices, but for the two I just went through here: every WRITEable characteristic is also READable.

= maybe characteristics can't be WRITE-only, always have to be readable too, in which case .ReadData is effectively listing all characteristics, and maybe that list has to be created first, before attempting to address individual characteristics by their id.
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
OK! We've reach Nirvana!

This is what worked
B4X:
    Dim s As String = "Hello!"
    Dim msg() As Byte
    msg = ByteConvert.StringToBytes(s, "utf8")

    Service = "49535343-fe7d-4ae5-8fa9-9fafd205e455"  '    UUID = 0x2902
    characteristicId ="49535343-1e4d-4bd9-ba61-23c647249616"  - got this from nRF

    manager.WriteData(Service, characteristicId, msg)


On the PC end, got Hello!....

Very very cool - thank you Emexes and Erel

Tim
 
Upvote 0

emexes

Expert
Licensed User
B4X:
Service = "49535343-fe7d-4ae5-8fa9-9fafd205e455"  '    UUID = 0x2902
characteristicId ="49535343-1e4d-4bd9-ba61-23c647249616"

From which orifice did you pluck those magic numbers? How?? Wtf???
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
From which orifice did you pluck those magic numbers? How?? Wtf???

LOL... thanks for the chuckle!
characteristicId ="49535343-1e4d-4bd9-ba61-23c647249616" - got this from nRF

from nRF... idk what it means or why it works, but it does! Eventually I am sure I'll find out. Spent the morning reading the docs, but never found where these magic strings are.

Tim
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
That Characteristic also shows up when reading from the device on my app too. There are 3 under the Service ID. So you were correct the other day when you wrote that it could be read from the device. Just didn't know how exactly or *what* to look for.

Tim
 
Upvote 0

emexes

Expert
Licensed User
Spent the morning reading the docs, but never found where these magic strings are.

Looks like somebody's already been down this path:

I would very much like to establish something here and I have just one question.

1. Has anyone got a working app using the transparent UART UUIDs?
...
It uses these UUIDs:
Service
49535343-fe7d-4ae5-8fa9-9fafd205e455
Read
49535343-1e4d-4bd9-ba61-23c647249616
Write
49535343-8841-43f4-a8d4-scbe34729bb3

but we didn't mutter the correct wizard search incantation.
 
Upvote 0

emexes

Expert
Licensed User
Are you going to be receiving characters from the other end of the serial connection too? (as well as sending)

If so, the BLE receive characteristic usually needs a .SetNotify so that the received data can be pushed to your app, rather than your app having to constantly poll the device checking for it.

I think the pushed received data just ends up in the usual manager_DataAvailable callback, same as if you'd done a .ReadData
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
Are you going to be receiving characters from the other end of the serial connection too? (as well as sending)

If so, the BLE receive characteristic usually needs a .SetNotify so that the received data can be pushed to your app, rather than your app having to constantly poll the device checking for it.

I think the pushed received data just ends up in the usual manager_DataAvailable callback, same as if you'd done a .ReadData

Good to know. I will try that tomorrow...

Tim
 
Upvote 0
Top