skip to Main Content

I’m on developing a flutter application contains a BLE module with flutter_blue package.
I tested it with android versions 8, 10 and 11 and it works but for android 12 I got an exception while scanning devices.
the exception is :
**E/flutter ( 8367): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(startScan, Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@bbd118dc: GattService registerScanner, java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN **

this is the permissions on manifest.xml

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

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
   android:usesPermissionFlags="neverForLocation" />

I would be very thankful if you can help me.

3

Answers


  1. Try adding these permissions.

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

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

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

    For FlutterBlue to function correctly on Android, these permissions are required (especially on newer Android versions).

    Ensure that the user is granting location permission.

    Use https://pub.dev/packages/permission_handler and request for location permissions like in the snippet below before you start the scan.

    await Permission.location.request();

    Login or Signup to reply.
  2. 1)Delete the app from device and get build again. It solved for me when i was trying to access it.

    2)Try to use permission_handler for permission to bluettoth.
    https://pub.dev/packages/permission_handler

    Login or Signup to reply.
  3. This is how I define permission in android
    In AndroidManifest.xml

    <!-- required for API 18 - 30 -->
    <uses-permission
        android:name="android.permission.BLUETOOTH"
        android:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="30" />
    
    <!-- required for API 23 - 30 -->
    <uses-permission-sdk-23
        android:name="android.permission.ACCESS_COARSE_LOCATION"
        android:maxSdkVersion="30" />
    <uses-permission-sdk-23
        android:name="android.permission.ACCESS_FINE_LOCATION"
        android:maxSdkVersion="30" />
    
    <!-- API 31+ -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />
    

    And You have to ask permission runtime from user based on your needs, you can follow here Android Bluetooth permissions

    if (androidApiVersion <= 30) {
      switch (await Permission.location.request()) {
    
      }
      return;
    }
    
    if (30 < androidApiVersion) {
      Map<Permission, PermissionStatus> statuses = await [Permission.bluetoothScan, Permission.bluetoothAdvertise].request();
    
      if (statuses[Permission.bluetoothScan] == PermissionStatus.granted && statuses[Permission.bluetoothScan] == PermissionStatus.granted) {
       // permission granted
      }
      return;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search