Xamarin iOS 13 在不触发本机弹出窗口的情况下检查蓝牙权限

Xamarin iOS 13 check bluetooth permission without triggering a native popup

iOS13开始请求蓝牙权限。当尚未授予蓝牙权限时,我想显示一个自定义屏幕来解释为什么我需要蓝牙并建议授予应用程序访问权限。在此之前,我必须检查蓝牙权限是否被授予。

此函数立即显示本机弹出窗口并请求权限:

public bool NeedsBluetoothPermission()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                return CBCentralManager.Authorization != CBManagerAuthorization.AllowedAlways;
            }
            else
            {
                return false;
            }
        }

问题:如何在不先触发原生弹出窗口的情况下检查应用是否需要请求蓝牙权限?

This answer 对我不起作用,因为我还没有创建 CBCentralManager 的任何实例,我只使用它的静态 属性.

本地 iOS 开发人员,也请插话。我想这不仅仅是 Xamarin 的问题...

我终于明白了。 此行为是 iOS 13.0 beta 的实际情况。

对于最新的iOS 13.2,我没有观察到这个问题。

我可以安静地检查 CBCentralManager.Authorization 属性。 当我创建 CBCentralManager 的实例时系统弹出窗口出现。

另一种方法是使用 CBCentralInitOptions,您可以在其中将“ShowPowerAlert”设置为 false。

当您创建 CBCentralManager 实例时,传递 init 选项,它不会显示本机弹出窗口

在我的例子中,CBCentralManager.Authorization 不存在。但是 CBPeripheralManager.authorization 是一个静态 class 变量,适用于 iOS 13+。 Apple's documentation

- (void)checkBluetoothPermissionStatus:(CDVInvokedUrlCommand *)command
{
    if (@available(iOS 13.1, *)) {
        switch(CBPeripheralManager.authorization) {
            case CBPeripheralManagerAuthorizationStatusAuthorized:
                NSLog(@"CBP BLE allowedAlways");
                [self requestBluetoothPermission: command]; // Further get the BLE on/off status once the permission is granted
                break;
            case CBPeripheralManagerAuthorizationStatusDenied:
                NSLog(@"CBP BLE denied");
                [self sendMessage:@"Denied" error:true command:command];
                break;
            case CBPeripheralManagerAuthorizationStatusNotDetermined:
                NSLog(@"CBP BLE notDetermined");
                [self sendMessage:@"Undetermined" error:true command:command];
                break;
            case CBPeripheralManagerAuthorizationStatusRestricted:
                NSLog(@"CBP BLE restricted");
                [self sendMessage:@"Restricted" error:true command:command];
                break;
        }
    } else {
        // Fallback on earlier versions
        NSLog(@"CBP BLE unknown");
        [self sendMessage:@"Unknown" error:true command:command];
    }
}