Hello, can someone advise how to correct the following code:
B4X:
Sub AStream_NewData (Buffer() As Byte)
Dim aaya() As Byte
aaya=BytesToString (Buffer, 0, Buffer.Length, "UTF8")
If aaya ="A" Then
Btn1.Color=Colors.RGB(128,255,128)
Else
Btn1.Color=Colors.RGB(255,128,128)
End If
End Sub
Thanks. What I mean is how would you work out the following:
With 1 String it works fine -
B4X:
If aaya ="A" Then
Btn1.Color=Colors.RGB(0,255,0)
Btn1.TextColor=Colors.RGB(139,0,0)
Else
Btn1.Color=Colors.RGB(139,0,0)
Btn1.TextColor=Colors.RGB(0,255,0)
End If
But I have 6 buttons. I tried this but it didn't worked:
B4X:
If aaya(0) ="A" Then
Btn1.Color=Colors.RGB(0,255,0)
Btn1.TextColor=Colors.RGB(139,0,0)
Else
Btn1.Color=Colors.RGB(139,0,0)
Btn1.TextColor=Colors.RGB(0,255,0)
End If
If aaya(1) ="B" Then '.....and so on for 6 buttons
Btn2.Color=Colors.RGB(0,255,0)
Btn2.TextColor=Colors.RGB(139,0,0)
Else
Btn2.Color=Colors.RGB(139,0,0)
Btn2.TextColor=Colors.RGB(0,255,0)
End If
In the first example only "A" comes in.
In the second example the incoming data is is ABcdef.
Do you know what is the value in the string and the length?
What does your string look like?
If it is: aaya = "ABCDE", you can do the following, but it depends that each charachter is at the right place.
B4X:
If aaya.Char(0) ="A" Then
Btn1.Color=Colors.RGB(0,255,0)
Btn1.TextColor=Colors.RGB(139,0,0)
Else
Btn1.Color=Colors.RGB(139,0,0)
Btn1.TextColor=Colors.RGB(0,255,0)
End If
If aaya.Char(1) ="B" Then '.....and so on for 6 buttons
Btn1.Color=Colors.RGB(0,255,0)
Btn1.TextColor=Colors.RGB(139,0,0)
Else
Btn1.Color=Colors.RGB(139,0,0)
Btn1.TextColor=Colors.RGB(0,255,0)
End If