Android Question Input Buffer

freetoair

Member
Licensed User
Longtime User
I need to receive 2-byte coded unusual and this routine works pretty chaotic. Is the problem in the deletion of the buffer? When an error occurs get a message on the screen:


An error has occurred in sub: main_astreams_nevdata (java line: 465)
java.lang.ArrayIndexOutOfBoundsException: length = 1; Index = 1






B4X:
Sub Astreams_NewData (Buffer() As Byte)
    Dim r As Int = 0
        r = ((Bit.ShiftRight(Buffer(0),4) * 100) + (Bit.ShiftRight(Buffer(1),4) * 10) + (Bit.And(Buffer(1),15))) * 2
            skbValue_ValueChanged(r, False)                'Sub  that refreshes the LED bar
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
You are not getting the data you expect. Buffer(1) is throwing the error because there is only one byte present i.e. length=1 in the error message. You could guard against this error by checking the size of the array of bytes before running your function.
 
Upvote 0
Top