Android Question Reading DataAvailable map

Steve-h

Member
I have data arriving but being unfamiliar with Maps can't figure how to access it.

B4X:
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Private rxdata As String
    Log("data arrived")
    rxdata=Characteristics.Get(ServiceId)
    Log(rxdata)
End Sub

Always seems to return a null value where nRF connect shows the 4byte number I am expecting.
 

drgottjr

Expert
Licensed User
Longtime User
if it's null, it could actually be null.
ServiceId may or may not be a key in the map.
"data arrived" is not particularly revealing. you might want to try something like log("data arrived: " & ServiceId & " " & Characteristics)

otherwise, your rxdata=Characteristics.Get(ServiceId) is technically correct
you need to see what's in Characteristics. printing it out is the easiest way. (you could also ask for its keys just to make sure ServiceId is actually one, but having a quick look at the map might be very helpful or surprising)
 
Upvote 0

Steve-h

Member
I tried
B4X:
Private rxdata As String
    rxdata=Characteristics.Get(rxuuid)
    Log(rxdata
)

Where rxuuid is the characteristic uuid the notification is coming from. Rather than reporting a null this reports some data but not what I was expecting. I then tried your suggestion and the logged result shows the same unexpected data after the = :-
B4X:
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@34eaa23d}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@124b5383}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@3e42539}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@2ae5a4df}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@2458dbf5}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@ba9affb}
data arrived: f1abd2e4-876c-11ec-a8a3-0242ac120002 (MyMap) {f1abd5fa-876c-11ec-a8a3-0242ac120002=[B@3284271}

The first section being the service uuid, the second is the characteristic uuid and the third after the = is something but I can't relate it to the data I am expecting.

If I look using nRF Connect I see exactly the data I am expecting. I can vary the data with a potentiometer and can change it from 0x0000 to 0xFFFF and reliably read the result, the numbers retrieved from data available don't seem to relate to what I am expecting, which in this case was 0x0000.

Not certain what you mean by "ask for its keys" Maps are still something of a mystery to me. Is MyMap the name of the map, the uuid the key and the bit at the end the data ?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
B4X:
For Each k As string In Characteristics .Keys
    Log("key: " & k & "   value: " & characteristics.Get(k))
Next

see what this gives you.

it looks like this problem is not unknown here. the values you get when running the above snippet may come out looking like garbage. they may be in bytes and would need to be converted. so after the log statement above, try inserting:
B4X:
   Log( bc.HexFromBytes( Characteristics.Get(k) ) )

i just took a quick look at the ble2 library. the valules in the map are byte arrays, so unless you're looking to deal with them at that level, you'll have to convert them to a hex string (as per above) in order to make any sense of them.

of course, this still doesn't address your original problem regarding a null value. either the value is actually null or the key used to access it isn't in the map to begin with. being able to look at the map as a whole would help resolve that.
 
Last edited:
Upvote 0
Top