#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 TT As Timer
End Sub
Sub Service_Create
TT.Initialize("TT",1000)
MyDeviceName = "ALCATEL ONE TOUCH POP7" 'Put the name of your coupled device here!
'MyDeviceName = "BlueTooth Printer"
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="Watch Application"
Notification1.SetInfo(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_StateChanged(NewState As Int,OldState As Int)
Try
If NewState = BT.STATE_ON Then
BTConnectToDevice
Log("BT Connecting")
Else
Serial1.Disconnect
btConnected = False
Log("BT Disconnecting")
End If
Catch
Log(LastException.Message)
End Try
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)
Try
If Success = True Then
ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
If ast.IsInitialized = False Then
ast.Initialize(Me, "ast", Serial1.InputStream, Serial1.OutputStream)
End If
Log("BT Connected")
SendCommand("OPEN DOOR")
Log("Sent")
ToastMessageShow(""&btConnected,True)
Else 'disconnected
ToastMessageShow("Connection to " & Serial1.Address &" broken!", True)
btConnected = False
ToastMessageShow(""&btConnected,True)
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 TT_Tick
End Sub
Sub ast_Error
Try
ToastMessageShow("Connection is broken.", True)
If btConnected = False Then
Log(DateTime.Time(DateTime.Now)&": Service_Start")
Dim r As Int = Rnd(100,200)
Log(DateTime.Time(DateTime.Now)&": random="&r)
StartServiceAt("", DateTime.Now + 2 * 100, False)
'StopService("")
Else
Return
End If
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub