2

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.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Saleh
  • 380
  • 3
  • 19

1 Answers1

2

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.

Adam Kaplan
  • 1,954
  • 17
  • 16
  • I attempt reconnect when central’s state is powered on. The callback to state restroration method is not called when I turn the Bluetooth on. I have put a log in it which writes on a file, nothing gets logged there. – Saleh Jan 01 '18 at 06:28
  • Hmm... I don't know what that would be since you are properly receiving other background messages. – Adam Kaplan Jan 01 '18 at 06:36
  • When I relaunch the app manually the peripheral state received in willRestoreState is connecting, but it never connects. – Saleh Jan 01 '18 at 16:25
  • I have edited my question to add central state method. Can you take a look? – Saleh Jan 06 '18 at 05:55
  • 1
    I'm stumped. However, this SO question appears to discuss some this issue a bit https://stackoverflow.com/questions/34290967/ble-background-reconnect . Check #5 on the accepted answer – it's what you are describing. However note that the OP editing his question after with some changes to address "point 5". Hope this helps. – Adam Kaplan Jan 06 '18 at 21:06
  • According to point 5 in the answer, state restoration is essentially useless if user turns his radio off. This is disappointing. – Saleh Jan 07 '18 at 07:03
  • Yes, but the original poster of that question edited his question after and appeared to say he found a usable solution to "point 5". I assume he was talking about #5 in the accepted answer. Look after where he says "regarding point 5 - my fall (sic, he meant fault?) - should use this key with connectPeripheral". It's worth a try to add those two key... but you may be out of luck. Empirically, my Fitbit seems to sync and I do turn bluetooth off periodically. – Adam Kaplan Jan 07 '18 at 21:56