Using permission Handler - Nearby device Permission Not working

742

Integrating the example app into my existing app is what helped me solve my error in the end.

The problem was that I was not allowing all the permissions I needed before attempting to scan for devices nearby.

it turns out when you manuelly toggle location on and off you also accept many other permissions as well.

here is the code needed: bool permGranted = true; var status = await Permission.location.status;

if (status.isDenied) {
  permGranted = false;
  Map<Permission, PermissionStatus> statuses = await [
    Permission.location,
    Permission.bluetoothScan,
    Permission.bluetoothAdvertise,
    Permission.bluetoothConnect
  ].request();
  if (statuses[Permission.location]!.isGranted &&
      statuses[Permission.bluetoothScan]!.isGranted &&
      statuses[Permission.bluetoothAdvertise]!.isGranted &&
      statuses[Permission.bluetoothConnect]!.isGranted) {
    permGranted = true;
  } //check each permission status after.
}
Share:
742
Quipper
Author by

Quipper

Updated on January 05, 2023

Comments

  • Quipper
    Quipper over 1 year

    I am building a Flutter Application that needs to connect to bluetooth devices nearby.

    I followed the documentation for permission_handler here and it works (kinda) but strangely enough it does not fully set the nearby device permission properly because when I go scan for nearby devices it is unable to find any.

    I know this is the problem because when I manually toggle the permission in the app settings it works as intended.

    For reference my androidmanifest.xml:

    <!-- Permissions options for the `contacts` group -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    
    <!-- Permissions options for the `storage` group -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    

    Code to check if permission is granted:

    bool permGranted = true;
    var status = await Permission.location.status;
    if (status.isDenied) {
      permGranted = false;
      if (await Permission.location.request().isGranted) {
        permGranted = true;
      }
    }
    
    if (permGranted) {
      _logMessage('Start ble discovery');
    ...
    

    thanks in advance for any help!