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
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();
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
This is how I define permission in android
In
AndroidManifest.xml
And You have to ask permission runtime from user based on your needs, you can follow here Android Bluetooth permissions