#Region Project Attributes
#ApplicationLabel: Walkie Talkie
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Private admin As BluetoothAdmin
Private btDevices As List
Type NameAndMac (name As String, mac As String)
Private searchInProgress As Boolean
Private ip As String
Dim mf As flash1
End Sub
Sub Globals
Dim btnConnectWifi As Button
Dim lblIP As Label
Dim spnrPairedDevices As Spinner
Dim edtIP As EditText
Dim btnConnectBT As Button
Dim lblWifiStatus As Label
Dim lblBTStatus As Label
Dim IME As IME
Dim btnMakeDiscoverable As Button
Dim btnBTSearch As Button
Dim lblPTT As Label
Private Button1 As Button
Private Button2 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
StartService(Connector)
btDevices.Initialize
admin.Initialize("Admin")
'add the already paired devices to the list
'later the user can search for new devices
Dim serial1 As Serial
serial1.Initialize("")
Dim pairedDevices As Map = serial1.GetPairedDevices
For i = 0 To pairedDevices.Size - 1
Dim nm As NameAndMac
nm.Initialize
nm.mac = pairedDevices.GetValueAt(i)
nm.name = pairedDevices.GetKeyAt(i)
btDevices.Add(nm)
Next
End If
IME.Initialize("")
Activity.LoadLayout("1")
Activity.AddMenuItem("Disconnect", "mnuDisconnect")
'filter for ip address (allow multiple dots)
IME.SetCustomFilter(edtIP, edtIP.INPUT_TYPE_DECIMAL_NUMBERS, "0123456789.")
End Sub
Sub Activity_Resume
edtIP.Text = ip
spnrPairedDevices.Clear
For Each nm As NameAndMac In btDevices
spnrPairedDevices.Add(nm.name)
Next
'try to turn on Bluetooth if it is disabled
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
End If
UpdateUI
End Sub
Sub mnuDisconnect_Click
CallSub(Connector, "Disconnect")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
'save the ip address
ip = edtIP.Text
End Sub
Sub Admin_StateChanged (NewState As Int, OldState As Int)
UpdateUI
End Sub
Public Sub UpdateUI
'this sub is responsible for updating the UI based on the state which is stored in the service
Dim wifi = True, bt = True, discover = True As Boolean
If admin.IsEnabled = False Then
bt = False
discover = False
End If
lblIP.Text = Connector.MyIP
lblWifiStatus.Text = Connector.WifiStatus
lblBTStatus.Text = Connector.BTStatus
If Connector.WifiConnected Then lblWifiStatus.TextColor = Colors.Green Else lblWifiStatus.TextColor = Colors.Red
If Connector.BTConnected Then lblBTStatus.TextColor = Colors.Green Else lblBTStatus.TextColor = Colors.Red
If spnrPairedDevices.Size = 0 Then bt = False
If Connector.BTConnected Or Connector.WifiConnected Then
wifi = False
bt = False
discover = False
End If
If wifi And Regex.IsMatch("[^.]+\.[^.]+\.[^.]+\.[^.]+", edtIP.Text) = False Then
wifi = False
End If
If Connector.MyIP.Length = 0 Or Connector.MyIP = "127.0.0.1" Then
wifi = False
End If
If searchInProgress Then bt = False
btnConnectBT.Enabled = bt
btnConnectWifi.Enabled = wifi
btnBTSearch.Enabled = discover
btnMakeDiscoverable.Enabled = discover
lblPTT.Visible = Connector.BTConnected Or Connector.WifiConnected
End Sub
Sub btnConnectWifi_Click
CallSubDelayed2(Connector, "ConnectWifi", edtIP.Text)
End Sub
Sub btnConnectBT_Click
If spnrPairedDevices.SelectedIndex = -1 Then Return
Dim nm As NameAndMac = btDevices.Get(spnrPairedDevices.SelectedIndex)
CallSubDelayed2(Connector, "ConnectBT", nm.mac)
End Sub
'starts a search process
Sub btnBTSearch_Click
spnrPairedDevices.Clear
btDevices.Clear
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
searchInProgress = True
SetBTStatus("Searching for BT devices...")
UpdateUI
End If
End Sub
Sub SetBTStatus(status As String)
Connector.BTstatus = status
lblBTStatus.Text = status
End Sub
Sub Admin_DiscoveryFinished
searchInProgress = False
If spnrPairedDevices.Size = 0 Then
SetBTStatus("No BT devices found.")
Else
SetBTStatus(spnrPairedDevices.Size & " device(s) found.")
End If
UpdateUI
End Sub
Sub Admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
spnrPairedDevices.Add(Name)
Dim nm As NameAndMac
nm.Initialize
nm.Name = Name
nm.mac = MacAddress
btDevices.Add(nm)
SetBTStatus("Searching for devices (" & btDevices.Size & " found)")
End Sub
Sub btnMakeDiscoverable_Click
'this intent makes the device discoverable for 300 seconds.
Dim i As Intent
i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
StartActivity(i)
End Sub
Sub edtIP_TextChanged (Old As String, New As String)
UpdateUI 'the btnConnectWifi.Enabled depends on the text
End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
If Connector.BTConnected = False And Connector.WifiConnected = False Then Return
If Action = Activity.ACTION_DOWN Then
Activity.Color = Colors.Red
CallSub(Connector, "SendAudio")
Else If Action = Activity.ACTION_UP Then
Activity.Color = Colors.Black
CallSub(Connector, "StopSendingAudio")
End If
End Sub
Sub Button1_Click
CallSub(Connector,"torciaon")
End Sub
Sub Button2_Click
CallSub(Connector,"torciaoff")
End Sub