您如何两次扫描同一BLE设备? [英] How do you scan for the same BLE device twice?

查看:206
本文介绍了您如何两次扫描同一BLE设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,可以扫描BLE外设,其中可能有很多,并且允许用户决定是否连接到设备。连接后,用户还可以决定断开连接。所有这些都可以正常工作。

I have an iOS app that scans for BLE peripherals, of which there may be many, and allows the user to decide whether or not to connect to the device. Once connected, the user can also decide to break the connection. All of this works fine.

我还想添加一个选项,以允许用户改变主意并重新扫描设备,但这似乎是有问题的。最明显的方法似乎是停止扫描并重新启动它,如下所示:

I would also like to add an option to allow the user to change their mind and rescan for a device, but this seems to be problematic. The most obvious way to do this seems to be to stop scanning and restart it, as in:

    [centralManager stopScan];

    CBUUID *myServiceUUID = [CBUUID UUIDWithString: SERVICE_MYSERVICE];
    NSArray *services = [[NSArray alloc] initWithObjects: myServiceUUID, nil];
    [centralManager scanForPeripheralsWithServices: services options: nil];

这不起作用。

最终,iOS决定告诉我的应用该设备再次在做广告,但这需要几分钟。

Eventually, iOS decides to tell my app the device is advertising again, but this takes several minutes.

我如何才能说服iOS不用等待第二次就告诉我有关外围设备几分钟?

How can I convince iOS to tell me about the peripheral a second time without waiting several minutes?

推荐答案

您只需在启用 CBCentralManagerScanOptionAllowDuplicatesKey 选项时启用初始化您的 CBCentralManager 。当您的应用程序在前台运行时,您将获得不断的回调,但是,请记住, CBCentralManagerScanOptionAllowDuplicatesKey 在后台运行时被禁用。同样,扫描间隔被调节到其前景对应对象的〜60%。

You can just enable the CBCentralManagerScanOptionAllowDuplicatesKey option when you init your CBCentralManager. You'll get constant callbacks when your app is in the foreground, however, you need to keep in mind that the CBCentralManagerScanOptionAllowDuplicatesKey is disabled in when the app is in the background. Also, the scanning interval is throttled to a ~60th of its foreground counterpart.

这篇关于您如何两次扫描同一BLE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆