'Service module
Sub Process_Globals
Dim Serial1 As Serial
Dim BluetoothAdmin1 As BluetoothAdmin
Dim AStreams As AsyncStreams
Dim Notification1 As Notification
End Sub
Sub Service_Create
Serial1.Initialize("Serial1")
BluetoothAdmin1.Initialize("BluetoothAdmin1")
Notification1.Initialize
Notification1.Icon = "icon" 'use the application icon file for the notification
Notification1.Light = False
Notification1.Sound = False
Notification1.Vibrate = False
Notification1.SetInfo("xxx", "Disconnected to xxx", Main)
'Make sure that the process is not killed during the download
Service.StartForeground(1, Notification1)
End Sub
Sub Service_Start (StartingIntent As Intent)
Dim BluetoothConnectionStatus, BluetoothConnectionID As String
StartServiceAt("", DateTime.Now + 30 * DateTime.TicksPerSecond, True)
BluetoothConnectionStatus = StartingIntent
BluetoothConnectionID = StartingIntent.ExtrasToString
' If BluetoothConnectionStatus.Contains("ACL_CONNECTED") AND BluetoothConnectionID.Contains(Main.DeviceID) Then
' Notification1.SetInfo("xxx", "Connected to Cruise", Main)
' Notification1.Notify(1)
' End If
If BluetoothConnectionStatus.Contains("ACL_DISCONNECTED") AND BluetoothConnectionID.Contains(Main.DeviceID) Then
Notification1.SetInfo("xxx", "Disconnected to xxx", Main)
Notification1.Notify(1)
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub
Sub Service_Destroy
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
Main.RecievedText = Main.RecievedText & msg
Log(Main.RecievedText)
'ToastMessageShow(Main.RecievedText, False)
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
Log(LastException.Message)
If LastException.Message = "java.io.IOException: Broken pipe" OR LastException.Message = "java.io.IOException: Connection reset by peer" OR LastException.Message = "java.io.IOException: Software caused connection abort" Then
Serial1.Disconnect
Main.connected = False
ClearRecievedData
End If
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success Then
BluetoothConnected
BluetoothSend("*")
Else
Main.connected = False
ToastMessageShow(Main.mtconnectionerror, False)
End If
End Sub
Sub BluetoothSend(dataString As String)
Dim buffer() As Byte
' Is Bluetooth connected
If AStreams.IsInitialized = False Then
ToastMessageShow(Main.mtnoconnedtiontoecruise, False)
Return
End If
Log(dataString)
' Send data to stream
buffer = dataString.GetBytes("ISO-8859-1")
AStreams.Write(buffer)
End Sub
Sub BluetoothConnected
ToastMessageShow(Main.mtconnected, False)
AStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStreams")
Notification1.SetInfo("xxx", "Connected to xxx", Main)
Notification1.Notify(1)
Main.connected = True
End Sub
Sub BluetoothAdmin1_StateChanged (NewState As Int, OldState As Int)
Dim i As Int
If (NewState = BluetoothAdmin1.STATE_OFF) Then
Serial1.Disconnect
Main.connected = False
ClearRecievedData
End If
End Sub
Sub ClearRecievedData
For i = 1 To 125
Main.RecievedData(i) = ""
Next
End Sub