Search code examples
androidflutterbluetooth

Flutter Bluetooth Permissions Issue on Android 13: Unable to Scan for Devices


I am developing a Flutter app that utilizes Bluetooth functionality, specifically Bluetooth scanning, using the flutter_blue package. I have encountered an issue related to Bluetooth permissions on Android 13.

Problem Description:

In my AndroidManifest.xml file, I have included the following permissions:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

In my Flutter code, I am using the permission_handler package to request Bluetooth scanning and location permissions. Here's my code snippet:

@override
void initState() {
  super.initState();
  requestBluetoothPermission();
}

void requestBluetoothPermission() async {
  PermissionStatus bluetoothStatus = await Permission.bluetoothScan.request();
  PermissionStatus locationStatus = await Permission.location.request();

  if (bluetoothStatus.isGranted && locationStatus.isGranted) {
    scanDevices();
  } else {  }

}

void scanDevices() {
  try {
    flutterBlue.startScan();
    flutterBlue.scanResults.listen((results) {
      setState(() {
        _scannedDevices = results;
        print("Bluetooth scan results: ${results.toString()}");
      });
    });
  } catch(e) {
    print("Error during Bluetooth scanning: $e");
  }
}

Issue: Despite requesting the necessary permissions, I am still encountering the following error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled     
Exception:PlatformException(startScan, Need android.permission.BLUETOOTH_SCAN 
permission for     AttributionSource { uid = XXXX, packageName = XXXX, attributionTag 
= null, token = android.os.BinderProxy@XXXX, next = null }: GattService 
registerScanner, java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN 
permission for AttributionSource { uid = XXXX, packageName = XXXX, attributionTag = 
null, token = android.os.BinderProxy@XXXX, next = null }: GattService registerScanner
Additional Information:

Flutter version: 3.13.2 flutter_blue package version: 0.8.0 permission_handler package version: 11.0.0 Android version: 13

I have already checked the permissions in my AndroidManifest.xml file and ensured that I am requesting them correctly in my Flutter code. What could be causing this issue, and how can I resolve it to successfully scan for Bluetooth devices on Android 13?

Thank you for your assistance!


Solution

  • As stated in the official guide, you have to include BLUETOOTH_SCAN permission in order to be able to scan Bluetooth devices.

    If your app looks for Bluetooth devices, such as BLE peripherals, declare the BLUETOOTH_SCAN permission.

    So in your manifest it should look like this:

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