#Region Service Attributes
#StartAtBoot: False
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
Sub Process_Globals
Dim BT As BluetoothAdmin
Dim Serial1 As Serial
Dim btConnected As Boolean
Private ast As AsyncStreamsText
Dim MyDeviceName As String
Dim Notification1 As Notification
Dim ServiceMessage As String
Dim foundDevices As List
Type NameAndMaco (Name As String, Mac As String)
Dim connectedDevice As NameAndMaco
Dim Tim As Timer
End Sub
Sub FindDevices
Log("FindDevices")
foundDevices.Initialize
If BT.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
Log("Searching for devices...")
End If
End Sub
Sub Tim_Tick
FindDevices
End Sub
Sub Service_Create
foundDevices.Initialize
Tim.Initialize("Tim",10000)
Tim.Enabled = False
MyDeviceName = "ALCATEL ONE TOUCH POP7" 'Put the name of your coupled device here!
ToastMessageShow("Trying to connect to " & MyDeviceName, True)
Try
BT.Initialize("BT")
Serial1.Initialize("Serial1")
Catch
ToastMessageShow("No BlueTooth Device visible...", True)
End Try
'Start Bluetooth
Try
If BT.IsEnabled = False Then
BT.Enable
Else
'connect to device
BTConnectToDevice
End If
Catch
Log(LastException.Message)
End Try
Notification1.Initialize
Notification1.Icon = "icon" 'use the application icon file for the notification
End Sub
Sub Service_Start (StartingIntent As Intent)
ServiceMessage="Door Service"
Notification1.SetInfo(ServiceMessage,ServiceMessage & " Is Running", Main)
Notification1.Sound = False
'Make sure that the process is not killed during the download
'This is important if the download is expected to be long.
'This will also show the status bar notification
Service.StartForeground(1, Notification1)
Log("service running")
End Sub
Sub Service_Destroy
End Sub
Sub BT_DiscoveryFinished
Log("In Admin_DiscoveryFinished")
If foundDevices.Size < 1 Then
Log("No device found.")
Else
Dim l As List
Dim Mac As List
l.Initialize
Mac.Initialize
For i = 0 To foundDevices.Size - 1
Dim nm As NameAndMaco
nm = foundDevices.Get(i)
l.Add(nm.Name)
Mac.Add(nm.Mac)
Next
End If
Tim.Enabled = False
End Sub
Sub BT_DeviceFound (Name As String, MacAddress As String)
Log("Admin_DeviceFound")
Log(Name & ":" & MacAddress)
Dim nm As NameAndMaco
nm.Name = Name
nm.Mac = MacAddress
foundDevices.Add(nm)
If foundDevices.Size = 0 Then
Log("No Device Found")
Else
Log("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
BTConnectToDevice
Tim.Enabled = False
Log("Devices Found")
End If
End Sub
Sub BT_StateChanged(NewState As Int,OldState As Int)
Log("BT_StateChanged")
If NewState = BT.STATE_ON Then
Log("BT Enabled")
Else
Serial1.Disconnect
'timBT.Enabled = False
Log("BT Not Enabled")
If IsPaused(Main) Then
Notification1.Sound = True
Notification1.SetInfo("Bluetooth Did Not Connect to " & Serial1.Address, " broken!", Main)
Notification1.AutoCancel = True
Notification1.Notify(1)
End If
End If
End Sub
Sub BTConnectToDevice
Log("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)
If Success = True Then
ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
If ast.IsInitialized = False Then
ast.Initialize(Me, "ast", Serial1.InputStream, Serial1.OutputStream)
Tim.Enabled = False
End If
'timBT.Enabled = True
Log("BT Connected")
'Service.StopForeground(1) 'Return the service to the "background" (also removes the ongoing notification)
If IsPaused(Main) Then
Notification1.Sound = True
Notification1.SetInfo("Bluetooth connected to " & Serial1.Address, "Done Successfully", Main)
Notification1.AutoCancel = True
Notification1.Notify(1)
CallSub(Main,"UpdateUi")
End If
Else 'disconnected
ToastMessageShow("Connection to " & Serial1.Address &" broken!", True)
If IsPaused(Main) Then
Notification1.Sound = True
Notification1.SetInfo("Bluetooth Did Not Connect to " & Serial1.Address, " broken!", Main)
Notification1.AutoCancel = True
Notification1.Notify(1)
End If
btConnected = False
Tim.Enabled = True
End If
End Sub
Sub ast_Error
Try
ToastMessageShow("Connection is broken.", True)
If IsPaused(Main) Then
Notification1.Sound = True
Notification1.SetInfo("Bluetooth Did Not Connect to " & Serial1.Address, " broken!", Main)
Notification1.AutoCancel = True
Notification1.Notify(1)
End If
If btConnected = False Then
Tim.Enabled = True
Else
End If
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub
Sub ast_Terminated
Try
ToastMessageShow("Connection is broken.", True)
ast_Error
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub
Sub ast_NewText(Text As String)
Log("Text: " & Text)
Log(Text.Length)
End Sub