'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim BT As BluetoothAdmin
Dim Serial1 As Serial
Dim btConnected As Boolean
Dim timBT As Timer
Dim AStreams 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.
Dim MyDeviceName As String
Dim lblBT As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout(1)
MyDeviceName = "EaSS-V040305BT" 'Put the name of your coupled device here!
ToastMessageShow("Trying to connect to " & MyDeviceName, True)
timBT.Initialize("timBT",5000)
timBT.Enabled = False
Try
BT.Initialize("BT")
Serial1.Initialize("Serial1")
Catch
ToastMessageShow("No BlueTooth Device visible...", True)
End Try
End Sub
Sub Activity_Resume
'Start Bluetooth
Try
If BT.IsEnabled = False Then
lblBT.Color = Colors.Black
BT.Enable
Else
'connect to device
BTConnectToDevice
End If
Catch
End Try
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub BT_StateChanged(NewState As Int,OldState As Int)
If NewState = BT.STATE_ON Then
BTConnectToDevice
Log("BT Connect")
Else
Serial1.Disconnect
timBT.Enabled = False
Log("BT Disconnect")
End If
End Sub
Sub BTConnectToDevice
Dim PairedDevices As Map
PairedDevices = Serial1.GetPairedDevices
Try
Serial1.Connect3(PairedDevices.Get(MyDeviceName),1)
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub
Sub Serial1_Connected (Success As Boolean)
Dim msg As String
If Success = True Then
ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
AStreams.Initialize(Serial1.InputStream,Serial1.OutputStream,"AStreams")
timBT.Enabled = True
Else 'disconnected
ToastMessageShow("Connection to " & Serial1.Address & _
" broken!", True)
timBT.Enabled = False
btConnected = False
End If
End Sub
Sub timBT_Tick
'Communicte with the device here
'if the device answers, fine
'if there is no response, communication might be lost
'Stop the timer and you should call BTConnectToDevice again
End Sub