Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private btnSearchForDevices As B4XView
Private btnAllowConnection As B4XView
Private rp As RuntimePermissions
Private AStream As AsyncStreams
Private serial As Serial
Private admin As BluetoothAdmin
Type NameAndMac (Name As String, Mac As String)
Public BluetoothState, ConnectionState As Boolean
Private ChatPage1 As ChatPage
Private AnotherProgressBar1 As B4XView
Private CLV As CustomListView
Private phone As Phone
Private ImgLogo As B4XView
Private ImgList As B4XView
Dim PacketBytes(27) As Byte ' 3-byte header "2BC" plus 8 x 3-byte space and 2-digit hex values
Dim NumPacketBytes As Int = 0
End Sub
'You can add more parameters here.
Public Sub Initialize
admin.Initialize("admin")
serial.Initialize("serial")
If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
Else
ToastMessageShow("Enabling Bluetooth adapter...", False)
End If
Else
BluetoothState = True
End If
sb.Initialize
ChatPage1.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("main")
AnotherProgressBar1.Tag = 1
B4XPages.AddPageAndCreate("Chat Page", ChatPage1)
End Sub
Private Sub Admin_StateChanged (NewState As Int, OldState As Int)
Log("state changed: " & NewState)
BluetoothState = NewState = admin.STATE_ON
StateChanged
End Sub
Sub btnSearchForDevices_Click
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)
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)
Return
End If
Next
End If
Dim success As Boolean = admin.StartDiscovery
If success = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
CLV.Clear
Dim Index As Int = ShowProgress
Wait For Admin_DiscoveryFinished
HideProgress(Index)
If CLV.Size = 0 Then
ToastMessageShow("No device found.", True)
End If
End If
End Sub
Sub btnAllowConnection_Click
If phone.SdkVersion >= 31 Then
rp.CheckAndRequest("android.permission.BLUETOOTH_CONNECT")
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission...", False)
Return
End If
End If
'this intent makes the device discoverable for 300 seconds.
Dim i As Intent
i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
StartActivity(i)
serial.Listen
End Sub
Public Sub SendMessage (msg As String)
AStream.Write(msg.GetBytes("utf8"))
End Sub
Private Sub AStream_NewData (Buffer() As Byte)
For I = 0 To Buffer.Length - 1 'process each received byte, one by one
Dim Ch As Byte = Buffer(I)
PacketBytes(NumPacketBytes) = Ch 'NumPacketBytes should always be less than 27 at this point,
NumPacketBytes = NumPacketBytes + 1 'since when it reaches 27 here, it is then reset back to 0 after the packet is handled below
Select Case NumPacketBytes
Case 1:
If Ch <> 50 Then '2'
NumPacketBytes = 0
End If
Case 2:
If Ch <> 66 Then 'B'
NumPacketBytes = 0
End If
Case 3:
If Ch <> 67 Then 'C'
NumPacketBytes = 0
End If
Case PacketBytes.Length:
HandlePacket(PacketBytes)
NumPacketBytes = 0
End Select
Next
End Sub
Sub HandlePacket(Pkt() As Byte)
Dim msg As String
msg = BytesToString(Pkt, 0, Pkt.Length, "UTF8")
ChatPage1.NewMessage(msg)
'Log(msg)
End Sub
Private Sub AStream_Error
ToastMessageShow("Connection is broken.", True)
ConnectionState = False
StateChanged
End Sub
Private Sub AStream_Terminated
AStream_Error
End Sub
Public Sub Disconnect
If AStream.IsInitialized Then AStream.Close
serial.Disconnect
End Sub
Private Sub Admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
Dim nm As NameAndMac = CreateNameAndMac(Name, MacAddress)
CLV.AddTextItem($"${nm.Name}: ${nm.Mac}"$, nm)
End Sub
Private Sub StateChanged
btnSearchForDevices.Enabled = BluetoothState
btnAllowConnection.Enabled = BluetoothState
ChatPage1.btnSend.Enabled = ConnectionState
End Sub
Sub CLV_ItemClick (Index As Int, Value As Object)
Dim nm As NameAndMac = Value
ToastMessageShow($"Trying to connect to: ${nm.Name}"$, True)
serial.Connect(nm.Mac)
Dim Index As Int = ShowProgress
Wait For Serial_Connected (Success As Boolean)
HideProgress(Index)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
Else
AfterSuccessfulConnection
End If
StateChanged
End Sub
'will be called when a client connects to this device
Sub Serial_Connected (Success As Boolean)
If Success Then
AfterSuccessfulConnection
StateChanged
End If
End Sub
Sub AfterSuccessfulConnection
If AStream.IsInitialized Then AStream.Close
'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
'AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
AStream.Initialize(serial.InputStream, serial.OutputStream, "astream")
ConnectionState = True
B4XPages.ShowPage("Chat Page")
End Sub
Sub ShowProgress As Int
AnotherProgressBar1.Tag = AnotherProgressBar1.Tag + 1
AnotherProgressBar1.Visible = True
Return AnotherProgressBar1.Tag
End Sub
Sub HideProgress (Index As Int)
If Index = AnotherProgressBar1.Tag Then
AnotherProgressBar1.Visible = False
End If
End Sub
Public Sub CreateNameAndMac (Name As String, Mac As String) As NameAndMac
Dim t1 As NameAndMac
t1.Initialize
t1.Name = Name
t1.Mac = Mac
Return t1
End Sub
Private Sub B4XPage_Appear
CLV.Clear
End Sub