'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=BluetoothChat.zip
Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private btnSearchPermission As B4XView
Private btnAllowConnection As Button
Private btnPrint As Button
Private rp As RuntimePermissions
Private serial As Serial
Type NameAndMac (Name As String, Mac As String)
Private phone As Phone
Dim astreams1 As AsyncStreams
Dim PrintBuffer As String
'Dim Version As Double = 2.0 ' Demo program version
End Sub
'You can add more parameters here.
Public Sub Initialize
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
'load the layout to Root
Root.LoadLayout("1")
serial.Initialize("serial")
End Sub
' Just to check if something strange is happening
Sub astreams1_Error
Log("astreams1 ERROR")
End Sub
Sub astreams1_Terminated
Log("astreams1 TERMINATED")
End Sub
Private Sub PermissionResult As ResumableSub
Dim Permissions As List
Dim phone As Phone
If phone.SdkVersion >= 31 Then
Permissions = Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", rp.PERMISSION_ACCESS_FINE_LOCATION)
Else
Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
End If
For Each per As String In Permissions
rp.CheckAndRequest(per)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission: " & Permission, True)
Return False
End If
Next
Log("PERMISSION'S - OK")
' Return True
End Sub
Sub btnSearchPermission_Click
Wait For(PermissionResult) Complete(res As Boolean)
If res = False Then
ToastMessageShow("not permission", False)
ExitApplication
Return
End If
' rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
' Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
' If Result = False And rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
' 'ToastMessageShow("No permission...", False)
'
' Log("test 1")
' Return
' End If
' If phone.SdkVersion >= 31 Then
' For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
' rp.CheckAndRequest(Permission)
' Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
' If Result = False Then
' 'ToastMessageShow("No permission...", False)
'
' Log("test 1")
' Return
' End If
' Next
' End If
End Sub
Sub btnAllowConnection_Click
Log("test 1")
If phone.SdkVersion >= 31 Then
Log("test 2")
rp.CheckAndRequest("android.permission.BLUETOOTH_CONNECT")
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
'ToastMessageShow("No permission...", False)
Log("test 3")
Return
End If
End If
Log("test 4")
Dim PairedDevices As Map
PairedDevices = serial.GetPairedDevices
Dim l As List
Dim deviceName, macAddress As String
l.Initialize
For i = 0 To PairedDevices.Size - 1 'Check all devices
l.Add(PairedDevices.GetKeyAt(i))
deviceName=PairedDevices.Getkeyat(i)
macAddress=PairedDevices.GetValueAt(i)
'"DC:0D:30:39:5A:4D"
Log(deviceName & " -> " & macAddress)
If macAddress="00:19:5D:25:E6:8D" Then 'Insert the BT-Name of the printer or use the MAC address
serial.Connect(macAddress)
Log("Connecting to printer...")
Sleep(500)
Exit
End If
Next
End Sub
Sub btnPrint_Click
'
Dim cutting As String
cutting = Chr(29) & "V" & Chr(66)
Dim PrintBuffer As String
PrintBuffer = Chr(27) & "t" & Chr(16)
PrintBuffer = PrintBuffer & " HELLO WORLD" & CRLF ' Just change this text
Log("HELLO WORLD...")
astreams1.Write(PrintBuffer.GetBytes("UTF8"))
astreams1.Write(cutting.GetBytes("UTF8"))
astreams1.Write(Array As Byte(13, 10))
astreams1.Close
Return
End Sub
Sub serial_Connected (Success As Boolean)
If Success Then
astreams1.Initialize(Null,serial.OutputStream,"astreams1")
'ToastMessageShow("Printer OK!",True)
Log("Printer OK!...")
Else
'ToastMessageShow("Printer not found...",True)
Log("Printer OK!...")
End If
End Sub