personalapps
Member
I am trying to connect to ESP32 C3 using the following class module. However I am not able to connect. can anyone please explain why it is not connecting? The relevant log is:
The relevant b4xpage code is:
StateChanged: 12
startLeScan(): [4fafc201-1fb5-459e-8fcc-c5c9c331914b]
isLeEnabled(): ON
onScannerRegistered() - status=0 scannerId=10 mScannerId=0
false 'Log(connected) is false
B4X:
Sub Class_Globals
#if B4A
Private manager As BleManager2
#else if B4I
Private manager As BleManager
#end if
Private bc As ByteConverter
Private LEADERSHIP_SERVICE As String = "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
Private LEADERSHIP_CHARACTERISTICS As String = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
Private firstRead As Boolean
Public connected As Boolean
End Sub
Public Sub Initialize
LEADERSHIP_SERVICE = LEADERSHIP_SERVICE
LEADERSHIP_CHARACTERISTICS= LEADERSHIP_CHARACTERISTICS
manager.Initialize("manager")
End Sub
Private Sub Manager_StateChanged (State As Int)
Log($"StateChanged: ${State}"$)
If State = manager.STATE_POWERED_ON Then
manager.Scan(Array(LEADERSHIP_SERVICE))
End If
End Sub
Private Sub Manager_DeviceFound (Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)
Log($"****************
Name: ${Name} RSSI: ${RSSI}
DeviceId: ${DeviceId}"$)
If Name.ToLowerCase.Contains("leadership") Then
'manager.StopScan
Log($"Connecting to: ${DeviceId}"$)
Try
manager.Connect(DeviceId)
Catch
Log(LastException)
End Try
End If
End Sub
Private Sub Manager_Connected (Services As List)
Log("Connected")
firstRead = True
manager.ReadData(LEADERSHIP_SERVICE)
End Sub
Private Sub Manager_Disconnected
Log("Disconnected")
connected=False
End Sub
Private Sub Manager_DataAvailable (serviceId As String, Characteristics As Map)
Log($"Data is available:${Characteristics}"$)
If firstRead Then
firstRead = False
manager.SetNotify(LEADERSHIP_SERVICE, LEADERSHIP_CHARACTERISTICS, True)
Return
End If
End Sub
Private Sub SendMsg(msg As String)
Log(msg)
Dim b() As Byte = msg.GetBytes("UTF8") 'convert to bytes
Try
manager.WriteData(LEADERSHIP_SERVICE, LEADERSHIP_CHARACTERISTICS, b)
Catch
Log(LastException)
End Try
End Sub
Public Sub SendMessage(m As String)
Log(connected)
Log(m)
If Not(connected) Then Return
Log(m)
SendMsg(m)
End Sub
The relevant b4xpage code is:
B4X:
Private Sub btnScan_Click
#if B4A
'Don't forget to add permission to manifest
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 B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission: " & Permission, True)
Return
End If
Next
#end if
startScan
End Sub
Private Sub startScan
em.Initialize ' em is class module
End Sub
Private Sub btn_color_Click
Dim randomNum As Int
randomNum = Rnd(0, 3)
Select Case randomNum
Case 0
em.SendMessage("G") ' Send Green
Case 1
em.SendMessage("R") ' Send Red
Case 2
em.SendMessage("B") ' Send Blue
End Select
End Sub