Turning bluetooth radio back on after the app is suspended doesn't call centralManagerDidUpdateState

♀尐吖头ヾ 提交于 2019-11-28 09:52:18

问题


My app connects with a low energy peripheral when the peripheral goes out of range, I get didDisconnect method callback, I simply call connect on the peripheral and whenever it comes back into range it connects.

Even in the background, if the app is suspended the by the iOS but since I have a pending connection, it wakes the app up and connects.

However, if the user turns the Bluetooth off, all the peripherals go into disconnected state, hence no pending connection remains. If the app is suspended by the iOS, and the user turns it back on after suspension, none of my delegate methods are called, I have added my initialization and state restoration methods below.

I initialize my central manager on background queue but whenever I receive a callback, I get the main queue to execute tasks:

- (void)initialize {
if (!self.centralManager) {
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) options:@{ CBCentralManagerOptionRestoreIdentifierKey:@"CBCentralManagerIdentifierKey" }];
}
}

My central state callback method

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

dispatch_async(dispatch_get_main_queue(), ^{
    [SFUtil writeLog:@"centralManagerDidUpdateState"];
    if (central.state == CBManagerStatePoweredOff) {
        [SFUtil writeLog:@"CBManagerStatePoweredOff"];
        [[NSNotificationCenter defaultCenter] postNotificationName:CB_MANAGER_BLUETOOTH_POWERED_OFF object:nil];
    }
    else if (central.state == CBManagerStatePoweredOn) {
        [SFUtil writeLog:@"CBManagerStatePoweredOn"];
        [self restoreConnections]; // here I reconnect to already known devices, retrieved from calling central method of retrievePeripheralsWithIdentifiers
        [[NSNotificationCenter defaultCenter] postNotificationName:CB_MANAGER_BLUETOOTH_POWERED_ON object:nil];
    }
});

}

My central restoration method:

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

dispatch_async(dispatch_get_main_queue(), ^{
    [DataManagerInstance startBackgroundTaskIfInBackground]; // Here, I start a background task.
    [self initialize];
   });
}

I need to reconnect to the peripheral in the background when user reopens the app, but since centralManagerDidUpdateState method is never call when user turns the bluetooth back on from control centre or from settings. I cannot send the connect call.

When I manually launch the app, peripheral is in connecting state but doesn't reconnect.


回答1:


Are you monitoring for state changes in the CBCentralManager? You should get a delegate callback when Bluetooth is turned off and on, documented here:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBManagerStatePoweredOn) {
        // reconnect/scan/etc
    }
}

It sounds like you are using Core Bluetooth State Preservation and Restoration, which is supposed to notify you on these state changes.

Also, I can't try it out right now, but you might be able to also attempt-reconnect when bluetooth is off since connect requests do not time out.



来源:https://stackoverflow.com/questions/48030480/turning-bluetooth-radio-back-on-after-the-app-is-suspended-doesnt-call-centralm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!