Public Sub Initialize
InitialiseSerialPort
GetPairedMACAddress
If Port.IsEnabled Then
Log("Bluetooth enabled")
If PairedMACAddress = "" Then
ListPairedDevices
End If
Try
If ConnectToPairedDevice Then
' This is where things start.
End If
Catch
Log("***** ERROR connecting to device *****")
End Try
Else
Log("Bluetooth not enabled")
ToastMessageShow("Enable Bluetooth and restart application. Closing application.", True)
End If
End Sub
Private Sub InitialiseSerialPort As Boolean
' Initialise the Serial port
Try
Port.Initialize("Port")
Queue.Initialize
Log("Serial port Initialised")
Return True
Catch
Log("Error initialising Serial port")
Return False
End Try
End Sub
Private Sub GetPairedMACAddress
PairedMACAddress = StateManager.GetSetting("PairedMACAddress")
Log("Retrieved the following pairing: " & PairedMACAddress)
End Sub
Private Sub Stream_NewData(Buffer() As Byte)
Private NewData As String = BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1")
Private i As Int = 0
Log("Received string:" & NewData)
For i = 0 To NewData.Length-1
Queue.Add(NewData.SubString2(i,i))
Next
End Sub
Private Sub Stream_Error
Log("Stream error")
End Sub
Private Sub Stream_Terminated
Log("Stream terminated")
'Stream_Error
End Sub
Private Sub Port_Connected(Success As Boolean)
If Success = True Then
If Stream.IsInitialized = False Then
If InitialiseStream Then
' Do the next thing here
End If
End If
End If
End Sub
Private Sub InitialiseStream As Boolean
Try
Stream.Initialize(Port.InputStream, Port.OutputStream, "Stream")
Log("Stream initialised")
Return True
Catch
Log("** Error initialising stream")
Return False
End Try
End Sub
Private Sub WriteToSerialPort(Data As Int)
Dim StringToSend As String = Chr(Data)
Dim DataToSend() As Byte = StringToSend.GetBytes("ISO8859_1")
Stream.Write(DataToSend)
Log("-----------Sent->" & Data )
End Sub
Private Sub ListPairedDevices
' Now to determine what MAC address to connect to
Private PairedDevices As Map = Port.GetPairedDevices
Private OnscreenList As List
Private UserResponse As Int
Log("Listing paired devices")
OnscreenList.Initialize
For I = 0 To PairedDevices.Size - 1
OnscreenList.Add(PairedDevices.GetKeyAt(I))
Next
UserResponse = InputList(OnscreenList, "Choose device", -1) 'show list with paired devices
If UserResponse <> DialogResponse.CANCEL Then
PairedMACAddress = PairedDevices.Get(OnscreenList.Get(UserResponse)) ' Convert the name to mac address
Log("Paired MAC Address (" & PairedMACAddress & ") being saved...")
StateManager.SetSetting("PairedMACAddress",PairedMACAddress)
StateManager.SaveSettings
End If
End Sub
Private Sub ConnectToPairedDevice As Boolean
If PairedMACAddress <> "" Then
Try
Port.Connect(PairedMACAddress)
BluetoothConnected = True
ToastMessageShow("Bluetooth connected.", False)
Log("Bluetooth connected")
If InitialiseSerialPort Then
' The remainder of the connection occurs after the Serial Port connection event fires. See Port_Connected()
Return True
Else
Return False
End If
Catch
BluetoothConnected = False
ToastMessageShow("Bluetooth connection failure.", False)
Log("Bluetooth connection failure")
Return False
End Try
End If
End Sub