简体   繁体   English

我可以在 iOS 中测量蓝牙信号强度吗?

[英]Can I measure Bluetooth signal strength in iOS?

Can I measure the signal strength of Bluetooth devices within range of my iPhone?我可以测量iPhone 范围内蓝牙设备的信号强度吗?

Basically what I want to do is scan for a list of devices within range, and then see which one has the highest signal strength.基本上我想做的是扫描范围内的设备列表,然后查看哪个具有最高的信号强度。

Is it possible in iOS and how would I do it if so?是否可以在 iOS 中使用,如果可以,我该怎么做?

Yes there is a way to measure the signal strength for Bluetooth Low Energy (4.0) it is the RSSI number.是的,有一种方法可以测量低功耗蓝牙 (4.0) 的信号强度,它是 RSSI 编号。 When you scan for peripherals you will set the flag CBCentralManagerScanOptionAllowDuplicatesKey to YES as shown below:当您扫描外围设备时,您将标志 CBCentralManagerScanOptionAllowDuplicatesKey 设置为 YES,如下所示:

NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@YES, CBCentralManagerScanOptionAllowDuplicatesKey, nil];

// Start scanning for peripherals
[cmanager scanForPeripheralsWithServices:nil options:dictionary];

If you want to see the RSSI number work without writing any code you should check out the app LightBlue in iTunes.如果您想在不编写任何代码的情况下查看 RSSI 编号,您应该查看 iTunes 中的应用程序LightBlue When you connect to a peripheral it will show you the updated RSSI number every second when it is connected.当您连接到外围设备时,它会在连接时每秒显示更新的 RSSI 编号。

Have a look to the CoreBluetooth documentation :看看CoreBluetooth 文档

- (void)centralManager:(CBCentralManager *)central 
 didDiscoverPeripheral:(CBPeripheral *)peripheral 
     advertisementData:(NSDictionary *)advertisementData 
                  RSSI:(NSNumber *)RSSI
{ ... }

RSSI is what you are looking for. RSSI就是您要寻找的。 Disclaimer: Core Bluetooth is made for Bluetooth 4 LE only.免责声明:核心蓝牙仅适用于蓝牙 4 LE。

If the exact range does not matter, but you are interested in scanning devices which are available in general, you may have a look to the github project BeeTee , which allows you to scan for all Bluetooth devices around you (not only Bluetooth LE).如果确切的范围无关紧要,但您对扫描一般可用的设备感兴趣,您可以查看 github 项目BeeTee ,它允许您扫描您周围的所有蓝牙设备(不仅是蓝牙 LE)。 Again disclaimer: I am the author of BeeTee.再次免责声明:我是 BeeTee 的作者。 ;-) ;-)

When you're coding your 'central' code using CBCentralManager, you'll eventually connect to the CBPeripheral you're looking for.当您使用 CBCentralManager 编写“中央”代码时,您最终将连接到您正在寻找的 CBPeripheral。 Once you're connected to the peripheral, keep a reference to it, set your object as the peripheral's delegate and invoke ' readRSSI ' on the peripheral.连接到外围设备后,保留对它的引用,将您的对象设置为外围设备的委托并在外围设备上调用“ readRSSI ”。 You'll get a delegate callback peripheral: didReadRSSI: error: If you write a method that invokes 'readRSSI', you can invoke it using performSelector: withObject: afterDelay: .你会得到一个委托回调外设: didReadRSSI: error:如果你编写了一个调用“readRSSI”的方法,你可以使用performSelector: withObject: afterDelay:来调用它。

Another of the suggested answers to this question is to supply the 'allow duplicates' key when scanning.这个问题的另一个建议答案是在扫描时提供“允许重复”键。 The docs for the dictionary key CBCentralManagerScanOptionAllowDuplicatesKey when passed to scanForPeripheralsWithServices:options: indicates that "Disabling this filtering can have an adverse effect on battery life and should be used only if necessary".当传递给scanForPeripheralsWithServices:options时,字典键CBCentralManagerScanOptionAllowDuplicatesKey的文档表示“禁用此过滤会对电池寿命产生不利影响,应仅在必要时使用”。

If you choose to write a delayed invocation, you can tune the frequency of the calls to help you manager the impact on your users' batteries.如果您选择编写延迟调用,则可以调整调用频率以帮助您管理对用户电池的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM