問題描述
我剛開始使用 iOS 的核心藍牙框架,我正在開發一個需要不斷掃描 BLE 設備的應用程序,以便我可以每分鐘左右檢索它們的 RSSI 編號.
I just started with the core bluetooth framework for iOS and I'm developing an app that needs to constantly scan for BLE devices so that I can retrieve their RSSI number every minute or so.
目前我有:
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
這將啟動我的應用程序掃描 BLE 設備并在發現設備時調用此委托方法:
this starts my app scanning for BLE devices and calls this delegate method when a device is discovered:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData);
//Do something when a peripheral is discovered.
rssiLabel.text = [RSSI stringValue];
[manager retrievePeripherals:[NSArray arrayWithObject:(id)peripheral.UUID]];}
這個方法可以得到我可以顯示的外圍設備的 RSSI 號碼.最后一行然后調用這個委托方法:
this method gets me the peripheral's RSSI number which i can display. The last line then calls this delegate method:
- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals {
NSLog(@"Currently known peripherals :");
int i = 0;
for(CBPeripheral *peripheral in peripherals) {
NSLog(@"[%d] - peripheral : %@ with UUID : %@",i,peripheral,peripheral.UUID);
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
}
這段代碼似乎在工作,并且大約每 1 分鐘掃描一次,但我不知道它為什么工作......
This code seems to be working and doing a scan roughly every 1 minute, but I don't exactly know why it working...
關于核心藍牙的文檔非常少,所以如果有人對如何做到這一點有任何想法,或者有更好的方法來完成我想要完成的工作,我將不勝感激!
The documentation on core bluetooth is pretty sparse so if anyone has any idea on how to do this, or has a better way to do what I'm trying to accomplish I'd appreciate the help!
推薦答案
你試過把掃描選項改成YES嗎?
Have you tried changing the scan option to YES?
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
如果您這樣做,您的 iPhone 看到的每個廣告包都會收到您的didDiscoverPeripheral"回調,通常大約每 100 毫秒(盡管我發現對于同一設備,此回調時間變化很大).這包括它看到的每個設備的 RSSI.
If you do this you will get your "didDiscoverPeripheral" callback with every ad packet that is seen by your iPhone, which would normally be about every 100ms (although I see this callback timing varying a lot for the same device). This includes the RSSI of each device it sees.
這應該比您大約 1 分鐘的更新速度快很多.
This should be a lot faster than your ~1 minute update rate.
這篇關于核心藍牙 - 范圍內設備的持續 RSSI 更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!