核心蓝牙 – 内置设备的恒定RSSI更新

我刚刚开始使用iOS的核心蓝牙框架,我正在开发一个应用程序,需要不断扫描BLE设备,以便我可以每分钟检索一次RSSI号码。

目前我有:

manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil]; [manager scanForPeripheralsWithServices:nil options:options]; 

这会启动我的应用程序扫描BLE设备,并在发现设备时调用此委托方法:

 - (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号码。 最后一行然后调用这个委托方法:

 - (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分钟进行一次扫描,但我不完全知道它为什么工作…

关于核心蓝牙的文档是相当稀疏的,所以如果任何人有任何想法如何做到这一点,或有更好的方式来做我想要完成我会很感激的帮助!

您是否尝试将扫描选项更改为YES?

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil]; [manager scanForPeripheralsWithServices:nil options:options]; 

如果你这样做,你会得到你的“didDiscoverPeripheral”callback与你的iPhone看到的每个广告数据包,这通常是每100毫秒(虽然我看到这个callback时间差异很大的同一个设备)。 这包括它看到的每个设备的RSSI。

这应该比你的1分钟更新速度快得多。

就我所知,这应该做你想做的事情。

当您开始使用原始呼叫扫描外围设备时,每当发现BLE设备时,您的代理应该开始接听电话。 这将继续,直到您通过呼叫停止扫描

 [manager stopScan]; 

我不认为你真的需要第二次调用你的中央pipe理器中的scanForPeripheralsWithServices:didRetrievePeripherals方法,因为据我所知,扫描不会停止,直到你告诉它。 尽pipe如此,我还是开始了这个工作,可能会有一个暂时没有find的地方。

我很确定你每分钟收到一次电话的原因是因为BLE设备只是经常发布广告。 如果广告更频繁,就像一个处于发现模式的设备,我想你会更频繁地接听电话。 如果你能证实,我会很有意思。 如果设备具有发现模式,则可以尝试触发它以查看通知是否加速。

你不应该连续扫描,因为它是非常昂贵的权力。 一旦你发现设备,你有一组CBPeripheral对象返回给你。 在CBPeripheral上,您可以读取RSSI并在RSSI更改时获得回叫。 请参阅以下文档: http : //developer.apple.com/library/mac/#documentation/CoreBluetooth/Reference/CBPeripheralDelegate_Protocol/translated_content/CBPeripheralDelegate.html

快速实施@Anders解决scheme:

 manager.scanForPeripheralsWithServices(nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(bool: true)])