Wish Updated / new BLE library that can handle multiple services / characteristics using the same UUID

OliverA

Expert
Licensed User
Longtime User
@Jmu5667 and I ran into an issue with BleManager2 while working on a device that implements Bluetooth's Environmental Sensing Service. The issue is that the UUIDs in Bluetooth can be the same for multiple services / multiple characteristics (and the device we are currently working on has multiple characteristics using the same UUID). Because of how the current BLE library is implemented, only one characteristic that shares the same UUID with others can be worked with. So this wish is for a BLE library that can work with services and characteristics that share the same UUID.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Futher to what @OliverA has written, I will try to explain the use case.

Draeger manufacture Gas detectors (https://www.draeger.com/en_uk/Home). Some of these device have a single sensor and some have multiple sensors. There is a service 04bc0201-f669-457e-92d0-75f4b06308cd which is their gas detector service. This service uses the same Characteristic UUID for each sensor on the device e.g 04bc0202-f669-457e-92d0-75f4b06308cd.

We did some work this morning on this and produced the following log data
B4X:
Service UUID 04bc0201-f669-457e-92d0-75f4b06308cd 
03/22/2024 14:00:54.597 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.598 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.600 - svc_drager::bleManager_DataAvailable::NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
03/22/2024 14:00:54.601 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.601 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.602 - svc_drager::bleManager_DataAvailable::NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
03/22/2024 14:00:54.603 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.603 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.604 - svc_drager::bleManager_DataAvailable::NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
03/22/2024 14:00:54.605 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0202-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.605 - svc_drager::bleManager_DataAvailable::NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
NEW BLE - 04bc0203-f669-457e-92d0-75f4b06308cd
03/22/2024 14:00:54.606 - svc_drager::bleManager_DataAvailable::NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
NEW BLE - 00002a7d-0000-1000-8000-00805f9b34fb
03/22/2024 14:00:54.607 - svc_drager::bleManager_DataAvailable::    Characteristic UUID 04bc0202-f669-457e-92d0-75f4b06308cd 
    Characteristic UUID 04bc0202-f669-457e-92d0-75f4b06308cd 
03/22/2024 14:00:54.607 - svc_drager::bleManager_DataAvailable::    Characteristic Data = 000000FF 
    Characteristic Data = 000000FF 
03/22/2024 14:00:55.117 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: 0100 
        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: 0100 
03/22/2024 14:00:55.635 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 04bc0210-f669-457e-92d0-75f4b06308cd, Value: 000000006368340037342D38322D3800254C454C00 
        Descriptor UUID 04bc0210-f669-457e-92d0-75f4b06308cd, Value: 000000006368340037342D38322D3800254C454C00 
03/22/2024 14:00:55.640 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 04bc0210-f669-457e-92d0-75f4b06308cd, Real Value:     ch4 74-82-8 %LEL 
        Descriptor UUID 04bc0210-f669-457e-92d0-75f4b06308cd, Real Value:     ch4 74-82-8 %LEL 
03/22/2024 14:00:55.643 - svc_drager::bleManager_DataAvailable::    Characteristic UUID 04bc0203-f669-457e-92d0-75f4b06308cd 
    Characteristic UUID 04bc0203-f669-457e-92d0-75f4b06308cd 
03/22/2024 14:00:55.646 - svc_drager::bleManager_DataAvailable::    Characteristic Data = 0000 
    Characteristic Data = 0000 
03/22/2024 14:00:56.161 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: Null 
        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: Null 
03/22/2024 14:00:56.672 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 04bc0211-f669-457e-92d0-75f4b06308cd, Value: Null 
        Descriptor UUID 04bc0211-f669-457e-92d0-75f4b06308cd, Value: Null 
03/22/2024 14:00:56.676 - svc_drager::bleManager_DataAvailable::    Characteristic UUID 00002a7d-0000-1000-8000-00805f9b34fb 
    Characteristic UUID 00002a7d-0000-1000-8000-00805f9b34fb 
03/22/2024 14:00:56.679 - svc_drager::bleManager_DataAvailable::    Characteristic Data =  
    Characteristic Data =  
03/22/2024 14:00:57.187 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: Null 
        Descriptor UUID 00002902-0000-1000-8000-00805f9b34fb, Value: Null 
03/22/2024 14:00:57.698 - svc_drager::bleManager_DataAvailable::        Descriptor UUID 04bc0211-f669-457e-92d0-75f4b06308cd, Value: Null 
        Descriptor UUID 04bc0211-f669-457e-92d0-75f4b06308cd, Value: Null

The NEW BLE entries are logged from the following code in bleManager_DataAvailable:
B4X:
Dim ser As JavaObject = jo.RunMethod("getService", Array(ServiceId))
Dim cLst As List = ser.RunMethod("getCharacteristics",Null)
For Each sJo As JavaObject In cLst
    log($"svc_drager::bleManager_DataAvailable::NEW BLE - ${sJo.RunMethodJO("getUuid",Null).RunMethod("toString",Null)}"$)
Next

So, from the above we can see there are multiple Characteristics UUID under the same service UUID. Using the nRF app we can connect to the device (X-AM 2800) and set a notification on for say the O2 sensor, we can trigger the sensor ansd see the notification values changing.

for each sensor you can set a notification so when the sensor is triggered we get notifications etc.

On a single sensor device the current BLE lib works as expected, with multiple sensors it does not function correctly.

I hope I have explained this but please feel free to ask myself for @OliverA any further information regards this.

Many thanks for reading this.

John.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've uploaded a beta version that adds an option to work with the BluetoothGattCharacteristics objects instead of the uuids.

Once you call:
B4X:
manager.As(JavaObject).SetField("charsAsObjects", True)

Characteristics will no longer be converted to uuids strings.
This means that the keys in the Characteristics map will be those objects. You should use JavaObject to access their properties.

There are also new methods to set notifications with those objects instead of the uuids.
You can find them in the source code: https://github.com/AnywhereSoftware...anywheresoftware/b4a/objects/BleManager2.java
And I'll help if needed.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Thanks for the fast response Erel. I will start testing this today and keep you updated.
 

OliverA

Expert
Licensed User
Longtime User
The events signature also changes:
Could we also get event triggers for onDescriptorRead and onDescriptorWrite Gatt callbacks? For some reason, we are getting back descriptor values from the first characteristic's descriptors, but not the second (and beyond) of the characteristics that share the same UUID. It would be good to know what the status is on those callbacks.

Update: We are currently using sleep(50) in a loop, but that seems hacky and would not catch any error notifications
Update: Increasing the time for the sleep(50) loop seems to get the values, but again, we had to guess the time to wait. Callbacks would surely help us clean up the code.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Two new events: DescriptorRead / DescriptorWrite

Please test.
B4X:
gatt.RunMethod("readDescriptor", Array(desc))
Wait For bleObjManager_DescriptorRead (Success As Boolean, objDesc As Object)
Dim value() As Byte = objDesc.As(JavaObject).RunMethod("getValue", Null)

Currently testing this.
 

OliverA

Expert
Licensed User
Longtime User
Cookies are required to use this site. You must accept them to continue using the site. Learn more…