Android Question [Solved] Need android.permission.BLUETOOTH_CONNECT permission

Mr Blue Sky

Active Member
Licensed User
Longtime User
Hello,
I don't understand since the Android 13 update on my phone my application no longer wants to work, I have this error message and can't find the solution?
I think it's a permission problem of the nearby device.

B4X:
b4xmainpage_initialize (java line: 662)
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@89ab1bba: enable
   
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.bluetooth.BluetoothManagerService.checkPermissionForDataDelivery(BluetoothManagerService.java:3112)
    at com.android.server.bluetooth.BluetoothManagerService.checkConnectPermissionForDataDelivery(BluetoothManagerService.java:3130)
    at com.android.server.bluetooth.BluetoothManagerService.checkBluetoothPermissions(BluetoothManagerService.java:1083)
    at com.android.server.bluetooth.BluetoothManagerService.enable(BluetoothManagerService.java:1278)
    at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:177)

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="33"/>
<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)
AddPermission(android.permission.WAKE_LOCK)

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 SelectPairing.Clear Dim Index As Int = ShowProgress Wait For Admin_DiscoveryFinished HideProgress(Index) If SelectPairing.Size = 0 Then ToastMessageShow("No device found.", True) End If End If 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
        SelectPairing.Clear
        Dim Index As Int = ShowProgress
        Wait For Admin_DiscoveryFinished
        HideProgress(Index)
        If SelectPairing.Size = 0 Then
            ToastMessageShow("No device found.", True)
        End If
    End If
   
End Sub
 
Last edited:

Brian Dean

Well-Known Member
Licensed User
Longtime User
Yes, that is a similar thread, but as I said in an earlier post in that thread different applications using Bluetooth in different ways can need different permissions.
 
Upvote 0

Mr Blue Sky

Active Member
Licensed User
Longtime User
Yes, that is a similar thread, but as I said in an earlier post in that thread different applications using Bluetooth in different ways can need different permissions.
by going to app info and manually authorized (device nearby) everything works fine.
there must be a permission for this function?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I will test this tomorrow
I think that you might be on the right track now. My Bluetooth app has the "Nearby devices" permission even though I did not consciously request it.

Without really understanding the situation I found this statement in Android Developers ...
The NEARBY_WIFI_DEVICES permission is part of the Nearby devices permission group. This group, added in Android 12 (API level 31), also includes permissions related to Bluetooth and Ultra-wideband.
This shows that there is an overlap between Wi-Fi and other devices. Maybe my Bluetooth app has this permission because I enabled it for WiFi location at "system" level. That might explain why some people are having permission problems with Android 12/13 and Bluetooth while others do not. Or this might all be nonsense. Maybe somebody can provide an authoritative answer.

Edit: Forgot to mention that there are variations between classic Bluetooth, which I am using, and Bluetooth LE, of course.
 
Last edited:
Upvote 0

Mr Blue Sky

Active Member
Licensed User
Longtime User
after modification I confirm in any case for my case that from Android 13 you must use: AddPermission(android.permission.NEARBY_WIFI_DEVICES)
everything is working perfectly now

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="33"/>
<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.NEARBY_WIFI_DEVICES)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.WAKE_LOCK)

B4X:
    If phone.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.NEARBY_WIFI_DEVICES")
            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
 
Upvote 0
Top