Android Question How to retrieve a Characteristic value from its UUID ?

Using the BLE2 library, from the documentation it's not clear to me how to read a characteristic value, knowing its UUID (and of course after a connection is already open).

In the Documentation,
ReadData2 (Service As String, Characteristic As String)
Are the Service and Characteristic strings, their UUIDs ?
 

emexes

Expert
Licensed User
Two tips that might save you some grief āœŒ

1/ get the UUID from the BLE session manager itself, rather than hardcoding them - that way, you know you've got the format exactly right šŸ§

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Private manager As BleManager2
    Private rp As RuntimePermissions
   
    Dim BLEId As String = ""
    Dim BLEService As String = ""
    Dim BLECharacteristic As String = ""
   
    Dim BLEReadyFlag As Boolean = 0
       
End Sub

Sub Manager_Connected(Services As List)
   
    Log("Manager_Connected, Services are:")
   
    For Each S As String In Services
        Log("  [" & S & "]")
       
        If S.Contains("07:45") Then    '*** PUT FILTER FOR RELAY SERVICE HERE ***
            BLEService = S
            BLEReadyFlag = True
        End If
    Next
   
End Sub

2/ Some devices (especially communication-oriented stuff like serial cable replacement) need to be read using SetNotify. For these devices, ReadData and ReadData2 will usually get you nothing. :oops: Sometimes reading using SetNotify or SetIndication is better anyway, in that it only sends you the data when it changes, rather than you having to poll it all the time.
 
Upvote 0

emexes

Expert
Licensed User
Each element in the Characteristics Map has two components:

The map element's key is the BLE Characteristic Id
The map element's value is the characteristic's data eg sensor value

B4X:
Sub Manager_DataAvailable(ServiceID As String, Characteristics As Map)
   
    For Each S As String In Characteristics.Keys
        If S.StartsWith("835ab4c0") Then   'you say lazy, I say flexible
            SensorCharacteristic = S
           
            Dim Data1() As Byte = Characteristics.Get(SensorCharacteristic)
           
            SensorReadingRaw = BytesToShort(Data1(1), Data1(0))
            SensorReadingTime = DateTime.Now
           
            Exit    'for each
        End If
    Next
       
End Sub
 
Last edited:
Upvote 0
Top