我刚开始使用 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 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!