I would like to have a list of ble devices to which my iOS can connect, which refreshes when ble devices appear and disappear.
In order to do that, I created an NSMutableDictionnary* peripheralsAvailable
, and everytime - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;
is called by the CBCentralManager, I add the peripheral to the peripheralsAvailable dictionnary (and then update a UITableView). All is OK here.
However I can't find how I can update the dictionnary if a Peripheral "disappear". It seems that I can only add peripherals in my dictionnary when they are detected, but I can't remove one when I shut it down for example.
Could you tell me if I'm missing something?
The OS will cache discovery of devices. I.e. you will only get one "discovery" event per device.
To continuously get discovery events while the peripheral is advertising, you must use the following option:
Setting the above option to
YES
, you could keep track of all of the peripherals that are advertising and when it stops advertising, you could remove it from the list.For a device that you've connected to, there is the
didDisconnectPeripheral
delegate event.Bluetooth devices don't advertise that they're about to go away, nor do they advertise that you're about to go out of range. You get an advertisement while they're advertising and you're within range, and you get nothing when you're out of range or they stop advertising. There's no event to trigger on when they're gone. You have to remember the devices that are advertising, and when they stop advertising (you haven't received an advertisement in awhile), you can remove it from the list.