为什么CoreBluetooth一次又一次地发现相同的外围设备? [英] Why Is CoreBluetooth Discovering the Same Peripheral Again, and Again, and Again?

查看:82
本文介绍了为什么CoreBluetooth一次又一次地发现相同的外围设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了关于StackOverflow的类似问题,但是由于收到了扫描响应数据,它们都处理了对CBManager委托的didDiscover方法的第二次调用。我的情况与众不同。

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.

我正坐在办公桌旁。我的应用程序在iPhone 6,iOS 10.2上运行。我的应用程序开始扫描时未指定任何服务uuid​​(即发现所有外围设备)。我的应用程序未连接到发现的任何外围设备。正在发现五个不同的外围设备,其中包括我开发的MacBook Pro,它是通过USB / Lightning电缆连接到iPhone的。

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.

我的应用程序对MacBook的表现很奇怪亲MacBook Pro一次又一次地调用didDiscover方法。终于停止了;直到我移动MacBook Pro的光标或点击键盘上的键;然后它再次开始。对于其他四个外围设备(包括两台Apple TV),不会发生这种情况。

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.

这是我的didDiscover方法:

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.

更新1:

首先要多信息:


  1. 我为scanForPeripherals方法的options参数传递了nil

  2. 如果我连接到然后,MacBook Pro呼叫didDiscover的频率从大约每秒减少到大约每分钟。

但是,正如CuriousRabbit指出的那样,所以呢?好吧,鉴于这种行为不是我的疏忽造成的,我所剩下的就是:我该如何编码?目前,我最好的镜头是:

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. 第一次致电didDiscover-做我为响应被发现的外围设备所做的事情。
  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

这对您来说是否正确?

而且,如果从这里学到的教训是,每当外围设备发送广告数据包时,就会调用didDiscover(有关CuriousRabbit的您的MacBook正在积极宣传的评论),那么为什么我看不到其他四个外围设备的这种行为呢?

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?

推荐答案

对于未指定CBCentralManagerScanOptionAllowDuplicatesKey 的情况, Apple document 仅可选部分指出,只要广告数据发生更改,就会重复执行didDiscoverPeripheral方法。

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.

我观察到的是,如果信号强度(rssi)会发生变化。对于MacBook Pro,rssi值会反弹(尽管有时会有两个背对背的日志语句,虽然它们没有变化,但总的来说在变化)。广告数据从不改变。也;在极少数情况下,对于其他外设之一重复了DisDiscoverPeripheral时,则是rssi值发生了变化。

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.

我对rssi的变化可以解释的原因充满信心我看到的行为。

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

这篇关于为什么CoreBluetooth一次又一次地发现相同的外围设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆