我解決了這個類型的問題與此代碼,每個廣告被處理的時間基本上只是重新啓動掃描。我遇到了CBCentralManager實例停止監聽外設的問題。
(設置CBCentralManagerScanOptionAllowDuplicatesKey
到@YES
沒有完全解決這個問題對我來說)
假設類實現CBCentralManagerDelegate:
- (id) init {
self.central = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
[self initScan];
}
- (void) initScan {
[self.central stopScan];
[self.central scanForPeripheralsWithServices:nil
options:[NSDictionary dictionaryWithObjectsAndKeys:@NO, CBCentralManagerScanOptionAllowDuplicatesKey, nil]];
}
- (void) centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI {
//
// Do stuff here
//
[self initScan];
}
是的,但我需要不斷獲得更新的RSSI。我的發現是,一旦中央管理器發現並重新發現外設幾次,即使掃描繼續,它也會停止發現它(似乎大約3秒鐘)。我剩下的就是需要反覆重新啓動掃描以嘗試獲取更新的RSSI值。 – Jonathan
是的,您可以重新啓動didDiscoverPeripheral函數中的掃描,並且您將快速獲取RSSI。 – Aboelseoud
謝謝。這樣可行。爲了節省電力,我實際上在100ms後停止掃描,然後每秒鐘做100ms的爆發。很棒。 – Jonathan