'Activity module
Sub Process_Globals
Dim admin As BluetoothAdmin
Dim serial1 As Serial
Dim foundDevices As List
Type NameAndMac (Name As String, Mac As String)
Dim connectedDevice As NameAndMac
End Sub
Sub Globals
Dim btnSearchForDevices As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
admin.Initialize("admin")
serial1.Initialize("serial1")
End If
Activity.LoadLayout("1")
Activity.Title="BlueToothSwitch"
End Sub
Sub Activity_Resume
btnSearchForDevices.Enabled = False
If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
Else
ToastMessageShow("Enabling Bluetooth adapter...", False)
'the StateChanged event will be soon raised
End If
Else
admin_StateChanged(admin.STATE_ON, 0)
End If
End Sub
Sub admin_StateChanged (NewState As Int, OldState As Int)
btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed = True Then
serial1.Disconnect
End If
End Sub
Sub btnSearchForDevices_Click
foundDevices.Initialize
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
ProgressDialogShow("Searching for devices...")
End If
End Sub
Sub admin_DiscoveryFinished
ProgressDialogHide
If foundDevices.Size = 0 Then
ToastMessageShow("No device found.", True)
Else
Dim l As List
l.Initialize
For i = 0 To foundDevices.Size - 1
Dim nm As NameAndMac
nm = foundDevices.Get(i)
l.Add(nm.Name)
Next
Dim res As Int
res = InputList(l, "Choose device to connect", -1)
If res <> DialogResponse.CANCEL Then
connectedDevice = foundDevices.Get(res)
ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
serial1.Connect(connectedDevice.Mac)
End If
End If
End Sub
Sub admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
Dim nm As NameAndMac
nm.Name = Name
nm.Mac = MacAddress
foundDevices.Add(nm)
ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub Serial1_Connected (Success As Boolean)
Log("Serial Connected")
ProgressDialogHide
Log("connected: " & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
Else
StartActivity(ChatActivity)
End If
End Sub