'Ionic Bluetooth permissions Issues in Android 9 , 10 & 11

I am experiencing some issues with Bluetooth permissions in an Ionic 4 app. My app scans and connects to a peripheral device over BLE.

Everything runs perfectly in Android 9 and in (most - I cannot speak for all) Android 11. But in Android 10 devices, I have seen the permissions code fail.

I have included android-permissions plugin:

import { AndroidPermissions } from '@awesome-cordova-plugins/android-permissions/ngx/';

And I have made some edits to the config.xml. I may have gone overboard in my effort to solve the issue.

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</edit-config>

In the code where I try to scan and connect, I use the permissions plugin:

let res = await this.androidPermissions.requestPermissions([
  this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION,
  this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
  this.androidPermissions.PERMISSION.BLUETOOTH_ADMIN,
  this.androidPermissions.PERMISSION.BLUETOOTH,
  this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION
]);
console.log('req all permissions = ', res);

However, it fails (at the very least) on some Android 10 devices. If I inspect the device, it throws up an error during the bluetooth scan code something like:

main.6c682b267596d65751ef.js:1 ERROR Error: Uncaught (in promise): Permission Denial: starting Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE cmp=com.android.settings/.bluetooth.RequestPermissionActivity } from ProcessRecord{19be0ad 12313:nz.co.monkwytronics.sensornode/u0a238} (pid=12313, uid=10238) requires android.permission.BLUETOOTH



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source