我正在做一个与 BLE 相关的小项目。我有一个要求,我需要在手动关闭后在后台重新连接设备,然后从 iPhone-> 设置-> 蓝牙关闭蓝牙。
问问题
1957 次
1 回答
2
只需存储外围设备标识符或(< iOS 7 的 UUID),检索外围设备,并在 centralManager 将状态更新为已打开时调用 connect 。
对于 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
(以及其他)并取消所有当前的外围设备连接。
于 2013-09-30T17:45:55.007 回答