Android Question BLE2 Cant Connect

fbritop

Well-Known Member
Licensed User
Longtime User
I´m using BLE2, it aws working fine, but on some update I think I miss something.

Is scans an returns the corrects MACs for the devices, when I try to connect to the one I'm interested, it just starts connecting and disconnects (never the connected state is called).

I was using an android app BLE Scanner, which correcty connects to the device, and I can see the device logs, that BLE is advertised on the app, but with B4A it never connects. I´m using the latest example from BLE2.

Is there something that I´m missing?
My device when I use BLE Scanner App throughs out this log, but with B4A nothing of this happens

1668117009442.png
 
it could be the new permissions, the BLE example also didn't work for me until founding this:

new permissions in the manifest editor:
B4X: Copy
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
 
Upvote 0
it could be the new permissions, the BLE example also didn't work for me until founding this:

new permissions in the manifest editor:
B4X: Copy
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
Thanks
it did not work, still pushing!
 
Upvote 0
This is my modified version
I did a quick try and I can connect to my hardware BLE, but I had commented a couple lines, then I can't tell you where is the issue
Edit: If I don't comment those lines, I can't connect to my hardware.

B4X: Copy
Sub btnScan_Click
    #if B4A
    'Don't forget to add permission to manifest
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission", True)
        Return
    End If
'    rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
'    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
'    If Result = False Then
'        ToastMessageShow("No permission", True)
'        Return
'    End If
   
'    Dim phone As Phone
'    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
    #end if
    pbScan.Show
    StartScan
End Sub
B4X: Copy
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
'    If Name.ToUpperCase.StartsWith("BLELM_10075") Then
'    If Id = "6D:D4:F2:0C:A4:74" Then
        '24:6f:28:aa:39:86
        Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
        ConnectedName = Name
        'Dim data1() As Byte = AdvertisingData.Get(9)
        'Dim data2() As Byte = AdvertisingData.Get("0")
        'you can use byte converter to convert to hex if you like:
        ''Dim bc As ByteConverter
        'Log(bc.HexFromBytes(data1))
        'Log(bc.HexFromBytes(data2))
    manager.StopScan
   
        #if B4A
        Log("connecting")
        manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
    'manager.Connect(Id) 'disabling auto connect can make the connection quicker
    #else if B4I
    manager.Connect(Id)
    #end if
'    End If
End Sub
 
Upvote 0
Thanks but it did not work
If it helps, I´m trying to connect to an ESP32 that runs under Mongoose-OS
 
Upvote 0
If I try to uncomment those lines in your example, I also got this: "it just starts connecting and disconnects", then I think the issue is about permissions.

Can you try the example:

If I try the example it work for me.
I updated:

B4X: Copy
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
 
Upvote 0
If I try to uncomment those lines in your example, I also got this: "it just starts connecting and disconnects", then I think the issue is about permissions.

Can you try the example:

If I try the example it work for me.
I updated:

B4X: Copy
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
The permissions where already added in my project, but I still have no clue why it does not connect
 
Upvote 0
I did found finally a solution, which relies on BLE3, a modified version.
It is because of the transport issue.
Here is the link to the original post


Maybe @Erel could include this function (connect3) to the standard BLE library.

Cheers and thanks to everyone
 
Upvote 0
Back
Top