Requesting android permissions in Ionic React app using Capacitor

Viewed 724

I have an Ionic React project that builds and runs successfully on a One Plus 7 Pro running android 11. I am having trouble implementing all of the permission requests that the app needs to function properly. I'm using the android permissions plugin to trigger the permission requests and some requests are working but none of the bluetooth related permissions are. Specifically the camera and access_fine_location permission requests are properly prompting the user to accept/decline but any bluetooth permissions don't prompt the user. Before I request each permission I perform a check to see if the permission has already been granted or not. Of the four bluetooth permissions I am trying to use, two report that they have already been granted (BLUETOOTH & BLUETOOTH_ADMIN) and two report that they haven't been granted (BlUETOOTH_CONNECT & BLUETOOTH_SCAN). But when I check permissions in app info it shows that the only permissions granted are the camera and access_fine_location, both of which I'm not having any trouble with. Can anyone tell me why none of my bluetooth permission requests are working?

Link to the android permissions plugin I am using:
https://ionicframework.com/docs/native/android-permissions

PermissionServices.tsx file with my functions that handle permission requests:

import { Capacitor } from '@capacitor/core';
import { AndroidPermissions } from '@ionic-native/android-permissions';

const permissions =
{
    //Working
    requestCameraPermission() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.CAMERA).then(
                result => {
                    if (result.hasPermission) {
                        //alert('you already have this permission')
                        alert('camera permission already granted');
                    } else {
                        //alert('please implement permission request')
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.CAMERA);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    },

    //NOT WORKING | App thinks request is already granted
    requestBluetooth() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.BLUETOOTH).then(
                result => {
                    if (result.hasPermission) {
                        alert('bluetooth permission already granted');
                    } else {
                        alert('bluetooth permission not granted');
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.BLUETOOTH);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    },

    //NOT WORKING | App thinks request is already granted
    requestBluetoothAdmin() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.BLUETOOTH_ADMIN).then(
                result => {
                    if (result.hasPermission) {
                        alert('bluetooth permission already granted');
                    } else {
                        alert('bluetooth permission not granted');
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.BLUETOOTH_ADMIN);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    },

    //NOT WORKING | permission not granted | request not working
    requestBluetoothConnect() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.BLUETOOTH_CONNECT).then(
                result => {
                    if (result.hasPermission) {
                        alert('bluetooth permission already granted');
                    } else {
                        alert('bluetooth permission not granted');
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.BLUETOOTH_CONNECT);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    },

    //NOTWORKING | permission not granted | request not working
    requestBluetoothScan() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.BLUETOOTH_SCAN).then(
                result => {
                    if (result.hasPermission) {
                        alert('bluetooth permission already granted');
                    } else {
                        alert('bluetooth permission not granted');
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.BLUETOOTH_SCAN);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    },

    //Working
    requestFineLocation() {
        if (Capacitor.isNativePlatform()) {
            AndroidPermissions.checkPermission(AndroidPermissions.PERMISSION.ACCESS_FINE_LOCATION).then(
                result => {
                    if (result.hasPermission) {
                        alert('fine location permission already granted');
                    } else {
                        alert('fine location permission not granted');
                        AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.ACCESS_FINE_LOCATION);
                    }
                }
            )
        }
        else {
            console.log('Capacitor not detected, this button will do nothing :(')
        }
    }

}

export default permissions;

AndroidManifest.xml file showing that I have properly declared the permissions I am trying to use

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.ionic.starter">

    <!-- android permissions -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="io.ionic.starter.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>
</manifest>
0 Answers
Related