Beta version of BLE extension for Android 12 permission changes

Hello everyone,

We have a beta version of the BLE extension that incorporates the new Bluetooth permissions introduced in Android 12. Please make a backup of your projects before upgrading the extension. Because this is a work in progress, there may be issues that still need to be addressed. If you try it out, please leave feedback in this thread.

edu.mit.appinventor.ble-20230223-beta.aix (200.0 KB)

Regards,
Evan W. Patton, Ph.D.
Lead Software Engineer, MIT App Inventor

9 Likes

Known issues will be added to this post as they arise.

(added to FAQ)

I checked it on an Android 13 device. It seems that the Location sensor must be enabled (also on Android 12+) to get BLE devices (BLE.DeviceList) - Companion & APK.

In addition, the BLUETOOTH_CONNECT permission is still not declared in the Manifest. So the BluetoothClient component has to be added again (as a dummy) to get this permission in the Manifest.

Summary

Did you include a block that actually tries to scan or connect? The dangerous permissions are included conditionally. This way, for example, your app doesn't get the BLUETOOTH_ADVERTISE permission if the app never uses the StartAdvertising block. For example, here is a copy of our healthy plant IOT tutorial with the beta version added and when I compile it to APK I get the location and Bluetooth permissions.

IoT_HealthyPlants_BLE12.aia (381.2 KB)

My test app (aia), which I used from one of my old BLE posts, only lists the available devices. The BLUETOOTH_CONNECT permission is also required for this. However, the new BLE extension only declares this permission if at least one (empty) BLE.Connect... block is added.

According to my tests, BLUETOOTH_CONNECT (+ BLUETOOTH_SCAN) is always required on Android 12+ (even for just scanning).

Hmm. That is troubling because the Google documentation for Bluetooth permissions definitely does not read that way to me. It claims:

  1. If your app communicates with already-paired Bluetooth devices, declare the BLUETOOTH_CONNECT permission.

but when one scans for devices there's no guarantee that the found devices are even paired so the antecedent can't hold.

I'll try to build a very simple test app outside of App Inventor and poke around the AOSP to see if there's some additional insight to be had there.

[1] Bluetooth permissions  |  Connectivity  |  Android Developers


Permissions:

I think I have an idea of what is going on. Can you attach a copy of the stack trace from adb logcat?

I've uploaded a new version which should mitigate the issue.

1 Like

To where?
EDIT: Got it.

Yes, this works.
But the Location sensor has to be enabled (also on Android 12+), as I already mentioned (at least to find / list my devices).

1 Like

Maybe one can get rid of the Location permission on Android 12+ if this is declared in the Manifest:

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
                     android:usesPermissionFlags="neverForLocation" />

I'll check it later ...

If you'd like to add that flag, you should be able to check the new design property "NoLocationNeeded". However, due to limitations in the App Inventor build system this won't result in the removal of the ACCESS_FINE_LOCATION permission in the Manifest, but it should stop the extension from asking for the permission at runtime.

Well, since I have to edit the manifest anyway, I might as well remove ACCESS_FINE_LOCATION there. At the moment, however, I also have the problem with my current work notebook that the "neverForLocation" flag is unknown in my APK Editor Studio version. It was probably added later by Google.

Unfortunately, the current APK Studio version cannot be installed on my ancient Win7 notebook. I'll have to try it on another Win10 PC or on my MacMini, later ..

Hi Evan - is anything being sorted out for the Bluetooth Classic issue?

Re: Need Help In Bluetooth Permission for an app on Android 12 - #97 by Madhav

That is about a Bluetooth Classic permission error but has got overwhelmed with the BLE issue. See post#97 from Madhav, the original Topic author.

Correct. The flags feature was something added for SDK 31 and needs a newer version of the build tools even for us to compile with it, so I expect many other tools will need to be updated to take it into account.

Hi Evan and Chris, I have figured out a way to solve this Bluetooth Classic Permission Issue.

  1. Build the APK file from your project in MIT APP inventor.
  2. Open the AndroidManifest.xml file to modify the permissions.
  3. Delete all permissions related to BLUETOOTH and add 3 lines.

  1. Save the AndroidManifest.xml file with above changes.
  2. Rebuild the APK file.

NOTE:

  1. How to view the AndroidManifest.xml?
    ------ Download APK Studio Editor from the link Download APK Editor for PC and Mac – APK Editor Studio
    ------ Open APK Studio Editor and Open the APK file.
    ------ Click on the "Open Contents" tab
    ------ Folder will be opened and "AndroidManifest.xml" file can be seen.

  2. How to Edit AndroidManifest.xml file?
    ------ Open the AndroidManifest.xml using any of the text editor.
    ------ Delete the lines related to Bluetooth related permissions.
    ------ Paste the below lines and save the changes.

  1. How to Rebuild the APK
    ------ After following the Step1 and Step2, On APK Studio Editor, Click on "SAVE APK"

Now, install the APK on mobile and Check the Bluetooth Permission Error Should be gone

1 Like

Even with the latest version of apktool it is not possible to set compileSdkVersion=31:

It seems to be related to aapt2.exe (aapt.exe) - I'm not sure... :woman_shrugging: :thinking:
Please any tip/hint that might help?