Another small example. Using XBee for wireless communication between a PC and an Arduino:
The left adapter is connected to the PC. It can also be connected to an Android or Raspberry Pi, such as in this example:
https://www.b4x.com/android/forum/threads/62564/#content
It is very simple to configure and the distance can be 100 meters+.
Writing to the serial causes the XBee to transmit the data and everything received by the XBee is sent to the serial input.
The B4R code:
Sub Process_Globals
Public Serial1 As Serial
Private astream As AsyncStreams
Private leds(2) As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
leds(0).Initialize(11, leds(0).MODE_OUTPUT)
leds(1).Initialize(12, leds(0).MODE_OUTPUT)
astream.Initialize(Serial1.Stream, "astream_NewData", "Astream_error")
End Sub
Sub Astream_NewData (Buffer() As Byte)
For i = 0 To Buffer.Length - 1 Step 2
Dim ledIndex As Byte = Buffer(0)
Dim ledStatus As Boolean
If Buffer(1) = 1 Then ledStatus = True Else ledStatus = False
leds(ledIndex).DigitalWrite(ledStatus)
Next
End Sub
Sub Astream_Error
Log("Error")
End Sub
B4J code:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private serial As Serial
Private astream As AsyncStreams
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
serial.Initialize("serial")
serial.Open("COM33")
astream.Initialize(serial.GetInputStream, serial.GetOutputStream, "Astream")
MainForm.Show
End Sub
Sub ToggleButton_SelectedChange(Selected As Boolean)
Dim v As Node = Sender
Dim ledIndex As Byte = v.Tag
Dim ledStatus As Byte
If Selected Then ledStatus = 1 Else ledStatus = 0
astream.Write(Array As Byte(ledIndex, ledStatus))
End Sub