dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Permission bug: Bluetooth scanning without location permission on Android 12+ #20871

Open janusw opened 8 months ago

janusw commented 8 months ago

Description

Since Android 12, it is possible to do Bluetooth scanning without location permissions. To achieve this, one has to declare the BT scanning permission in the Android manifest like this:

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

Requesting the permissions at runtime works well for this scenario, as long as ACCESS_FINE_LOCATION is not declared in the manifest:

            PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Bluetooth>();

            if (status != PermissionStatus.Granted)
                status = await Permissions.RequestAsync<Permissions.Bluetooth>();

However, if ACCESS_FINE_LOCATION is declared in the manifest, but is denied at runtime (while BLUETOOTH_SCAN is granted), then the snippet above reports the status as PermissionStatus.Denied.

This wrongly signals to the application that scanning is not possible, while in fact it is.

Steps to Reproduce

  1. Check out the dotnet-bluetooth-le repository (link below).
  2. Build the sample BLE.Client.MAUI and run it on a Android 12+ device.
  3. Reject the location permission, but accept the Bluetooth/Nearby devices permission at runtime.
  4. As a result, the app will not start scanning (although it could): "Permissions denied. Not scanning."

Link to public reproduction project repository

https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12 and up

Did you find any workaround?

Ignore the wrong permission status.

Relevant log output

No response

janusw commented 8 months ago

Btw, the exact same problem also affects Permissions.NearbyWifiDevices, which can also be used with android:usesPermissionFlags="neverForLocation".

The permissions Bluetooth and NearbyWifiDevices were introduced with PR #12413 and #14718.

Android documentation references:

bernieEV commented 3 months ago

YES, I want measurement equipment using bluetooth and smartphone as control device/display never ask for location permission!