Reputation: 832
I am working on a small project related to BLE. I have a requirement that I need to re-connect the device in background after manual turn off and then off bluetooth from iPhone->Settings->Bluetooth.
Upvotes: 2
Views: 2010
Reputation: 13549
Just store the peripheral identifier or (UUID for < iOS 7), retrieve the peripheral, and call connect on it when the centralManager updates state to powered on.
For iOS 7:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere
NSArray *peripherals = [_cbCentralManager retrievePeripheralsWithIdentifiers:@[uuid]];
for(CBPeripheral *periph in peripherals)
{
[_cbCentralManager connectPeripheral:periph options:nil];
}
}
}
For iOS 6:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
CFUUIDRef uuid;//the cfuuidref you've previously saved
[central retrievePeripherals:@[(id)uuid]];//now wait for the delegate callback below
}
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
for(CBPeripheral *periph in peripherals)
{
[_centralManager connectPeripheral:periph options:nil];
}
}
NOTE: these are just code snippets. You should also monitor CBCentralManagerStatePoweredOff
(among others) and cancel all current peripheral connections when you get that update.
Upvotes: 2