Android Question Does B4A understand hex?

MartinR

Member
Licensed User
The behaviour of the following code puzzles me.

B4X:
Sub AStream_NewData (Buffer() As Byte)
    'Cirel sends 0xff handhsake before sending file
    'We need to respond with 0xfe to acknowledge
    If Buffer(0) = 0xFF Then
        Log("FF received")
        Dim tw As TextWriter
        tw.Initialize(serial1.OutputStream)
        tw.Write(Chr(0xFE))
        tw.Flush    'to get the FE transmitted
    Else      
        Log(Buffer(0) & "received")
    End If
      
End Sub

When Buffer(0) contains hex FF the first test fails, but the else clause logs -1 received.

The buffer is an array of bytes, so I would have thought 0xFF and -1 would be treated equivalently.

The work around is to test if Buffer(0) = -1, of course. If I do this the textwriter appears to write 195 to the (Bluetooth) serial port, rather than 254. Even if I write Chr(254) to the textwriter, I receive 195 at the other side.

A second question, if I may: what's a more straightforward way of sending a byte to the serial port? Using a textwriter seems illogical if I'm thinking hex bytes.


Thanks, Martin
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Java uses signed bytes values -128 to 127.

To convert it to an int so you can compare it to an unsigned value, you need to do :

B4X:
Bit.And(Buffer(0),0xFF)
 
Upvote 0

MartinR

Member
Licensed User
Thanks, Steve. That works OK for the Buffer test.

Sill got problems sending/receiving the 0xFE/254 back but suspect the problem is at the receiving end.

Any thoughts about sending a byte to the Bluetooth serial port without using a textwriter?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No, Sorry I've not used Bluetooth for anything. Have you checked the examples?
 
Upvote 0

MartinR

Member
Licensed User
Many thanks.

I had do declare the array as an array of bytes for the code to compile, and then it works fine. Since I am using prefix mode, I receive the four-byte length before the handshake data itself, but that's OK.
B4X:
        AStream.Write(Array As Byte(0xFE))
 
Last edited:
Upvote 0
Top