#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Serial Example
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#End Region
'Activity module
Sub Process_Globals
Dim Timer1 As Timer
Dim m As Map
Dim CurrentState As String = "arry1"
Dim serial1 As Serial
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)
m.Initialize
m.put("arry1", Array As Byte(83, 68, 82, 158, 127, 0, 1, 29))
m.put("aray2", Array As Byte(83, 68, 82, 95, 127, 0, 0, 222))
m.put("arry3", Array As Byte(83, 68, 82, 127, 158, 0, 1, 29))
m.put("arry4", Array As Byte(83, 68, 82, 127, 95, 0, 0, 229))
m.put("arry5", Array As Byte(83, 68, 82, 127, 127, 0, 0,254))
Timer1.Initialize("Timer1", 50)
Timer1.Enabled = True
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)
Timer1.Enabled = True
connected = True
Else
connected = False
Timer1.Enabled = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub
Sub mnuDisconnect_Click
serial1.Disconnect
connected = False
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnSend_Click
If connected Then
Dim a() As Byte = Array As Byte(83, 68, 82, 127, 95, 0, 0, 222)
Dim out As OutputStream
out = serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
Sub Timer1_Tick
If connected Then
Dim a() As Byte = m.Get(CurrentState) ' grabs the byte array with the current state
Dim out As OutputStream
out = serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub