React Native Android 12 Permission, Ble cannot scan

623 Views Asked by At

On Android 12 or higher Ble cannot scan. My android.manifest is below:

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and my run-time ble permission is below:

try {
   const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
   );
   if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use location');
      SetPermission(true);
      osVer > 11 && blePermissionAndroid();
   } else {
      console.log('Location permission denied');
      SetPermission(false);
   }
} catch (err) {
   console.warn(err);
}

async function blePermissionAndroid() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('You can use ble');
        SetPermission(true);
      } else {
        console.log('ble permission denied');
        SetPermission(false);
      }
    } catch (err) {
      console.warn(err);
    }
  }

Finally i get this error on Android 12 and higher version:

"error XXX BleError: Unknown error occurred. This is probably a bug! Check reason property."

How can i work ble scan on Android 12 and higher.

Below Android 12 ble scan works

0

There are 0 best solutions below