'Activity module
Sub Process_Globals
Dim AStreams As AsyncStreams
Dim Serial1 As Serial
'Dim Timer1 As Timer
Dim connected As Boolean
End Sub
Sub Globals
Dim btnSend As Button
Dim txtLog As EditText
Dim txtSend As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Serial1.Initialize("Serial1")
'Timer1.Initialize("Timer1", 200)
End If
Activity.LoadLayout("1")
Activity.AddMenuItem("Connect", "mnuConnect")
Activity.AddMenuItem("Disconnect", "mnuDisconnect")
End Sub
Sub Activity_Resume
If Serial1.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
Serial1.Listen 'listen for incoming connections
End If
End Sub
Sub mnuConnect_Click
Dim PairedDevices As Map
PairedDevices = Serial1.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1
l.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res = InputList(l, "Choose device", -1) 'show list with paired devices
If res <> DialogResponse.CANCEL Then
Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
End If
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Connected successfully", False)
AStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStreams")
Log("AStreams Initialized")
'timer1.Enabled = True
connected = True
Else
connected = False
'Timer1.Enabled = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub
Sub mnuDisconnect_Click
If connected Then
AStreams.Close
Serial1.Disconnect
connected = False
Log("AStreams Closed")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Log("closing")
AStreams.Close
End If
End Sub
Sub btnSend_Click
If connected Then
If AStreams.IsInitialized = False Then
Return
End If
If txtSend.Text.Length > 0 Then
Dim buffer() As Byte
txtSend.Text = txtSend.Text
buffer = txtSend.Text.GetBytes("UTF8")
AStreams.Write(buffer)
Log("Sending: " & txtSend.Text)
txtSend.Text = ""
End If
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
txtLog.Text = txtLog.Text & msg
Log("Rec: " & msg)
End Sub
Sub AStreams_Error
Log("AStreams Error: " & LastException.Message)
ToastMessageShow(LastException.Message, True)
End Sub
'Sub Timer1_Tick
' If connected Then
'
' End If
'End Sub