flutter-permission-handler icon indicating copy to clipboard operation
flutter-permission-handler copied to clipboard

[Android] Bluetooth permission required twice

Open c15yi opened this issue 3 years ago • 16 comments

🐛 Bug Report

I only want to test whether Bluetooth is turned on or off (with flutter_blue_plus), therefore I would assume that only <uses-permission android:name="android.permission.BLUETOOTH"/> would be sufficient.

However, when I request Bluetooth permission I get D/permissions_handler(27550): Bluetooth permission missing in manifest. The same is true when there is no uses-permission entry at all.

Setting the line twice like this however works:

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

This only happens in combination with flutter_blue_plus, but since the error message is reported by this plugin I wanted to start here.

I actually can request the bluetooth state even without the permission at all on my device (android 12) but I guess it's necessary on older devices.

The problem with adding the permission twice is that Google Play refuses to accept the AABs.

Expected behaviour

Requesting/getting the permission result without error messages.

Reproduction steps

I created a small reproducing project here. In the AndroidManifest.xml the Bluetooth permission is only added once, which leads to the error above.

Configuration

Phone: Pixel 5 with Android 12

Version: 10.0.0

Platform:

  • [ ] :iphone: iOS
  • [x] :robot: Android

c15yi avatar Aug 01 '22 14:08 c15yi

Android 12's permission handling has changed, so the <uses-permission android:name="android.permission.bluetooth> must be changed, the permission is only used for devices which target API level 30 and lower. You can read more about it here; https://developer.android.com/guide/topics/connectivity/bluetooth/permissions.

I think this should be changed in the documentation of the permission handler, since the documentation is still targeting Android 11 and lower.

florissmit1 avatar Aug 05 '22 13:08 florissmit1

Since my use case is only to check whether bluetooth is turned on or off, I actually don't need any bluetooth permission at all on no android device.

The documentation says that the android.permissions.BLUETOOTH permission is only to connect to paired devices (not really what I needed anyways).

In the end on Android I can safely ignore the Bluetooth permission handling.

But regarding the behaviour of the library it's still strange, that there is that error message. Even when I add these permissions

<uses-permission android:name="android.permission.BLUETOOTH"
                     android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>

it still shows me this error message: D/permissions_handler(17875): Bluetooth permission missing in manifest

I updated the example repo with this for you to check.

c15yi avatar Aug 05 '22 13:08 c15yi

I've encountered a similar problem, but with bluetooth AND location. After duplicating the BLUETOOTH permission, I got an error "No permissions found in manifest for: []3", which number, looking at the code, maps to the location group. I then duplicated my existing location permissions, and the app then successfully asked for permissions and ran correctly. This is pretty clearly a bug in something; having the permissions present once should be sufficient, and having them present MORE than once should do nothing.

Permissions:

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

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

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

Request code:

  Map<Permission, PermissionStatus> statuses = await [
    Permission.bluetooth,
    Permission.bluetoothScan,
    Permission.bluetoothConnect,
    Permission.bluetoothAdvertise,
    Permission.location,
  ].request();

Android 12

Flutter 3.3.0-0.2.pre • channel beta • https://github.com/flutter/flutter.git Framework • revision 7ac27ac8e6 (2 weeks ago) • 2022-08-02 14:35:08 -0700 Engine • revision d1e7dc18bf Tools • Dart 2.18.0 (build 2.18.0-271.4.beta) • DevTools 2.15.0

Erhannis avatar Aug 17 '22 00:08 Erhannis

I've been struggling the the exact same issue with flutter_blue_plus for the last few days. I stumbled across your post and tried duplicating the bluetooth permission in the manifest and it worked. Another odd thing about this is that I was previously using the original flutter_blue and didn't have any manifest issues even though I didn't duplicate the permission.

benjaminpaik avatar Dec 25 '22 20:12 benjaminpaik

@benjaminpaik, if I remember correctly there were issues with release builds when having the permission set twice in the manifest. So that is not really a workaround.

c15yi avatar Jan 11 '23 10:01 c15yi

In my case I got same error before flutter 3.7.0. After updating flutter version to 3.7.0, the error disappeared.. 😂

appano1 avatar Jan 30 '23 06:01 appano1

@appano1 Hmm. I just tried upgrading to flutter 3.7.0 as well and I still have permission issues.

benjaminpaik avatar Jan 31 '23 04:01 benjaminpaik

@benjaminpaik Try this..!

flutter: 3.7.0 permission_handler: ^10.0.2

AndroidManifest.xml

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

    <!-- Bluetooth permission for Android sdk version > 30 -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

in your dart file

// If the Android version is higher than 30,
await [
  Permission.bluetoothConnect,
  Permission.bluetoothScan,
  Permission.bluetoothAdvertise,
].request();

appano1 avatar Jan 31 '23 10:01 appano1

@appano1 Thanks for replying.

That's basically how I have it, but it still only works if I add BLUETOOTH permissions twice.

benjaminpaik avatar Feb 01 '23 06:02 benjaminpaik

Duplicating the BLUETOOTH permission worked for me as well. Couldn't it be that the first occurrence gets merged with another library's definition with maxSdkVersion version set and then the second occurrence is left as is?

My working merged AndroidManifest.xml looks like this:

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

I suspect that permission_handler is requiring manifest entries that aren't really necessary and fails with the Bluetooth permission missing in manifest message even though everything should work.

albertmoravec avatar Feb 03 '23 14:02 albertmoravec

This might help https://www.youtube.com/watch?v=uMvGpBOT0ZY

myselfuser1 avatar Mar 17 '23 12:03 myselfuser1

Adding bluetooth permission twice is not working anymore. It throws error: Element uses-permission#android.permission.BLUETOOTH at AndroidManifest.xml:6:3-66 duplicated with element declared at AndroidManifest.xml:3:3-5:36

C-8 avatar Jun 21 '23 10:06 C-8

Any update on this issue?

DevTiago avatar Nov 18 '23 00:11 DevTiago

@C-8 any workaround on this?

alexxgarci avatar Nov 21 '23 13:11 alexxgarci

Adding bluetooth permission twice is not working anymore. It throws error: Element uses-permission#android.permission.BLUETOOTH at AndroidManifest.xml:6:3-66 duplicated with element declared at AndroidManifest.xml:3:3-5:36

I just tested my old project upgrading to Flutter 3.16.3 and permission_handler 11.1.0. Adding BLUETOOTH to the manifest twice still works for me. Perhaps it was broken and then fixed in a recent update.

benjaminpaik avatar Dec 11 '23 07:12 benjaminpaik