如何在 OSX 上恢复 CoreBluetooth 的 CentralManager 状态?或者:为什么 CBCentralManagerRestoredStatePeripheralsKey 不可用?

How to restore CoreBluetooth's CentralManager state on OSX? Or: Why is CBCentralManagerRestoredStatePeripheralsKey unavailable?

我的 OSX 10.10 应用程序应使用 centralManager:willRestore 委托方法恢复 CoreBluetooth CentralManager 状态。

因此我使用CBCentralManagerRestoredStatePeripheralsKey字典键来恢复蓝牙外设。但由于错误

,它没有编译

CBCentralManagerRestoredStatePeripheralsKey is unavailable

该符号仅针对 iOS 7+ 定义为:

CB_EXTERN NSString * const CBCentralManagerRestoredStatePeripheralsKey NS_AVAILABLE(NA, 7_0);

也就是说,NS_AVAILABLE 注释使该符号在 OSX 上不可用。

The CBCentralManagerDelegate's documentation unfortunately says nothing about how to restore the manager's state。它唯一提到的是

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict

dict: A dictionary containing information about the central manager that was preserved by the system at the time the app was terminated. For the available keys to this dictionary, see Central Manager State Restoration Options.

但是没有这样的字典描述。 It however exists for iOS.

那么,如何在 OSX 上成功恢复 CentralManager

在 macOS 上,由于内存原因,应用程序不会像在 iOS 上那样被操作系统终止。

当 iOS 然后恢复终止的应用程序时,它还会恢复以前连接的外围设备,让 iOS 应用程序有机会继续与这些外围设备对话。

也就是说,macOS 上不需要恢复此类外设连接。