Sub Process_Globals
Dim BtAdmin As BluetoothAdmin
Dim BtSerial As Serial
Dim Broadcast As BroadCastReceiver
Dim AStreams As AsyncStreams
Dim DeviceName As String = ""
Dim DeviceMac As String = ""
End Sub
Sub Globals
Dim btnStartDiscovery As Button
End Sub
'Activity
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
BtAdmin.Initialize("BtAdmin")
BtAdmin.Enable
BtSerial.Initialize("BtSerial")
Broadcast_Init
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnStartDiscovery_Click
DeviceName = ""
DeviceMac = ""
BtAdmin.StartDiscovery
Log("Start discovery")
End Sub
'BtAdmin
Sub BtAdmin_DiscoveryFinished 'Event handler
Log("Discovery stopped")
End Sub
Sub BtAdmin_StateChanged (NewState As Int, OldState As Int) 'Event handler
If (NewState =BtAdmin.STATE_OFF) Then
BtAdmin.Enable
End If
End Sub
Sub BtAdmin_DeviceFound (Name As String, Mac As String) 'Event handler
BtAdmin.CancelDiscovery
DeviceName = Name
DeviceMac = Mac
Log("Device found: " & DeviceName & ", " & DeviceMac)
BtSerial.Connect(DeviceMac)
End Sub
Sub BtSerial_Connected (Success As Boolean) ' Event handler
If (Success=True) Then
If AStreams.IsInitialized = False Then AStreams.Initialize(BtSerial.InputStream, BtSerial.OutputStream, "Astreams")
Log("Connected")
If AStreams.IsInitialized = True Then
Dim buffer() As Byte
Dim sendchar As String = "a"
buffer = sendchar.GetBytes("UTF8")
AStreams.Write(buffer)
End If
Else
Log("Not Connected: " & LastException.Message)
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log(msg)
End Sub
Sub AStreams_Error
Log(LastException.Message)
ToastMessageShow(LastException.Message, True)
End Sub
Sub AStreams_Terminated
Log("end")
End Sub
'Broadcast
Sub Broadcast_Init
Broadcast.Initialize("BroadCastReceiver")
Broadcast.addAction("android.bluetooth.device.action.PAIRING_REQUEST")
Broadcast.SetPriority(2147483647)
Broadcast.registerReceiver("")
End Sub
Sub BroadCastReceiver_OnReceive (Action As String, i As Object) 'Event handler
Dim intnt As Intent = i
Broadcast.AbortBroadcast
Dim autopair As BtAutoPair
Dim pinBytes() As Byte
If (Action = "android.bluetooth.device.action.PAIRING_REQUEST") Then
If (intnt.HasExtra("android.bluetooth.device.extra.DEVICE")) Then
Log("Pairing request: " & DeviceName & ", " & DeviceMac)
pinBytes = Array As Byte(0x31, 0x32, 0x33, 0x34)'1234
autopair.Start4(intnt, pinBytes)
End If
End If
End Sub