How do you scan for the same BLE device twice?

最后都变了- 提交于 2020-01-06 03:07:20

问题


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];

This does not work.

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/26720630/how-do-you-scan-for-the-same-ble-device-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!