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:
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
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