简体   繁体   English

背景中的CoreBluetooth重新连接

[英]CoreBluetooth reconnection in background

I want my App to either be connected or attempting to connect to a known peripheral retrieved with -retrievePeripheralsWithIdentifiers or -retrieveConnectedPeripheralsWithServices . 我希望我的应用程序可以连接或尝试连接到使用-retrievePeripheralsWithIdentifiers-retrieveConnectedPeripheralsWithServices检索的已知外围设备。 When the peripheral disconnects, re-connection should be attempted. 当外围设备断开连接时,应尝试重新连接。 In this way, the App receives notifications from the peripheral and presents local push notifications. 这样,该应用程序会从外围设备接收通知,并显示本地推送通知。 This works fine when the App is in the foreground or background and still running. 当应用程序处于前台或后台且仍在运行时,此方法可以正常工作。

In the background, if the App is killed for whatever reason (user closes, memory low) then the system disconnects the peripheral, but the App does not get notified of disconnection, so cannot reconnect automatically to continue receiving notifications. 在后台,如果由于某种原因(用户关闭,内存不足)杀死了该应用程序,则系统会断开外围设备的连接,但是该应用程序不会收到断开连接的通知,因此无法自动重新连接以继续接收通知。 How can I attempt reconnection when the App is quit? 退出应用程序后如何尝试重新连接?

  1. Enable the bluetooth-central background mode 启用蓝牙中央背景模式

  2. Use the CBCentralManagerOptionRestoreIdentifierKey option supplying a unique identifier when instantiating your CBCentralManager 实例化CBCentralManager时,使用提供唯一标识符的CBCentralManagerOptionRestoreIdentifierKey选项

  3. Implement the - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)state delegate method. 实现-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)state委托方法。

3.1 Get an array of restorable peripherals using NSArray *peripherals = state[CBCentralManagerRestoredStatePeripheralsKey] 3.1使用NSArray * peripherals = state [CBCentralManagerRestoredStatePeripheralsKey]获取可恢复的外围设备数组

3.2 Call the connectPeripheral method of your CBCentralManager supplying each peripheral to restore 3.2调用CBCentralManager的connectPeripheral方法,提供每个外围设备以进行还原

Note :- Read more of the details in the Core Bluetooth Programming Guide 注意:-阅读《核心蓝牙编程指南》中的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM