Search code examples
javascriptandroidreact-nativebluetooth

Android requesting location instead of bluetooth permissions; depending on android version


I am debugging a react native app that absolutely needs bluetooth permissions, however at the moment in android the bluetooth permissions are returning unavailable.
I am requesting permissions in my AndroidManifest.xml like this:

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

And I am checking the status of bluetooth permissions like this:

const checkBluetoothPermissions = () => {
    requestMultiple(
      Platform.OS === 'android'
        ? [
            PERMISSIONS.ANDROID.BLUETOOTH_SCAN,
            PERMISSIONS.ANDROID.BLUETOOTH_CONNECT,
            PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
          ]
        : [PERMISSIONS.IOS.BLUETOOTH_PERIPHERAL],
    ).then(_ => {
      console.log('permission results', _);
    });
  };

What is being logged from this function is permission results {"android.permission.ACCESS_FINE_LOCATION": "granted", "android.permission.BLUETOOTH_CONNECT": "unavailable", "android.permission.BLUETOOTH_SCAN": "unavailable"} And when installing the app the only permission that is asked is location and I do not know why. I followed the instructions here: react-native-permissions.

There is no issue on IOS from the same codebase.


Solution

  • do it like this

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    

    and call this function

    import {
      requestMultiple,
      PERMISSIONS,
      RESULTS,
    } from "react-native-permissions";
    
    const getStatusPermissions = async () => {
      const androidOsVer =
        Platform.os === "android" && Platform.constants["Release"];
      let androidPermissions = [PERMISSIONS.ANDROID.BLUETOOTH_SCAN];
    
      if (androidOsVer > 11) {
        androidPermissions.push(PERMISSIONS.ANDROID.BLUETOOTH_SCAN);
      }
    
      const statusesList = await requestMultiple(androidPermissions);
    
      return Object.values(statusesList).some((el) => el === RESULTS.GRANTED);
    };