2014-09-22 61 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

相關問題