Search code examples
iosobjective-ccore-bluetooth

Does a didLostPeripheral (or similar method) exists in CoreBluetooth


Does a didLostPeripheral method, the opposite of didDiscoverPeripheral:, exist in CoreBluetooth? A method that is called when peripheral stops advertisings. I am quite sure that the answer is no but I would rather be wrong than sorry. I didn't find any mentions of anything like that in CBCentralManagerDelegate.

Motivation
The idea is to be able to maintain a list of active peripherals advertising a specific service

Post Scriptum
I was hoping to find a delegate method similar to NSNetServiceBrowserDelegate, found in NSNetService, but that's a different framework with a different purpose.


Solution

  • As you suspect, the answer is “No”. If you are connected to a peripheral then you will get a didDisconnect delegate callback when a peripheral goes out of range.

    If your app is running in the foreground then you can use the allowDuplicates scanning key to receive advertising packets repeatedly. You can then maintain your own list of peripherals that you have seen.

    The general algorithm would be something like:

    • when a peripheral is seen (didDiscover) add it to the list, if not present and set the age to 0
    • Each second iterate over the list, incrementing the age. Remove any peripheral whose age is, say, 5