蓝牙LE,scanForPeripheralsWithServices在后台,提高速度

时间:2021-08-27 15:24:41

I am using Bluetooth LE on iPhone 5S and I have done the following:

我在iPhone 5S上使用蓝牙LE,我做了以下工作:

  • I have a bluetooth peripheral and I have configured it to advertise each 20th ms on all three bluetooth advertising channels (37, 38 and 39).

    我有一个蓝牙外设,我已经配置它在所有三个蓝牙广告频道(37,38和39)上宣传每个20毫秒。

  • I have configured my app with UIBacgroundModes = bluetooth-central in Info.plist

    我在Info.plist中使用UIBacgroundModes = bluetooth-central配置了我的应用程序

  • I have started a scanForPeripheralsWithServices like below

    我已经启动了scanForPeripheralsWithServices,如下所示

Code:

码:

NSDictionary *options = @{
    CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier",
    CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]
};
self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];    
[self.manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"<uuid removed>"]] options:options];

The current state is:

目前的状态是:

  • In foreground mode the app quickly receives advertisement messages when I start my peripheral (within one second)

    在前台模式下,当我启动外围设备时,应用程序会快速接收广告消息(在一秒钟内)

  • In background mode the app sometimes receives advertisement messages within like 10 seconds after starting the peripheral (which can be acceptable even if it is bad) however often it does not receive the advertisement for a very long time (minutes or more) and this does not work for the application.

    在后台模式下,应用程序有时会在启动外围设备后的10秒内接收广告消息(即使它很糟糕也可以接受)但是通常它不会在很长时间内(几分钟或更长时间)收到广告而这不是为申请工作。

Question is:

问题是:

Is it in some way possible to increase the rate of which iOS is scanning for Bluetooth 4.0 LE peripherals when the scanning app is in background? Since I don't think there is a public API to do this, I am looking for a private API that is not published by Apple. This is for an internal enterprise app so a private API would be perfectly OK.

在扫描应用程序处于后台时,是否可以通过某种方式提高iOS扫描蓝牙4.0 LE外围设备的速率?由于我认为没有公共API可以做到这一点,我正在寻找一个Apple未发布的私有API。这适用于内部企业应用程序,因此私有API完全可以。

1 个解决方案

#1


5  

Background mode works differently for scanning.

背景模式对扫描的工作方式不同

  • Every peripheral is only reported the first single time it is observed (you cannot track proximity through RSSI without connecting to it while in background mode).

    每个外围设备仅在第一次观察时报告(在后台模式下,如果不连接RSSI,则无法跟踪RSSI的接近程度)。

  • You can initiate a connect request to a peripheral that's not within range, and the connection will complete when the peripheral becomes available. Don't have to actively scan for this (except for initial discovery, so you know which UUID to connect).

    您可以向不在范围内的外围设备发起连接请求,并在外围设备可用时完成连接。不必主动扫描(初始发现除外,因此您知道要连接哪个UUID)。

Maybe, you can solve the problem by sending a connect request instead of scanning, while in background. This way, iOS knows that you are really interested in a specific peripheral and I could imagine that this affects discovery times.

也许,您可以通过在后台发送连接请求而不是扫描来解决问题。这样,iOS知道你真的对特定的外围设备感兴趣,我可以想象这会影响发现时间。

Depending on your goal, a different approach could be to reverse the connection by placing the iPhone in peripheral mode and using your current LE peripherals in central mode. Seems hackish, but if it solves the problem for your case: why not :)

根据您的目标,不同的方法可以是通过将iPhone置于外围模式并在*模式下使用当前的LE外设来反转连接。看似hackish,但如果它解决了你的情况的问题:为什么不:)

Regarding a private API: sorry, I don't know one that affects scanning parameters.

关于私有API:抱歉,我不知道影响扫描参数的API。

#1


5  

Background mode works differently for scanning.

背景模式对扫描的工作方式不同

  • Every peripheral is only reported the first single time it is observed (you cannot track proximity through RSSI without connecting to it while in background mode).

    每个外围设备仅在第一次观察时报告(在后台模式下,如果不连接RSSI,则无法跟踪RSSI的接近程度)。

  • You can initiate a connect request to a peripheral that's not within range, and the connection will complete when the peripheral becomes available. Don't have to actively scan for this (except for initial discovery, so you know which UUID to connect).

    您可以向不在范围内的外围设备发起连接请求,并在外围设备可用时完成连接。不必主动扫描(初始发现除外,因此您知道要连接哪个UUID)。

Maybe, you can solve the problem by sending a connect request instead of scanning, while in background. This way, iOS knows that you are really interested in a specific peripheral and I could imagine that this affects discovery times.

也许,您可以通过在后台发送连接请求而不是扫描来解决问题。这样,iOS知道你真的对特定的外围设备感兴趣,我可以想象这会影响发现时间。

Depending on your goal, a different approach could be to reverse the connection by placing the iPhone in peripheral mode and using your current LE peripherals in central mode. Seems hackish, but if it solves the problem for your case: why not :)

根据您的目标,不同的方法可以是通过将iPhone置于外围模式并在*模式下使用当前的LE外设来反转连接。看似hackish,但如果它解决了你的情况的问题:为什么不:)

Regarding a private API: sorry, I don't know one that affects scanning parameters.

关于私有API:抱歉,我不知道影响扫描参数的API。