无法在iOS上恢复蓝牙状态
几周来,我一直在试图弄清楚iOS中核心蓝牙的状态保存和恢复是如何工作的,但我完全不知所措 到目前为止,我已经做了以下工作: 向我的CBCentralManager添加了恢复标识符无法在iOS上恢复蓝牙状态,ios,xcode,bluetooth,Ios,Xcode,Bluetooth,几周来,我一直在试图弄清楚iOS中核心蓝牙的状态保存和恢复是如何工作的,但我完全不知所措 到目前为止,我已经做了以下工作: 向我的CBCentralManager添加了恢复标识符 centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManager"}];
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManager"}];
这将使我的应用程序能够调用委托方法:
centralManager:(CBCentralManager *)central
willRestoreState:(NSDictionary *)dict;
在这里,我尝试重新连接到我的外围设备,如果我在应用程序挂起之前有连接,否则我将尝试再次扫描我的外围设备,如下所示:
if (dict[CBCentralManagerRestoredStatePeripheralsKey]) {
for (CBPeripheral *currentPeripheral in peripherals) {
peripheral = currentPeripheral;
}
// Connect to peripheral
}
else{
// Scan for UUID
}
我这样做是对的,还是很接近,还是完全错了?现在对我来说很有效。
我初始化我的中央管理器,就像我最初做的那样,然后在willRestoreState方法中运行以下代码:
NSArray* peripherals = [central retrieveConnectedPeripheralsWithServices:[NSArray arrayWithObject:UUID]];
if ([peripherals count] > 0) {
_discoveredPeripheral = [peripherals objectAtIndex:0];
_discoveredPeripheral.delegate= self;
[central connectPeripheral:_discoveredPeripheral options:nil];
} else {
[self scan];
}
我有一个类似的问题,无法确定我的代码是否在willRestoreState方法中工作。我添加了一个答案,这个答案对我有效,我希望它也对您有效:)另外,为了测试它是否工作,您可以使用这个kill(getpid(),SIGKILL);要模拟您的应用程序被操作系统杀死:)祝您好运!您好@newbiesovs,在willRestoreState,我也很高兴能够获取检索到的外围设备,但无法连接到所有外围设备,根据苹果文档,当应用程序在后台启动时,我们只有10秒的时间来执行任务。因此,在给定的时间内,无法完成连接系统中多个外围设备的任务。你也遇到过同样的问题吗?你对此有何评论?这将非常有帮助。谢谢。你能发布你的代码项目吗?我也有类似的问题。谢谢