skip to Main Content

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.

2

Answers


  1. 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);
    };
    
    Login or Signup to reply.
  2. It should implicitly include BLUETOOTH_SCAN and BLUETOOTH_CONNECT by requesting the BLUETOOTH permission. Because location permission is frequently needed for Bluetooth functionality, make sure it’s also being requested.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
     const checkBluetoothPermissions = () => {
        requestMultiple(
          Platform.OS === 'android'
            ? [
                PERMISSIONS.ANDROID.BLUETOOTH,
                PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
              ]
            : [PERMISSIONS.IOS.BLUETOOTH_PERIPHERAL],
        ).then((results) => {
          console.log('permission results', results);
        });
      };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search