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
 

tseyfarth

Member
Licensed User
Longtime User
Thank you Erel.
Gotten past that using this Service UUID - there are 4 of them, tried them all...
B4X:
    Dim Service As String
    Service = "00001800-0000-1000-8000-00805f9b34fb"
    'Service = "0000180a-0000-1000-8000-00805f9b34fb"
    'Service = "49535343-5d82-6099-9348-7aac4d5fbc51"
    'Service = "49535343-c9d0-cc83-a44a-6fe238d06d33"
    'Service = "49535343-fe7d-4ae5-8fa9-9fafd205e455"
    
    manager.WriteData(Service, characteristicId, msg)

But now says
java.lang.RuntimeException: Characterisic not found: 0000ffe1-0000-1000-8000-00805f9b34fb

How to find out what the Characteristic needs to be?
Tim
 
Upvote 0

emexes

Expert
Licensed User
Different services have different characteristics.

.ReadData returns a list of READ characteristics.

The WRITE characteristics *might* be in the advertising data. I've always gotten them via nRF Connect (and tested them first using that app too).
 
Upvote 0

emexes

Expert
Licensed User
Yes, I can read nRF Connect app. Cannot figure out how to write using nRF...

Click the upload arrow to the right of the characteristic's name.

https://www.b4x.com/android/forum/t...-pre-ble2-problem-solving.145070/#post-919566

Unless it's not there 🤔 in which case, what are the characteristic's properties? Is it definitely writeable?

Looking at that screen reminded me of another common cause of derailment: converting from the short id to the full UUID. I have a bad feeling it might even be case-sensitive too.

Anyway, once you can write to the characteristic via nRF Connect - and you have some characters arriving in Coolterm? - then we'll worry about clearing the next hurdle.
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
I used these to connect with nRF
Service = "49535343-fe7d-4ae5-8fa9-9fafd205e455"
characteristicId = "0x2902"
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
On my learning app, I get this message when trying to write:

java.lang.RuntimeException: Characterisic not found: 0x2902


B4X:
Service = "49535343-fe7d-4ae5-8fa9-9fafd205e455" 
characteristicId = "0x2902"
manager.WriteData(Service, characteristicId, msg)


I think I have the Characteristic in the wrong format. Am I correct? If so what would it be?
Tim
 
Upvote 0

emexes

Expert
Licensed User
I think I have the Characteristic in the wrong format. Am I correct? If so what would it be?
OK, how to make it long?

I just found this code buried at the bottom of the BLE Example B4XMainPage.bas

B4X:
'utility to convert short UUIDs to long format on Android
Private Sub UUID(id As String) As String 'ignore
#if B4A
    Return "0000" & id.ToLowerCase & "-0000-1000-8000-00805f9b34fb"
#else if B4I
    Return id.ToUpperCase
#End If
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
I just found this code buried at the bottom of the BLE Example B4XMainPage.bas

which tallies up with:

JSON:
22314        -59    FF:FF:00:0B:76:36    "iTAG            "
22321    connecting to FF:FF:00:0B:76:36
Discovering services.
68139    Connected
4
0    00001800-0000-1000-8000-00805f9b34fb
1    0000180f-0000-1000-8000-00805f9b34fb
2    00001802-0000-1000-8000-00805f9b34fb
3    0000ffe0-0000-1000-8000-00805f9b34fb

corresponding to:

1672185983240.png
 
Upvote 0

tseyfarth

Member
Licensed User
Longtime User
Well, it appeared to work once, but not again. When it did work, nothing was on the Term Emulator on the PC

My Characteristic appears to be in the correct format - same as yours:
00002902-0000-1000-8000-00805f9b34fb <<== mine
00001800-0000-1000-8000-00805f9b34fb <<== yours

Error message:
java.lang.RuntimeException: Characterisic not found: 00002902-0000-1000-8000-00805f9b34fb

Hmmm....
Any other ideas?
 
Upvote 0

emexes

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

Far out. But ok, at least we're being told the characteristic id is wrong, which is better than being told nothing and wondering why it's not working.

Was the characteristic readable as well as writeable?

Was it just WRITE, or WRITE NO RESPONSE, or both, or something else?

I have no idea what WRITE NO RESPONSE is, but perhaps it means that a normal WRITE sends back a confirmation that we have to read/acknowledge before the next write will be accepted.

Although that doesn't quite sound like java.lang.RuntimeException: Characterisic not found: 00002902-0000-1000-8000-00805f9b34fb

We might be forced to RTFM. 🤔
 
Last edited:
Upvote 0
Top