Android Question how to get bytes from BLE communication

jimseng

Active Member
Licensed User
Longtime User
Hello
Using the BLE example I can see my byte array from my BLE peripheral by hovering over Characteristics.Get(ServiceId)) with a break point. I just need help getting the array into something. I just can't work it out.
B4X:
    Dim bc As ByteConverter
'    Dim KeyCode As String
    
    For Each ServiceId As String In Characteristics.Keys
    '    KeyCode = Characteristics.Get(ServiceId)
    Dim result As Byte =(bc.IntsFromBytes(Characteristics.Get(ServiceId)))
    
    lblStat.Text = result
Here I can see my data (11,15,127,128,129)
BLE.jpg
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

In your B4A (Basic4android) application, you are trying to handle a byte array from a BLE (Bluetooth Low Energy) peripheral. From your code snippet, it looks like you are attempting to convert the byte array from the BLE characteristic into integers, and then display these integers in a label. However, there seem to be a few issues with your code. Let's go through it and I'll suggest corrections.

Issues in the Code

  1. Type Mismatch in Conversion: The IntsFromBytes method returns an array of integers (Int()), not a single byte. Assigning it to a byte variable (Dim result As Byte) will not work.
  2. Loop Handling: If there are multiple services, the label (lblStat.Text) will only show the result from the last service processed in the loop.

Corrected and Improved Code

Here's a revised version of your code that handles these issues:
B4X:
Dim bc As ByteConverter
Dim KeyCode() As Byte
Dim result() As Int
Dim results As String = ""

For Each ServiceId As String In Characteristics.Keys
    KeyCode = Characteristics.Get(ServiceId)  ' Get the byte array from the characteristic
    result = bc.IntsFromBytes(KeyCode)  ' Convert byte array to an array of integers

    ' Append all integers from the result to the results string
    For Each val As Int In result
        results = results & val & ", "
    Next
Next

' Update the label text with all results, trimming the trailing comma
If results.Length > 0 Then
    results = results.SubString2(0, results.Length - 2)
End If

lblStat.Text = results

Explanation

  • Data Handling: The code now correctly handles the byte array conversion to an array of integers. It processes all services and concatenates their results into a string, which is then displayed in the label.
  • Concatenation of Results: It concatenates the integer results into a single string, separated by commas. This allows you to see all the integer values from all services.
  • Final Display: The final string is assigned to lblStat.Text, showing all integer values derived from the byte arrays of all services.
This code assumes that the byte array from each service can be meaningfully converted to an array of integers. If there's a specific format or encoding in the byte array (like UTF-8 text or specific structured data), you might need to handle the conversion differently.
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
You just beat me to it. Actually all I needed in the end was
B4X:
Dim f() As Byte = Characteristics.Get(ServiceId)
I didn't need the ByteConverter at all.
 
Upvote 0

emexes

Expert
Licensed User
all I needed in the end
So, all sorted out?

The 5 returned bytes are not ASCII text, nor seemingly any combination of 16-bit and/or 32-bit numbers.

This trick might be useful:
B4X:
Dim f() As Byte = Characteristics.Get(ServiceId)
Log(f.As(List))    'if List-to-String works here
lblStat.Text = f.As(List)    'then it might work here too
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
HI emexes
I hard wired some integer values into my micropython code just for testing purposes and your trick from the previous post gives me the exact values:
The f.as(list) works:
(ArrayList) [11, 15, 127, -128, -127, -2, -46] (unsigned)
B4X:
Dim str As String
    For i =0 To f.Length -1
            str =str & Bit.And(f(i), 0xFF) & ","
    Next
    Log(str)
11,15,127,128,129,254,210,
 
Upvote 0
Top