Ok, here goes newbie....
Using the Bluetooth.b4a example I'm able to talk to one of my BT devices. Commands go in, data comes out. That data consists mostly of messages of 7 bytes of sensor data but can also contain 6 bytes (a mac address and then maybe 20 bytes representing ascii characters. So I am trying to separate those from the stream.
From the (reworked) example:
Above code works. What I do not understand is if I do not initialize the size of the temp var (like temp()) this breaks when I try to fill it with an out-of-bounds exception. But the Buffer() var, which is also not initialized with a size, does not break. What is the difference?
And another thing, how do I get a single Hex byte from the Buffer() array? HexFromBytes(Buffer(1)) does not work.
Using the Bluetooth.b4a example I'm able to talk to one of my BT devices. Commands go in, data comes out. That data consists mostly of messages of 7 bytes of sensor data but can also contain 6 bytes (a mac address and then maybe 20 bytes representing ascii characters. So I am trying to separate those from the stream.
From the (reworked) example:
B4X:
Private Sub AStream_NewData (Buffer() As Byte)
Private temp(6) As Byte
Private i As Int
Log(Buffer(1))
Log(conv.hexFromBytes(Buffer))
If Buffer.Length = 7 Then
'This is a message containing a battery charge value and sensor data
'I can display these bytes using HexFromBytes
CallSub2(ChatActivity, "NewMessage", conv.HexFromBytes(Buffer))
Else
For i = 0 To 5
temp(i) = Buffer(i)
Next
'This line displays the mac address of the device
CallSub2(ChatActivity, "NewMessage", conv.HexFromBytes(temp))
'and the next line displays the ascii part of the message (program name and version).
CallSub2(ChatActivity, "NewMessage", BytesToString(Buffer, 6, Buffer.Length-6, "UTF8"))
End If
End Sub
Above code works. What I do not understand is if I do not initialize the size of the temp var (like temp()) this breaks when I try to fill it with an out-of-bounds exception. But the Buffer() var, which is also not initialized with a size, does not break. What is the difference?
And another thing, how do I get a single Hex byte from the Buffer() array? HexFromBytes(Buffer(1)) does not work.