Sub Service_Create
Dim n As Notification
n.Initialize
n.SetInfo("TempTester", "Hermosa Temp Tester", Main)
Service.StartForeground(1, n)
admin.Initialize("admin")
serial1.Initialize("serial1")
foundDevices.Initialize
Dim i As Intent
i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
StartActivity(i)
ph.Initialize("ph")
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
serial1.Listen
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy
End Sub
Sub StartDiscovery
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
''ProgressDialogShow("Searching for devices...")
CallSubDelayed(Main, "showDevices")
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
CallSubDelayed3(Main, "ShowDevicesFound", l, foundDevices)
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))
CallSubDelayed2(Main, "AddDevices", "Searching for devices ( ~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub Connect(mac As String)
serial1.Connect(mac)
End Sub
Sub astream_NewData (Buffer() As Byte)
End Sub
Sub astream_Terminated
LogColor("astreams terminated", Colors.Red)
End Sub
Sub astream_Error
LogColor("astreams error", Colors.Red)
astream.Close
serial1.Disconnect
connected = False
End Sub
Sub serial1_Connected (Success As Boolean)
LogColor("connection succesful: " & Success, Colors.Blue)
If Success Then
connected = True
If astream.IsInitialized = False Then
astream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
End If
ProgressDialogHide
Else
ProgressDialogHide
LogColor("connection failed", Colors.Red)
End If
End Sub