刷新众多蓝牙外设的RSSI值

lux*_*cem 1 bluetooth objective-c ios core-bluetooth bluetooth-lowenergy

我正在尝试从几个蓝牙外设测量 iOS(6 带 BLE)上的 RSSI 指示器。我可以通过 scanForPeripheral 获得 RSSI:

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

[_manager scanForPeripheralsWithServices:nil
                                 options:options];
Run Code Online (Sandbox Code Playgroud)

加上:

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
Run Code Online (Sandbox Code Playgroud)

这有效,但我无法控制接收数据包的速率,结果似乎不确定。

我读过:https : //stackoverflow.com/a/12486927/270209但我的速度根本不接近 100ms(更多 1~2 秒)

如果我连接到设备,readRSSI 的结果似乎更可靠。

我正在寻找一种方法来“刺激”外围设备以在扫描模式下进行更频繁的更新,或者一次连接到多个外围设备的方法。

谢谢

编辑:我也尝试过快速启动/停止扫描,似乎在开始扫描时检测到更多设备并且更新更频繁

小智 5

我相信你已经想通了,但以防万一有人遇到这个(就像我刚刚做的那样)寻找其他信息,如果你没有使用其他带有 coreLocation 的 iOS 设备,你需要peripheralDidUpdateRSSI:使用[self.myPeripheral readRSSI];.

您可以didUpdateValueForCharacteristic:在 updateValue 委托中随时调用 RSSI 数据更新。

在 viewDidLoad 中不需要这个: NSDictionary *options = etc...这个 NSDictionary 是在didDiscoverPeripheral:委托中创建的。

所以整体流程是:

检查您是否在获取 RSSI 数据的 NSLog 中接收 RSSI...

- (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 ...
}
Run Code Online (Sandbox Code Playgroud)

peripheralDidUpdateRSSI:在这里调用,因为如果您将通知值设置为 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 
}
Run Code Online (Sandbox Code Playgroud)

readRSSI 每次更新以前的特征值时,都会调用您在 UILabel(或您使用的任何东西)上执行 RSSI 更新的委托:

- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error;
{
_rssiLabel.text = [self.myPeripheral.RSSI stringValue];
NSLog(@"RSSI Method”);   
}
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序不需要特征值,只需在其中运行一个循环,无论您需要 RSSI 值刷新的时间如何。