Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ser As Serial
'Dim TextReader1 As TextReader
'Dim TextWriter1 As TextWriter
Dim Timer1 As Timer
Dim connected As Boolean
Dim time_out As Int
Dim AStream As AsyncStreams
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Label1 As Label
Private Button1 As Button
Private Button2 As Button
Private Button3 As Button
Private txtLog As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
If FirstTime Then
ser.Initialize("Serial1")
'Timer1.Initialize("Timer1", 300)
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
Sub AStream_Error
ToastMessageShow("Connection is broken.", True)
'btnSend.Enabled = False
'txtInput.Enabled = False
End Sub
Sub AStream_Terminated
AStream_Error
End Sub
Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End Sub
Sub Activity_Resume
If ser.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
ser.Listen 'listen for incoming connections
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If connected Then
ser.Disconnect
End If
End Sub
Sub Button1_Click
Dim PairedDevices As Map
PairedDevices = ser.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
'Msgbox(l.Get(res),"Microlino")
ser.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
Label1.Text="Connected successfully"
'TextReader1.Initialize(ser.InputStream)
'TextWriter1.Initialize(ser.OutputStream)
'If AStream.IsInitialized = False Then
AStream.InitializePrefix(ser.InputStream, True, ser.OutputStream, "AStream")
'End If
'Timer1.Enabled = True
connected = True
'TextWriter1.WriteLine("$")
'TextWriter1.Flush
Else
connected = False
'Timer1.Enabled = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub