简体   繁体   中英

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 . 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

  3. Implement the - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)state delegate method.

3.1 Get an array of restorable peripherals using NSArray *peripherals = state[CBCentralManagerRestoredStatePeripheralsKey]

3.2 Call the connectPeripheral method of your CBCentralManager supplying each peripheral to restore

Note :- Read more of the details in the Core Bluetooth Programming Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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