Ariel Malka
Ariel Malka

Reputation: 15987

iOS: scanForPeripheralsWithServices can't find device while nRF Connect app is able to find it

I have a BLE peripheral running on Raspberry Pi with gobbledegook.

In parallel, I have an iOS app working as BLE central which does a scanForPeripheralsWithServices with nil in order to detect all the devices around.

I'm able to detect a few devices, but not the one running on Raspberry Pi.
However, when scanning with the nRF Connect app on iOS, I'm able to see it.

How is that possible? (I assume that nRF Connect is also using Core Bluetooth...)

Upvotes: 0

Views: 551

Answers (1)

Rob Napier
Rob Napier

Reputation: 299355

You're likely doing something (such as using the nRF Connect app) to connect to the device, causing it to stop advertising. Most BLE platforms stop advertising when they are connected to.

Use retrieveConnectedPeripherals(withServices:) to detect devices that are already connected. You must provide a list of services; there is no way to detect all devices currently connected.

Since this is on a Pi, you can also verify on that side whether the system is really currently advertising and whether it has any connections. Core Bluetooth does extensive caching, which can lead to things appearing to be happen that actually are coming out of cache. I haven't recently dug into the nRF app to verify how much it relies on the cache. I recommend also testing with LightBlue, and testing with multiple devices simultaneously (I typically use multiple apps on iOS, Android, and Mac, and occasionally an Ubertooth, to verify what I'm seeing is true).

Note also that once you've seen an advertisement from a device once, you won't see any more during that scan unless you set CBCentralManagerScanOptionAllowDuplicatesKey to true in you scan options. That flag is ignored in the background. This probably isn't the issue in this case, but often trips people up who are used to other platforms. Similarly it is almost always faster and more reliable to scan for specific services rather than nil unless you're building a generic BLE scanner. (Again, I doubt that's the problem in this case.)

Upvotes: 2

Related Questions