2014-09-22 85 views
3

我正在从AppDelegate的didFinishLaunchingWithOptions回调调用scanForPeripheralsWithServices。代码如下所示:从iOS 7升级到iOS 8后,didDiscoverPeripheral不会被调用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 
    NSLog(@"didFinishLaunchingWithOptions"); 
    // Override point for customization after application launch. 
    cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
    [cm scanForPeripheralsWithServices:nil 
           options:nil]; 
} 

- (void) centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI{ 

    NSLog(@"Did discover peripheral %@", peripheral.name); 

    [cm stopScan]; 
} 

升级到iOS 8一切之前运作良好,升级(完全相同的代码,没有一个单一的线被改变),我没有得到任何错误,但也didDiscoverPeripheral是后然而,没有被调用。

回答

4

我发现iOS 8中Core Core发生的主要变化是BLE堆栈在您尝试连接(或者可能发出其他命令)之前未上电。

CBCentralManager *cbCentralManager; 
[cbCentralManager scanForPeripheralsWithServices:...]; 

这个调用用来发出在Xcode调试日志说,中央管理器需要的是加电时,可以使用前一个警告可见。然而,这种警告始终是一个难题 - 为中央经理开启电源的唯一方式是向其发送消息,并且处理消息的唯一方式是启动它。

苹果似乎通过处理开机有点不同来解决这个问题。现在,在发布上述命令后,中央经理告诉其代表,其状态通过centralManagerDidUpdateState:更改。

我们通过重新发出scanForPeripherals...消息来回复centralManagerDidUpdateState:解决了您描述的问题。

+0

我也寻找这个问题的解决方案!非常感谢它完美的工作!我检查了central.state是否是PoweredOn,然后开始在那里扫描。我删除了第一个scanForPeripherals ... – philm5 2014-09-23 21:34:57

相关问题