B4R Question Unexpected results on bluetooth comms (B4R-B4J)

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi:

I've tried the B4R Bluetooth example with the B4J Bluetooth example program and I have strange results sending numbers or string from B4R to B4j

B4R

B4X:
    count=1
    Log ("Sending =",count)
    astream.Write(Array As Byte(count))
    astream.Write("AT+FLAG".GetBytes)

B4J

B4X:
Sub AStream_NewData (Buffer() As Byte)

    Log ("Buffer  " & Buffer.Length & " Buffer =" &Buffer (0))
    Log ("Buffer to String "&BytesToString(Buffer, 0, Buffer.Length, "UTF8"))

End Sub

When received the stream on PC, if the sended stream is a string - "AT+FLAG"-, buffer contains the ASCII codes of each character of the string, but if the sended stream is a number (0,1,...), the buffer contains the number not the ASCII code of the number (so BytestoString -UTF8- doesn't work)

B4X:
Buffer  1 Buffer =1
Buffer to String
Buffer  1 Buffer =65
Buffer to String A
Buffer  7 Buffer =84
Buffer to String T+FLAG
I've tried to change at Arduino (byte to String) but I didn't find how to do it, which is the best way? (obviously, I can control the buffer value and add the value to the corresponding ASCII value)

Another question, is possible to configure astream in prefix mode?? I receive the string stream on PC in several streams (I'll ve to implement a function to implement EOT on Arduino and read it on PC)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
the buffer contains the number not the ASCII code of the number
This is the expected result. You are sending numbers not strings.

You can convert the number to string with:
B4X:
Dim s As String = count
astream.Write(s.GetBytes)
Or:
B4X:
astream.Write(NumberFormat(count, 0, 0).GetBytes)

Note that if the data that you are sending is numbers then in most cases it is better to send it as numbers and avoid converting it to strings.

Another question, is possible to configure astream in prefix mode?
Currently no.

You can use AsyncStreamsText on the PC side and end each message with:
B4X:
astream.Write("AT+FLAG".GetBytes)
astream.Write(Array As Byte(10))
 
Upvote 0
Top