Android Question BLE2 Characteristic Read Question

Steve_ES

Member
Licensed User
Longtime User
Hello all,

Please forgive the Newbie question, but I've searched through the forum and the docs without success.

I'm having difficulty understanding how to read from a BLE Peripheral using the Map of the Characteristics When DataAvailable fires. The setup is as follows:

The Service which carries the data is 0000ffe1-0000-1000-8000-00805f9b34fb. Manager_DataAvailable is firing correctly, when it should, on the expected Characteristic=0000ffe1, and I can see in the debugger that the value that I intended to send is there and waiting for me. This is what the routine in question looks like:

B4X:
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Dim i As Byte
    Dim J As Byte
    Dim message As String
   
    Log(" ")
    Log("Parse Begin")
    For i = 0 To Characteristics.Size - 1
        If Characteristics.GetValueAt(i)=Null Then
            Log("Characteristic Null")
        Else
            Dim DataFromChar() As Int = Characteristics.GetValueAt(i)
            If DataFromChar.Length>0 Then
                Log("Characteristic Has Data")
                Log("Key: " & Characteristics.GetKeyAt(i))
'                Log("Value: " & " " & Characteristics.GetValueAt(i))
                For j=0 To DataFromChar.Length-1
                    message=message & Chr(DataFromChar(J))
                Next
                Log("Value: " & message)
                message=""
            Else
                Log("No Data")
            End If
        End If
    Next
    Log("Parse End")

End Sub

The question is why do Characteristic values which are null fail the test of Characteristics.GetValueAt(i)=Null? If the object returned is null (not zero, actually no data) then this condition is caught by the test of the array which the data is loaded into, namely that the Length parameter of that loaded array is 0.

Am I doing this wrong? Can I simply load the array and not be concerned if the source object is null or not?

This behavior is odd because if a null object is passed in the following line:
Dim DataFromChar() As Int = Characteristics.GetValueAt(i)
Then no error is reported, everything runs fine, but later if the a value of DataFromChar is referenced, for example somevariable=DataFromChar(index)+somethingelse then a fatal java out-of-bounds error occurs and the program crashes. The Length parameter of an array loaded in this manner is (so far) the only reliable way I've found to tell if a DataAvailable event is actually firing on a service+characteristic which has data.

BTW, in the application layer of my project I'll be parsing for a specific Characteristic in the Service of interest which is assumed to have data because I wrote the application in the Peripheral. I'm looking for robustness in the general case, and I'm sure I'm not there yet. And do please stop laughing at that last statement. We can hope, right?

Steve
 

Steve_ES

Member
Licensed User
Longtime User
Hi Erel,

This is a snippet of the code, following exactly your example, with a minor addition to show my inability to test if the returned obect
upload_2019-1-21_19-48-6.png
upload_2019-1-21_19-50-30.png
upload_2019-1-21_19-50-38.png
is null:

upload_2019-1-21_19-48-6.png


The yellow highlight shows where execution has stopped. I chose this characteristic because I know for sure the value is null. Here's what the debugger window shows for the values of the Characteristic and b():

upload_2019-1-21_19-50-38.png


The IDE thinks that the values are null. The Length of the array b() is zero. The logged output reports this:
This key has data
00002a05-0000-1000-8000-00805f9b34fb:

I believe this is the same result. No way to tell if the returned object is null? Just load an array and make sure to check the length before any reference to any element in the array, otherwise fatal java exception?

Steve
 
Upvote 0
Top