一、后台执行模式
如果你的应用在后台时也需要处理蓝牙事件,就必须在Info.plist中声明应用要支持蓝牙后台模式,这样,当有蓝牙事件发生时,系统会唤醒应用来处理。
有两种蓝牙后台模式,一种为central角色,另一种为peripheral角色。如果应用需要两种角色,则可以声明支持两种模式。声明方式:增加UIBackgroundModes 键,并增加包含下列字符串的array值。
• bluetooth-central—The app communicates with Bluetooth low energy peripherals using the Core Bluetooth framework.
• bluetooth-peripheral—The app shares data using the Core Bluetooth framework
注意:Info.plist中会显示为更加人性化的文本,不是直接显示实际的键值对。如要显示实际值,可右键,或control点击,在弹出菜单中选择Show Raw Keys/Values
二、解释
The bluetooth-central Background Execution Mode
支持central后台运行的模式
如果你的应用支持central角色的后台模式,也就是Info.plist中UIBackgroundModes键的值中包含bluetooth-central值。那么应用将可以在后台处理特定的蓝牙相关事件。即使在后台,你仍然可以发现和连接peripherals,可以检索和读写数据。并且当有CBCentralManagerDelegate or CBPeripheralDelegate 代理事件发生时,系统会唤醒应用来处理。
The bluetooth-peripheral Background Execution Mode
支持peripheral后台运行的模式
如果要支持peripheral角色的后台模式,你需要在Info.plist中的增加UIBackgroundModes键并在值中包含bluetooth-peripheral值。这样系统会唤醒应用来处理读写和订阅事件。