我正在开展一个与BLE相关的小项目。我要求我需要在手动关闭之后在后台重新连接设备,然后从iPhone->设置 - >蓝牙关闭蓝牙。
答案 0 :(得分:2)
只需存储外围设备标识符或(< iOS 7的UUID),检索外围设备,并在centralManager更新状态为启动时调用其上的连接。
适用于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
(以及其他)并在获得更新时取消所有当前的外围设备连接。