Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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, even if the app is suspended by 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 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 called 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...