3

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:

enter image description here

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:

  1. I am passing nil for the scanForPeripherals method's options parameter
  2. 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:

  1. First call to didDiscover - Do what I do in response to a peripheral being discovered.
  2. Second call - There might be scan response data that is of interest; see here
  3. 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?

Community
  • 1
  • 1
Verticon
  • 2,419
  • 16
  • 34
  • Because your MacBook keeps saying in BLE "I'm Here" (advertising). The CBCentralManager "doesn't always keep track" of the device, it just responds to the advertisement by triggering the delegate method didDiscoverDevice. I guess that you set allow duplicate Keys in the scan method? – Larme Apr 11 '17 at 20:38
  • It keeps discovering your Mac because it is aggressive in its advertising? Why does it matter? What is the question here? – CuriousRabbit Apr 12 '17 at 00:17
  • @Larme I am passing nil for the scanForPeripherals method's options parameter. – Verticon Apr 12 '17 at 08:29
  • @CuriousRabbit Okay, I see it frequently because the MacBook advertises frequently. So, why do I never see it for the other peripherals? Nonetheless, I get you - so what? I am going to update my question. – Verticon Apr 12 '17 at 08:30
  • @Verticon - I don't know why other peripherals are not seen as often. I would guess the MacBook is not as conservative with power and has a high advertising frequency, but, lacking understanding, I won't speculate any further. – CuriousRabbit Apr 13 '17 at 19:56
  • @CuriousRabbit Thx. And, instead of "not as often" it is "never again". It is only the MacBook for which didDiscoverPeripheral is being called more than once. I watched for a few minutes. Oh well, some things are not meant to be understood; I'll just filter it out and get on with it. BTW: Do you have a thought on my "How to code" up above? In particular, step 2. – Verticon Apr 13 '17 at 20:10
  • @Verticon - Not understanding the goals, I can't be sure my approach is workable. I would start a scan, collect a set of peripherals, stop the scan, then, in a separate action, iterate over the collected peripherals connecting to them, discovering the characteristics I am interested in, reading the values, and disconnecting. Start from the top and do it again, and again. – CuriousRabbit Apr 13 '17 at 20:21
  • @CuriousRabbit All good. Thx for your attention. Good luck with your development efforts. – Verticon Apr 13 '17 at 20:32

1 Answers1

1

For the case where CBCentralManagerScanOptionAllowDuplicatesKey is not being specified, the Apple documentation (see the "Specify the CBCentralManagerScanOptionAllowDuplicatesKey Option Only When Necessary" section) states that repeat invocations of the didDiscoverPeripheral method will occur whenever the advertisement data changes.

What I am observing is that repeat calls also occur if the signal strength (rssi) changes. For the MacBook Pro the rssi value is bouncing around (albeit that there sometimes are two back to back log statements wherein it does not change but in general it is changing). The advertisement data is never changing. Also; on those rare occasions when disDiscoverPeripheral is repeated for one of the other peripherals, it is the rssi value that has changed.

I have a high degree of confidence that rssi changes explain the behavior that I am seeing.

Verticon
  • 2,419
  • 16
  • 34