如何在 iOS13 中处理用户与新蓝牙提示的交互?

How to handle user interaction with new Bluetooth prompt in iOS13?

"There’s a new key for the Info.plist file: NSBluetoothAlwaysUsageDescription, which you can use to explain how and why your app uses Bluetooth. In contrast to the existing NSBluetoothPeripheralUsageDescription, the new description will be shown as part of the new pop-up, and also in the Settings app on the Bluetooth Privacy screens."

更新到 iOS 13 后,我们的应用程序崩溃了,我相信很多人都遇到过。我们的问题是由于 Xamarin.iOS 项目 Info.plist 文件中没有包含新需要的蓝牙密钥。

然而,在加载时添加此后,出现的第一个 "action" 会向用户显示新的蓝牙访问提示。

我们不清楚如何捕获对此提示的响应。事实上,在这个提示与应用程序交互后,没有 "return" 点。无法完全找到提示交互/结果处理的断点,我们的应用程序从不 returns 从提示。它是 运行 但下一个 "action" 永远不会发生。

那么- capture/handle 用户如何与 iOS 13 中的新蓝牙提示进行交互?

注意*:为了绝对透明 - 我们的应用程序不会初始化 CBCentralManager 的任何实例,而是利用本身在内部使用蓝牙 LE 的本机框架(出于我们的控制)。

我们的案例可能非常独特,但对于那些体验过此功能的人来说,实施 CBCentralManager 并利用其 UpdatedState 方法来捕获用户与新出现的蓝牙对话的交互。

在我们的 Apps MainPage 的页面创建中调用初始化

BleManager = new BluetoothManager();

Class

using System;
using CoreBluetooth;

namespace MyApp.iOS.Classes
{
    public class BluetoothManager
    {
        public CBCentralManager CentralBleManager
        {
            get { return this.bleCntrlMgr; }
        }
        protected CBCentralManager bleCntrlMgr;
        protected CBCentralManagerDelegate bleMgrDel;

        public BluetoothManager()
        {
            this.bleCntrlMgr = new CoreBluetooth.CBCentralManager();
            this.bleCntrlMgr.UpdatedState += BleMgr_UpdatedState;
        }

        private void BleMgr_UpdatedState(object sender, EventArgs e)
        {
            Console.WriteLine("UpdatedState: {0}", bleCntrlMgr.State);
            if (bleCntrlMgr.State == CBCentralManagerState.PoweredOn
                || bleCntrlMgr.State == CBCentralManagerState.PoweredOff
                || bleCntrlMgr.State == CBCentralManagerState.Resetting
                || bleCntrlMgr.State == CBCentralManagerState.Unauthorized
                || bleCntrlMgr.State == CBCentralManagerState.Unsupported)
            {
                /* return point */
                // i.e.: CallMethod();
            }
            else if (bleCntrlMgr.State == CBCentralManagerState.Unknown)
            {
                /* <-- request access --> */
            }
        }
    }
}