2
我正在研究一个与BLE相关的小项目。我有一个要求,我需要在手动关闭后在后台重新连接设备,然后从iPhone->设置 - >蓝牙关闭蓝牙。关闭并重新打开后,如何在后台重新连接BLE设备?
我正在研究一个与BLE相关的小项目。我有一个要求,我需要在手动关闭后在后台重新连接设备,然后从iPhone->设置 - >蓝牙关闭蓝牙。关闭并重新打开后,如何在后台重新连接BLE设备?
只需存储外围设备标识符或(UUID为< iOS 7),检索外围设备,并在中央管理器更新状态为启动时调用连接。
对于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];
}
}
}
对于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];
}
}
注:这些只是代码段。您还应该监视CBCentralManagerStatePoweredOff
(其中包括)并取消所有当前的外围设备连接。