skip to Main Content

I am using this code to handle Bluetooth scan, but in Android 11, if my Bluetooth is not on, and location permission is also not granted, yet it is showing permission granted. If I go to App permissions in settings, I see no permission granted.

Here is the code:

Future requestBluetoothPermission() async {
   var status = await Permission.bluetoothScan.request();
   if (status.isGranted) {
      print("Bluetooth scan permission granted");
   } else if (status.isDenied) {
      await Permission.bluetooth.request();
   } else if (status.isPermanentlyDenied) {
      await openAppSettings();
   } else {
      print("Bluetooth scan permission denied");
   }
}

2

Answers


  1. For android

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <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.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
    

    you have to put theese permissions on manifest file.It works fine android 8 ,9, 10, 11, 12, 13 ,14 . I don’t know IOS permissions.

    Login or Signup to reply.
  2. It’s worth using a package that handles the bluetooth permissions for you such as https://pub.dev/packages/flutter_blue_plus

    If you want location permission enabled you’ll need to add this:

    Android > android/app/src/main/AndroidManifest.xml

    <!-- New Bluetooth permissions in Android 12
    https://developer.android.com/about/versions/12/features/bluetooth-permissions -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <!-- legacy for Android 11 or lower -->
    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
    
    <!-- legacy for Android 9 or lower -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
    

    iOS > ios/Runner/Info.plist

    <dict>
        <key>NSBluetoothAlwaysUsageDescription</key>
        <string>This app needs Bluetooth to function</string>
    

    Once thats done, you can add this code and listen to bluetooth events

    FlutterBluePlus.events.onConnectionStateChanged.listen((event)) {
        print('${event.device} ${event.connectionState}');
    }
    

    Once you’ve added the package, updated the manifest / plist, and if you’re still developing your app, i’d reccomend deleting the app so permissions will be reset and trying again

    If you need finer permission handling, then at that point i’d reccomend using the https://pub.dev/packages/permission_handler package so you can have more control over whats going on, in a similar way to this

    await Permission.bluetooth
      .onDeniedCallback(() {
        // Your code
      })
      .onGrantedCallback(() {
        // Your code
      })
      .onPermanentlyDeniedCallback(() {
        // Your code
      })
      .onRestrictedCallback(() {
        // Your code
      })
      .onLimitedCallback(() {
        // Your code
      })
      .onProvisionalCallback(() {
        // Your code
      })
      .request();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search