Android permissions - BLE - Android 12

When I try to run the app, I get a runtime error:
Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@1fe6f5d:GattService registerScanner

I can't find a way to get past this. Help.

The only thing I found so far, is that you have to ask the phone for permission to scan using BLE and it has something to do with android version 12.

Thanks,
Cass

The BluetoothLE extension has not yet been updated to support Android 12. As a workaround, you can add the BluetoothClient component to your project (which will add the right permissions to the project) and then use the screen's AskForPermission block to request the BLUETOOTH_SCAN permission. Usually the extension would do this all on your behalf but I have not yet had time to update the extension.

See here: Bluetooth permissions  |  Android Developers

See also here and here:

3 Likes

I did the 'ask for permission' and tried to continue after that in the 'if' block, but It looks like you only have to enter the permission one time since being in the 'if' block, it missed the connecting part.
Putting in the BT Client component seems to have done it.
Thanks, I'll look forward to the fix in the BLE version.

What do you mean by that? The Screen.PermissionGranted event checks whether the BLUETOOTH_CONNECT permission has been granted and then requests permission BLUETOOTH_SCAN.
It should be correct like that.

Are you getting an error message? If yes, which? (Post a screenshot)
And on which Android version are you testing?

What I meant was this: (I'm not sure if this is the right way to paste my blocks)
I started with this when I first got the permission error:
image
I then added the 'ask for permission':
image
This produced the request for permission but it also didn't connect since I hadn't answered the request yet. I then answered the request and put the connect part in after the permission was granted:
image
This didn't work since I believe what happened was that I had gave it permission the time before so that since the connecting part was buried in the 'if' statement, it didn't run. It seems that the permission is 'already on file' and doesn't have to be repeated.
I went back to the original version, added the BT Client component, and now everything is OK.

BluetoothLE.StartScanning can only be executed after permission BLUETOOTH_SCAN has been granted. So do it like this:

... and this:

Uninstall the previous app, build the new APK (with my blocks), install it and try.

Hi to all
I'm trying the workaround suggest from Anke and this seems work fine on Android 12, but nt work on old android version. For Example in 8.0 (samsung Galaxy S7) on start app (call screen AskForPermssion) show error like:
"Error 908:The permission BLUETOOTH_CONNECT has been denied. Please enable it in the Settings app".
Anyone have the same problem?
I'm trying to turn around the error detecting the version of anroid and skipping the woraround above, but i think is better patch the extension

Show your blocks (and post the aia).

See here:

I'm using your aia sample Android10BLETest_copy3.aia
now i will try to understand what you have replyed.

Many Many thanks

Thanks for your reply
i'm not understand if we need to detect android version and set different permission

I'm in trouble
i have decompiled apk and open the AndroidManifest.xml
the permission for BLUETOOTH, ACCESS_FINE_LOCATION is there

i have tryed to add ACCESS_COARSE_LOCATION and recompile apk ,but the error is the same.
is there a wa to be the app compatible from 8 to 12 android version?

thanks, i will try
I did not know the getApiLevel extension, good.

Many many thanks Anke
for me works both with 8 and 12 android version with these little difference of block

and this is the aia project
AndroidBLETest_v8_12.aia (191.2 KB)

at screen.initialize i need to ask for permission only if newer android version.

I hope my solution could help other developer.
the FineLocation seems to be already in permission.

I hope also a new release of the extension with compatibility.

You could also leverage Screen1.PlatformVersion, which would need to be compared to 12.0, but it would allow you to do this in a way that doesn't require extensions. The only caveat technically would be one of the 4.2.x versions which won't coerce to numbers, but since the BLE extension requires Android 5.0 or higher this shouldn't be an issue.

I checked it now on Android 9 and removed my post.
I'll post a new aia tomorrow.

It didn't work. If I am right, you only have to give it (the phone in question) permission one time. After that, it doesn't ask for it again. I think that's why nothing happens when you bury the connection process in an 'if' statement that it doesn't get to.

Here's what I used.


Apparently it doesn't get to the Start Scanning part.