[英]Continuous scanning for iOS CoreBluetooth Central Manager?
低功耗藍牙規范並未說明外設是否可以一次連接到多個中心,但我的測試經驗告訴我他們不能。
因為我的應用程序需要與外圍設備的非占有關系(即沒有連接,這會阻止其他連接),並且需要不斷更新其RSSI值,我正在尋找一種方法來連續掃描外設並捕獲它們的RSSI值。
scanForPeripheralsWithServices方法似乎掃描一定的間隔然后停止。 我相信我最好的選擇是一次掃描3秒,stopScan,等待(幾秒鍾),然后重新開始掃描。 重復。
任何人都可以指出一種更好的方法嗎? 例如,配置外圍設備以連接到多個Central?
外圍設備無法連接到多個中心。 但是如果你需要捕獲RSSI,那么你甚至不需要連接。 掃描設備可以使用此功能檢索RSSI:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
至於上一個答案,如果您只對RSSI感興趣,可以直接將其放入委托方法:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
順便說一句,默認情況下, CBCentralManager
只會調用此方法一次。 如果每次CBCentralManager
收到廣告包時都需要調用此回調,則需要使用選項CBCentralManagerScanOptionAllowDuplicatesKey
設置為YES
來啟動掃描:
NSDictionary *scanningOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey: @YES};
[centralManager scanForPeripheralsWithServices:nil options:scanningOptions];
請注意,如果不是絕對必要,Apple會阻止使用此選項。
我用這段代碼解決了這類問題,基本上只是在每次處理廣告時重新啟動掃描。 我遇到了同樣的問題,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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.