Sub Class_Globals
Private AStream As AsyncStreams
Private serial As Serial
Private admin As BluetoothAdmin
Public foundDevices As List
Type NameAndMac (Name As String, Mac As String)
Public BluetoothState, ConnectionState As Boolean
Public Timer1 As Timer
End Sub
Public Sub Initialize
DateTime .TimeFormat = "hh:mm"
Timer1.Initialize("timer1",10000)
Timer1.Enabled = False
admin.Initialize("admin")
serial.Initialize("serial")
If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Fehler beim Verbinden des Bluetooth-Adapters.", True)
Else
ToastMessageShow("Verbinde Bluetooth-Adapter...", False)
End If
Else
BluetoothState = True
End If
End Sub
Private Sub Admin_StateChanged (NewState As Int, OldState As Int)
Log("Satus gewechselt: " & NewState)
BluetoothState = NewState = admin.STATE_ON
NotifyOfStateChanged
End Sub
Public Sub ConnectTo (Device As NameAndMac)
serial.Connect(Device.Mac)
End Sub
Private Sub Serial_Connected (Success As Boolean)
Log("Verbunden: " & Success)
CallSub2(Main, "AfterConnect", Success)
ConnectionState = Success
If Success = False Then
Log(LastException.Message)
CallSub2(ChatActivity,"NewMessage", DateTime.Time ( DateTime .Now) & " Verbindung unterbrochen...")
ToastMessageShow("Verbindungsfehler: " & LastException.Message, True)
Timer1.Enabled = True
Else
If AStream.IsInitialized Then AStream.Close
AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
StartActivity(ChatActivity)
CallSub2(ChatActivity,"NewMessage", DateTime.Time ( DateTime .Now) & " Verbindung hergestellt...")
ToastMessageShow("Verbindung zu " & Main.Name & " ist Aktiv...", True)
Timer1.Enabled = False
End If
NotifyOfStateChanged
End Sub
Public Sub SendMessage (msg As String)
AStream.Write(msg.GetBytes("utf8"))
End Sub
Private Sub AStream_NewData (Buffer() As Byte)
CallSub2(ChatActivity, "NewMessage", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
Private Sub AStream_Error
CallSub2(ChatActivity,"NewMessage", DateTime.Time ( DateTime .Now) & " Verbindung unterbrochen...")
ToastMessageShow("Verbindung zu " & Main.Name & " ist unterbrochen.", True)
Timer1.Enabled = True
ConnectionState = False
NotifyOfStateChanged
End Sub
Private Sub AStream_Terminated
AStream_Error
End Sub
Public Sub Disconnect
If AStream.IsInitialized Then AStream.Close
serial.Disconnect
End Sub
Public Sub SearchForDevices As Boolean
foundDevices.Initialize
Return admin.StartDiscovery
End Sub
Private Sub Admin_DiscoveryFinished
CallSub(Main, "DiscoverFinished")
End Sub
Private 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)
End Sub
Private Sub NotifyOfStateChanged
For Each Target In Array(Main, ChatActivity)
CallSub(Target, "UpdateState")
Next
End Sub
Sub Timer1_Tick
If ConnectionState = False Then
ToastMessageShow("Suche " & Main.Name , True)
Dim device As NameAndMac = foundDevices.Get(Main.Index1)
ConnectTo(device)
Else If ConnectionState = True Then
If AStream.IsInitialized Then AStream.Close
AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
StartActivity(ChatActivity)
End If
End Sub