I have reviewed similar questions on StackOverflow but they all deal with a second call to the CBManager delegate's didDiscover method as a result of scan response data being received. My scenario is different.
I am sitting at my desk home. My app is running on an iPhone 6, iOS 10.2. My app starts a scan without specifying any service uuids (i.e. discover all peripherals). My app does not connect to any of the peripherals that are discovered. Five different peripherals are being discovered, among them my development MacBook Pro which is connected to the iPhone via a USB/Lightening cable.
My app behaves oddly with regard to the MacBook Pro. The didDiscover method is being called for the MacBook Pro again and again, second after second. Finally it stops; until I move the MacBook Pro's cursor or tap a key on the keyboard; then it starts again. This does not occur for the other four peripherals, among them two Apple TVs.
Here is my didDiscover method:
@objc func centralManager(_ manager: CBCentralManager, didDiscover cbPeripheral: CBPeripheral, advertisementData data: [String : Any], rssi signalStrength: NSNumber) {
NSLog("Peripheral discovered: \(cbPeripheral)")
}
Here is a sample from the console:
I do not know how to think about this. I cannot come up with anything that I might try or check that would reveal more information about what is occurring. Any ideas will be much appreciated.
Update 1:
First some additional information:
- I am passing nil for the scanForPeripherals method's options parameter
- If I connect to the MacBook Pro then the frequency of the calls to didDiscover diminish from about every second to about every minute.
But, as CuriousRabbit pointed out, so what? Well, given that this behavior is not the result of some oversight on my part, what I am left with is: How do I code? At the moment, my best shot is:
- First call to didDiscover - Do what I do in response to a peripheral being discovered.
- Second call - There might be scan response data that is of interest; see here
- Subsequent calls - Ignore
Does this seem correct to you folks?
And, if the lesson learned here is to expect didDiscover to be called whenever the peripheral sends an advertising packet (re CuriousRabbit's "your MacBook is advertising aggressively" comment), then why do I not see this behavior for the other four peripherals?