Android Question unable to connect ESP32 C3 using BLE

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:
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
 
Solution
It looks like you have a mix of b4x and .ini code.
Update the .ini code to respond to 00 or 01
B4X:
if (value == "00") {
        digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green
        digitalWrite(RED_LED_PIN, LOW);  // Turn off red
        digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue

Add a button to that BLE example and try the following:
B4X:
Private Sub btnLedOFF_Click
    Dim ByteA As ByteConverter
    Dim a()...
Start with the BLE example: https://www.b4x.com/android/forum/threads/b4x-ble-2-bluetooth-low-energy.59937/#content

Are you able to connect?

Are you able to connect with a different BLE app?
Was able to connect both with BLE example and the class module after pressing 'reset' on the ESP32 C3 ! However the connection is immediately lost (disconnected). I am able to connect using nrf connect app and was able to switch on the Red Blue and Green LED by sending 'R', "B", "G" text. The log for class module is . Please let me know what to try next:
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
It has been said many times. ESP32 is a dual device Bluetooth and BLE. Erel's BLE2 library doesn't work with it. You can read about this issue here https://www.b4x.com/android/forum/threads/solved-ble-connection-problems.133142/
See https://www.b4x.com/android/forum/threads/ble2-library-additional-functions.133296/#content and find ble3.jar and ble3.xml.
Unfortunately Erel didn't want to add it to internal ble2 library - I could never understand his reason - so now there are to BLE libraries.
 
Upvote 0
Thank you for the BLE3 . I tried it but was unable to connect. I will explore it further later and report the log
 
Upvote 0
Which version of Android are you testing? Maybe it's a permissions issue or location off?
it works for me..check my demo here: Led ON/OFF
I am using ver 13. I request the permissions (please see 1st post of the thread). I am using the standard "BLE WRITE.ino" code in the ESP. Can you share your b4x code? I am able to connect and write to the RGB LED using nrf connect app.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I am using ver 13. I request the permissions (please see 1st post of the thread). I am using the standard "BLE WRITE.ino" code in the ESP. Can you share your b4x code? I am able to connect and write to the RGB LED using nrf connect app.
I'm using the example post #2
 
Upvote 0
Using the example in post #2 , I am able to connect (tried with ble 2 and ble 3) briefly after pressing 'reset' on ESP32 but I am not getting the write/read on tapping the "beb5483e-36e1-4688-b7f5-ea07361b26a8" characteristics to write . On the other hand, I have no problem with nrf connect.


 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Have you read post #1 in this thread Connect3(String DeviceId, boolean AutoConnect, int BtTransport) ?
Are you using Connect3(String DeviceId, boolean AutoConnect, int BtTransport) to connect when using Ble3 library?
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
It looks like you have a mix of b4x and .ini code.
Update the .ini code to respond to 00 or 01
B4X:
if (value == "00") {
        digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green
        digitalWrite(RED_LED_PIN, LOW);  // Turn off red
        digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue

Add a button to that BLE example and try the following:
B4X:
Private Sub btnLedOFF_Click
    Dim ByteA As ByteConverter
    Dim a() As Byte
    a=ByteA.HexToBytes("00")
    manager.WriteData("4fafc201-1fb5-459e-8fcc-c5c9c331914b", "beb5483e-36e1-4688-b7f5-ea07361b26a8", a)
    Log("Led ON/OFF ")
    
End Sub

Using the example in post #2 , I am able to connect (tried with ble 2 and ble 3) briefly after pressing 'reset' on ESP32
Yes, you need to do that to ensure the ESP32 disconnects from nRFApp (check my video)
 
Upvote 1
Solution
Thank you. Your code (I modified it slightly) worked in b4x example but I still have no idea why the original post about class doesn't work.

B4X:
Private Sub btn_random_Click
    If Not(connected) Then Return
    Dim randomNum As Int
    randomNum = Rnd(0, 4) ' Generates a random number between 0 and 3 (inclusive)
    Log(randomNum)
    Select Case randomNum
        Case 0
            sendColor("G") ' Send Green
        Case 1
            sendColor("R") ' Send Red
        Case 2
            sendColor("B") ' Send Blue
        Case Else
            sendColor("S") 'stop
    End Select
End Sub

Private Sub sendColor(msg As String)
    Dim a() As Byte
    a = msg.GetBytes("UTF-8")
    manager.WriteData("4fafc201-1fb5-459e-8fcc-c5c9c331914b", "beb5483e-36e1-4688-b7f5-ea07361b26a8", a)
    Log($"The ${msg} LED is on "$)
   
End Sub
 
Upvote 0
Just for completeness of this thread, note that in Arduino code, I forgot to add the service UUID to the advertisement which is why it didn't show up in the class when scanned. I have no idea how nrf app works even with the missing code, but after adding the following code, my class is able to discover the device:
C++:
pAdvertising = pServer->getAdvertising();
// Add service UUID to advertising data
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->start();
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…