我们可以控制何时以编程方式显示蓝牙访问提示(如位置)吗?

Can we control when the Bluetooth access prompt is shown programmatically (like location)?

我最近问了一个问题,关于如何从 iOS 13 中关于新描述的新蓝牙访问提示访问回调 - NSBluetoothAlwaysUsageDescription。我不确定我是否看到了蓝牙访问提示,除非蓝牙已关闭并显示旧描述 - NSBluetoothPeripheralUsageDescription.

注册提示回调涉及实现 CBCentralManager 的实例并注册 UpdatedState 事件处理程序。

现在我想知道 - 有没有办法确定何时显示提示

如果我们创建一个默认的 Xamarin.Forms/Xamarin.iOS 应用程序并将以下内容添加到它的 Info.plist 文件中-

<key>UIBackgroundModes</key>
<array>
<!--The app communicates with BLE peripherals using the Core Bluetooth framework.-->
<string>bluetooth-central</string>
<!--The app shares data using the Core Bluetooth framework.-->
<string>bluetooth-peripheral</string>
</array>

<key>NSBluetoothPeripheralUsageDescription</key>
<string>App would like to use bluetooth.</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>App would like to use bluetooth.</string>

在应用程序启动时,新的蓝牙提示似乎是 ALWAYS。作为开发人员,我们可以解决这个类似的位置吗?

这就是我所说的“喜欢位置”的意思。同样,如果将位置访问添加到 plist 文件,如下所示,则提示会在应用程序启动时出现。 但是 这个提示显示给用户的时间是可以操纵的!

当我们想要显示提示时,只需实例化 LocationManager 实例 -

Manager = new LocationManager();
Manager.LocationUpdated += HandleLocationChanged;

我们面临的问题是,即使没有初始化任何与蓝牙相关的 instances/classes/implementations/etc 应用程序 ALWAYS 首先提示蓝牙。所以-

我们可以控制何时以编程方式显示蓝牙访问提示(如位置)吗?

(如果有人好奇 - 要访问位置提示的回调,请注册 CLLocationManager.AuthorizationChanged 事件)

同样,这些是我们目前拥有的位置的 plist 条目

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>App would like to access location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>App would like to access location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App would like to access location.</string>

如果你查看CBCentralManager的源代码。你会发现它有一个构造函数

public CBCentralManager(ICBCentralManagerDelegate centralDelegate, DispatchQueue queue, CBCentralInitOptions options);

因此您可以在初始化 CBCentralManager

时将 属性 ShowPowerAlert 设置为 true
CBCentralManager manager = new CBCentralManager(new xxxDelegate(),null,new CBCentralInitOptions() { ShowPowerAlert=true});

初始化CentralManager时,如果蓝牙没有打开,会弹出Alert。