刷新许多蓝牙外围设备的RSSI值 [英] Refreshing RSSI value of many bluetooth peripherals
问题描述
我正在尝试从多个蓝牙外设确保iOS上的RSSI指示器(带BLE的6). 我可以使用scanForPeripheral获取RSSI:
I'm trying to mesure RSSI indicator on iOS (6 with BLE) from several bluetooth peripheral. I can get RSSI with scanForPeripheral :
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[_manager scanForPeripheralsWithServices:nil
options:options];
加上:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
这可行,但是我无法控制数据包的接收速率,结果似乎不确定.
this works but I have no control on the rate of packets receive and the result seems uncertain.
我已阅读: https://stackoverflow.com/a/12486927/270209 ,但我的评分不是完全接近100毫秒(超过1到2秒)
I've read : https://stackoverflow.com/a/12486927/270209 but my rate is not close to 100ms at all (more 1~2 seconds)
如果我连接到设备,则readRSSI的结果似乎更可靠.
If I'm connected to the device the result with readRSSI seems more reliable.
我正在寻找一种激发"外围设备以使其在扫描模式下更频繁地更新的方法,或者一次连接至多个外围设备的方法.
I'm looking for a way to "stimulate" peripherals for more frequents updates in scan mode or a way to connect to more than one peripheral at a time.
谢谢
我也尝试过快速启动/停止扫描,看来在开始扫描时可以检测到更多设备并且更新频率更高
Edit : I've also tried to start / stop scan quickly, it seems that at start scan detects more devices and updates are more frequent
推荐答案
我确定您已经知道了这一点,但以防万一有人遇到此问题(如我刚才所做的那样)以寻找其他信息,如果您如果不将其他iOS设备与coreLocation一起使用,则需要使用[self.myPeripheral readRSSI];
调用peripheralDidUpdateRSSI:
委托.
I'm sure you've already figured this out but just in case someone comes across this (like I just did) looking for other info, if you're not using other iOS devices with coreLocation you need to call the peripheralDidUpdateRSSI:
delegate using [self.myPeripheral readRSSI];
.
您可以根据需要在didUpdateValueForCharacteristic:
中一次调用RSSI数据更新,一次在updateValue委托中.
You can call for RSSI data updates in didUpdateValueForCharacteristic:
as often as you like once in the updateValue delegate.
在viewDidLoad中不需要此:
NSDictionary *options = etc...
此NSDictionary在didDiscoverPeripheral:
委托中创建.
You don't need this in viewDidLoad:
NSDictionary *options = etc...
This NSDictionary is created in the didDiscoverPeripheral:
delegate.
因此总体流程为:
在您获取RSSI数据的NSLog中检查您是否正在接收RSSI ...
Check that you are receiving RSSI in the NSLog where you obtained the RSSI data...
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if ([localName length] > 0){
NSLog(@"Discovered: %@ RSSI: %@", peripheral.name, RSSI);
// your other needs ...
}
在此处调用peripheralDidUpdateRSSI:
,因为如果将通知值设置为YES,这将连续更新.
Call peripheralDidUpdateRSSI:
here since this is where updates will occur continuously if you've set notify value to YES [peripheral setNotifyValue:YES forCharacteristic:characteristic];
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
//Call the peripheralDidUpdateRSSI delegate ..
[self.myPeripheral readRSSI];
// do all of your other characteristic value updates here
}
每次更新以前的特征值时,
readRSSI
都会在您在UILabel(或您使用的任何东西)上执行RSSI更新的地方调用该委托:
readRSSI
will call the delegate where you perform your RSSI updates on your UILabel (or whatever you're using) every time you update the previous characteristics values:
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error;
{
_rssiLabel.text = [self.myPeripheral.RSSI stringValue];
NSLog(@"RSSI Method");
}
如果您不需要应用程序的特征值,则可以在其中以需要RSSI值刷新的任何时间在其中运行循环.
If you don't need characteristic values for your app just run a loop in there with whatever timing you need the RSSI value to refresh.
这篇关于刷新许多蓝牙外围设备的RSSI值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!