#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim btnOpen As Button
Dim cmbPort As ComboBox
Dim txtInput As TextField
Dim txtLog As TextArea
Dim lblStatus As Label
Private sp As Serial
Private astream As AsyncStreams
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
MainForm.Title = "Bluetooth Chat"
MainForm.BackColor = fx.Colors.White
sp.Initialize("")
cmbPort.Items.AddAll(sp.ListPorts)
End Sub
Sub cmbPort_SelectedIndexChanged(Index As Int, Value As Object)
btnOpen.Enabled = Index > -1 'enable the button if there is a selected item
End Sub
Sub btnOpen_Action
sp.Open(cmbPort.Value)
sp.SetParams(115200, 8, 1, 0)
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
txtInput.Enabled = True
btnOpen.Enabled = False
lblStatus.Text = "Status: Open"
End Sub
Sub MainForm_Closed
sp.Close
End Sub
Sub AStream_NewData (Buffer() As Byte)
LogDebug("Hey I'm in NewData!")
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
'Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
'Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
LogMessage("You", s)
End Sub
Sub txtInput_Action
astream.Write(txtInput.Text.GetBytes("UTF8"))
'astream.Write(txtInput.Text.GetBytes("ASCII"))
'astream.Write(txtInput.Text.GetBytes("UTF-8"))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Me", txtInput.Text)
End Sub
Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SetSelection(txtLog.Text.Length, txtLog.Text.Length)
End Sub
Sub AStream_Error
Log("Error: " & LastException)
astream.Close
AStream_Terminated
End Sub
Sub AStream_Terminated
Log("Connection is broken.")
lblStatus.Text = "Status: Close"
txtInput.Enabled = False
btnOpen.Enabled = True
End Sub