Xamarin Forms BLE 插件扫描问题 Android 12

Xamarin Forms BLE Plugin Scan Issue Android 12

我编写了一个 xamarin 表单应用程序,它使用 Plugin.BLE NuGet 包来扫描和连接到 ble 设备。它在我的 phone 上 working/debugging 完美无缺,直到它(我的 phone)更新为 Android 12.0 - API 31。我正在使用来自插件:

adapter.DeviceDiscovered += (s,a) => deviceList.Add(a.Device);
await adapter.StartScanningForDevicesAsync();

我发现 Android 12 需要不同的运行时权限 ([https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#declare-android11-or-lower][ 1]) 并且我尝试将它们包含在清单文件中:

    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="Rep_001.Android" android:theme="@style/MainTheme"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#declare-android11-or-lower

可惜我运气不好。我在想我需要在运行时向用户询问扫描和连接权限,但我不确定该怎么做,因为 Xamarin.Essentials 没有蓝牙权限请求。

BLUETOOTH_ADVERTISEBLUETOOTH_CONNECTBLUETOOTH_SCAN权限是运行时权限。

因此,您必须在您的应用程序中明确请求用户批准,然后才能查找蓝牙设备、使设备可被其他设备发现或与 already-paired 蓝牙设备通信。

当您的应用请求其中至少一项时 permissions,系统会提示用户允许您的应用访问附近的设备。

好吧,我明白了。在 Android 项目中,我创建了一个 class 继承自 Xamarin.Essentials.Permissions.BasePlatformPermission:

 public class BLEPermission : Xamarin.Essentials.Permissions.BasePlatformPermission
{
    public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new List<(string androidPermission, bool isRuntime)>
{
    (Android.Manifest.Permission.BluetoothScan, true),
    (Android.Manifest.Permission.BluetoothConnect, true)
}.ToArray();
}

然后在 MainActivity.cs -> OnCreate() 中,我在加载应用程序后立即插入了 RequestAsync:

        protected override async void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        await Permissions.RequestAsync<BLEPermission>();
    }

现在,当应用程序首次加载时,它会要求用户进行蓝牙扫描和连接权限。所有后续启动不 re-prompt 权限。 Sites/Documentation一路帮助

https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#declare-android11-or-lower

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows