Android Question Sending a bluetooth byte

saunwin

Active Member
Licensed User
Longtime User
Good Morning,
Can someone steer straight (again - sob sob - soz)
I am trying to send a single unsigned byte (higher than 128) to my microcontroller but couldn't (and couldn't figure out how to change a signed byte to an unsigned one).
so I used the binary pattern equivalent and used negative numbers in B4A.

Anyway - the following code is supposed to send a single byte from a button press, but the compiler grumbles with a

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

at buffer(0)= -15

Any/All help gratefully appreciated.
Cheers.


B4X:
Sub Button_Click
'BT transmission protocol from tablet TO base unit (one byte)
'81 (Start game 1) Or 82 (Start game 2) Or 83 (Start game 3)
'8F (Stop current game)
    Dim buffer(0) As Byte
    If Button.Text="START" And TLRB.Checked=True Then
        buffer(0) = -2
           
        Else If Button.Text="START" And CNRB.Checked=True Then
        buffer(0) = -3
           
        Else If Button.Text="START" And BCRB.Checked=True Then
        buffer(0) = -1
           
        Else If Button.Text="STOP" Then
        buffer(0) = -15
           
        End If
    AStreams.Write(buffer)
End Sub
 

udg

Expert
Licensed User
Longtime User
Dim buffer(0) As Byte
buffer will have a size of zero, so no place for values..change it to 1

edit: Manfred was quicker than me, just a second or two but he was..eheh
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Ok, but this one is more complete.. eheh
Your array size is set to zero, so no place for values; please modify it to 1.

Regarding the unsigned byte conversion, Erel replied already in a different thread of yours.

Now it's your turn again, Manfred .
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

udg

Expert
Licensed User
Longtime User
Yes, I was joking. I attached a reference to a different thread where Erel explained how to solve the signed versus unsigned byte conversion.

ps: and you beat me again, posting while I was writing this..eheh
We should set for a token protocol. The one holding the token posts while the other one waits silently..
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Ok OK already !
I got it - I had an empty array - DOH !
Thanks chaps - I appreciate it.
Although I kinda don't get the unsigned bit, because in Erels code the number gets turned into an int from a byte and i need a byte for astreams.write
(Y/N ?) - The code works fine for receiving.

B4X:
Sub ToUnsigned(b As Byte) As Int
    Return Bit.And(0xFF, b)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…