Android Question [SOLVED] Runtime BLUETOOTH_SCAN permission not working - android 12 device, targetsdk 31, android33 sdk

foakgul

Member
Licensed User
Longtime User
Hello,

I followed the instructions here to try to make the bluetooth scan work on Android 12 device with app targeted for API level 31.

However, even though the permission is added in the manifest and access_fine_location permission is requested in runtime in the code, I'm getting the below error in logcat, and the program crashes. I don't get typical Android nearby_devices permission dialog box.

Am I missing something? Is there a function to request runtime permission just like location? Similar to this:
B4X:
rp.CheckAndRequest(rp.PERMISSION_BLUETOOTH_SCAN)

Thanks

Android SDK used - Android 33:
platforms\android-33\android.jar

Logcat error:
B4X:
12-11 23:59:12.123 18500 18500 E AndroidRuntime: java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10702, packageName = X, attributionTag = null, token = android.os.BinderProxy@b1e0deb, next = null }: GattService registerScanner
12-11 23:59:12.123 18500 18500 E AndroidRuntime:        at X.beaconmonitor._startscan(beaconmonitor.java:399)
12-11 23:59:12.123 18500 18500 E AndroidRuntime:        at X.main._startscan(main.java:2070)
12-11 23:59:12.123 18500 18500 E AndroidRuntime:        at X.main._timer2_tick(main.java:2151)

Code requesting runtime location permission: (this is requested before Bluetooth scan operation)
B4X:
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        File.MakeDir(rp.GetSafeDirDefaultExternal(""), "X")
    Else
        ToastMessageShow("Location permission is needed to collect data. Please grant permission! Quitting...",True)
        Sleep(3000)
        Activity.Finish
    End If

Manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="31"/>
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddPermission(android.permission.BLUETOOTH_SCAN)
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Add

B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)

And

B4X:
Sub permission_Bluetooth
    Dim phone As Phone
    If phone.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_ADVERTISE")
            Sleep(0)
            Starter.rp.CheckAndRequest(Permission)
            Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
            If Result = False Then
                ToastMessageShow("No permission...", False)
            Else
                LogColor("Permiso Bluetooth aprobado",Colors.Blue)
            End If
        Next
    Else If phone.SdkVersion <> 31 Then
        Log("Es diferente SdkVersion 31")
    End If
End Sub
 
Upvote 1

foakgul

Member
Licensed User
Longtime User
Great @Johan Hormaza . It works. I'm just wondering, are the following really needed for the dialog to show up? Since I'm not advertising or connecting to a BT device for comm, I thought these permissions would not be needed.

B4X:
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)

Or is it the new SUB that asks each of the permissions as runtime permission?

Thanks again!
 
Upvote 0
Top