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
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
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.
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.
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)