Android Question How do I read data from a service address UUID and characteristic UUID using the BLE2 library?

seskindell

Active Member
Licensed User
I connect to the device ok ...

Now, how do I read data from service: "0000ffa0-0000-1000-8000-00805f9b34fb"
at the characteristic: "0000ffa3-0000-1000-8000-00805f9b34fb"

The examples I seen use ble and not ble2
I have NO idea where the library ble is at .. it is not in my Libraries for the latest version of B4A.
 

JordiCP

Well-Known Member
Licensed User
Can you describe what you are trying or add your code?

Once you start the scan, must wait for the xxx_DeviceFound event to be fired. Then try to connect to the specific device and wait for the xxx_Connected event.
Only then, you can try to read services and characteristics with
B4X:
myBLE.ReadData( myServiceString )
'or
myBLE.ReadData2( myServiceString, myCharacteristicString )
and if it works, receive the data in xxx_DataAvailable event.
 
Upvote 0

seskindell

Active Member
Licensed User
Can you describe what you are trying or add your code?

Once you start the scan, must wait for the xxx_DeviceFound event to be fired. Then try to connect to the specific device and wait for the xxx_Connected event.
Only then, you can try to read services and characteristics with
B4X:
myBLE.ReadData( myServiceString )
'or
myBLE.ReadData2( myServiceString, myCharacteristicString )
and if it works, receive the data in xxx_DataAvailable event.

Thank you!

The readdata2 seems to work good. Can you give me an example of what I need to include for the DataAvailable event (and to parse out the data).

I'm trying to read the x,y,z coordinates of an accel.

- Steve
 
Upvote 0

JordiCP

Well-Known Member
Licensed User
It depends on how your BLE device specifically serves the data for that characteristic. For instance, it can be one byte for each axis, or a string representation...do you have any documentation of this sensor?
 
Upvote 0

seskindell

Active Member
Licensed User
Hi Jordi,

I was able to output the results for the x,y,z axis .. I'm not sure how to parse it out. I will look for the documentation on the sensor.

Here is the output:
0000ffa1-Accel Enable
0000ffa0-0000-1000-8000-00805f9b34fb
(MyMap) {0000ffa1-0000-1000-8000-00805f9b34fb=[B@3f6528c4, 0000ffa2-0000-1000-8000-00805f9b34fb=[B@2a996fb, 0000ffa3-0000-1000-8000-00805f9b34fb=[B@f57f318, 0000ffa4-0000-1000-8000-00805f9b34fb=[B@31140171, 0000ffa5-0000-1000-8000-00805f9b34fb=[B@c5b3b56}

0000ffa2-Accel Range
0000ffa0-0000-1000-8000-00805f9b34fb
(MyMap) {0000ffa1-0000-1000-8000-00805f9b34fb=[B@3f6528c4, 0000ffa2-0000-1000-8000-00805f9b34fb=[B@1c1643ad, 0000ffa3-0000-1000-8000-00805f9b34fb=[B@f57f318, 0000ffa4-0000-1000-8000-00805f9b34fb=[B@31140171, 0000ffa5-0000-1000-8000-00805f9b34fb=[B@c5b3b56}

0000ffa3-x axis coord.
0000ffa0-0000-1000-8000-00805f9b34fb
(MyMap) {0000ffa1-0000-1000-8000-00805f9b34fb=[B@3f6528c4, 0000ffa2-0000-1000-8000-00805f9b34fb=[B@1c1643ad, 0000ffa3-0000-1000-8000-00805f9b34fb=[B@2e7b6e2, 0000ffa4-0000-1000-8000-00805f9b34fb=[B@31140171, 0000ffa5-0000-1000-8000-00805f9b34fb=[B@c5b3b56}

0000ffa4-y axis coord.
0000ffa0-0000-1000-8000-00805f9b34fb
(MyMap) {0000ffa1-0000-1000-8000-00805f9b34fb=[B@3f6528c4, 0000ffa2-0000-1000-8000-00805f9b34fb=[B@1c1643ad, 0000ffa3-0000-1000-8000-00805f9b34fb=[B@2e7b6e2, 0000ffa4-0000-1000-8000-00805f9b34fb=[B@2b2dea73, 0000ffa5-0000-1000-8000-00805f9b34fb=[B@c5b3b56}

0000ffa5-z axis coord.
0000ffa0-0000-1000-8000-00805f9b34fb
(MyMap) {0000ffa1-0000-1000-8000-00805f9b34fb=[B@3f6528c4, 0000ffa2-0000-1000-8000-00805f9b34fb=[B@1c1643ad, 0000ffa3-0000-1000-8000-00805f9b34fb=[B@2e7b6e2, 0000ffa4-0000-1000-8000-00805f9b34fb=[B@2b2dea73, 0000ffa5-0000-1000-8000-00805f9b34fb=[B@27ee0d30}
 
Upvote 0

JordiCP

Well-Known Member
Licensed User
Yes, it will be needed. Is it a commercial device?

Anyway, you can run the BLE example in the BLE2 library thread adapted to the exact device you want to connect and the service/characteristics to be read, or, if you are using your own implementation, log it to view its contents.

B4X:
Sub xxx_DataAvailable (Service As String, Characteristics As Map)   'whatever xxx is
    ' Here we just log what is received
    For Each id As String In Characteristics.Keys
        Dim dataContent() as Byte = Characteristics.Get(id)

        ' If the values are string-formatted, convert the bytes to string
        ' Log( "Characteristic:"& id &": "&BytesToString(dataContent, 0, dataContent.Length, "UTF8") )

        ' Or, if we know that each byte has a meaning, just show the bytes 
        ' Dim bc as ByteConverter    '<---- will need the ByteConverter library
        ' Log( "Characteristic:"& id &": "& bc.HexFromBytes(dataContent) )
    Next
End Sub
 
Upvote 0

seskindell

Active Member
Licensed User
Yes, it will be needed. Is it a commercial device?

Anyway, you can run the BLE example in the BLE2 library thread adapted to the exact device you want to connect and the service/characteristics to be read, or, if you are using your own implementation, log it to view its contents.

B4X:
Sub xxx_DataAvailable (Service As String, Characteristics As Map)   'whatever xxx is
    ' Here we just log what is received
    For Each id As String In Characteristics.Keys
        Dim dataContent() as Byte = Characteristics.Get(id)

        ' If the values are string-formatted, convert the bytes to string
        ' Log( "Characteristic:"& id &": "&BytesToString(dataContent, 0, dataContent.Length, "UTF8") )

        ' Or, if we know that each byte has a meaning, just show the bytes
        ' Dim bc as ByteConverter    '<---- will need the ByteConverter library
        ' Log( "Characteristic:"& id &": "& bc.HexFromBytes(dataContent) )
    Next
End Sub
Yes .. it is a small sensor from u-blox .. the OLP425

Thank you for you help.

- Steve
 
Upvote 0

JordiCP

Well-Known Member
Licensed User
According to the documentation (at 6.1.0.0.3), seems that X-axis acceleration (FFA3) is encoded in a single byte with a range [-2G...2G]
Also states that the characteristic is notifiable, but you can always simply read its value.

Here comes the interpretation part :)
It is defined as 'int8' in the uBLox side, corresponding to [-2G...2G]. As in B4A bytes are also signed, unless a strange mapping is used, the conversion is direct, so you will have (not tested):
B4X:
Sub xxx_DataAvailable (Service As String, Characteristics As Map)   'whatever xxx is
    ' Here we just log what is received
    For Each id As String In Characteristics.Keys
        Dim dataContent() as Byte = Characteristics.Get(id)

        ' Calculate X_axis acceleration if the read characteristic (id) is the 0000ffa3-..., or Y_axis if 0000ffa4, ....
        if id="0000ffa3-......"  Then  '<--fill with correct values 
           Dim X_accel_scaled as Byte = dataContent(0)
           Dim X_accel_final as Float = X_accel_scaled *2*9.8/127     ' Scale conversion : convert [-128..+127] to [-2G..+2G]
           Log( "X_accel value is: "& X_accel_final )
        End if   

    Next
End Sub
 

Attachments

  • Bluetooth Low Energy (BLE) GATT Examples with OBS421 & OLP425.pdf
    69.2 KB · Views: 222
Upvote 0
Top