CBPeripheral的已存储UUID多长时间可以重新连接? [英] How long is a CBPeripheral's stored UUID valid for reconnection?

查看:509
本文介绍了CBPeripheral的已存储UUID多长时间可以重新连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩Corebluetooth,并且我了解到,除了每次要连接时都要进行繁琐的扫描外围设备的过程之外,我还可以存储外围设备的UUID并将其与retrievePeripheralsWithIdentifiers和connectPeripheral结合使用

I've been playing with Corebluetooth and I've learnt that besides going through the tedious process of scanning for a peripheral every time I want to connect, I can also store the peripheral's UUID and use it with retrievePeripheralsWithIdentifiers and connectPeripheral for further reconnections.

原来,它只能工作一定时间,显然外围设备的UUID已更新,因此存储的UUID可以认为已过期。

Turns out that it only works for a certain amount of time, apparently the peripheral has its UUID updated, therefore the stored one can be considered expired.

我无法在此找到任何一致的文档,所以我不确定它能持续多久。

I haven't been able to find any consistent documentation on this, so I'm not sure how long it lasts for.

有什么办法可以使以后的重新连接比重新扫描外围设备更快?

Is there any way around this in order to allow later reconnections faster than scanning for peripherals all over again?

推荐答案

在我看来,直接持有NSArray或NSDictionary中的CBPeripheral对象是最简单的方法。保留CBP外围设备后,您可以根据需要进行多次连接和断开连接(无需每次扫描)。您唯一需要注意的是CBCentralManager的状态。您将通过实现CBCentralManagerDelegate centralManagerDidUpdateState:方法来接收状态更改。如果状态更改为CBCentralManagerStatePoweredOn以外的任何状态,则需要清除所有保留的CBPeripheral对象,并在状态重新打开后再次进行扫描。

In my opinion, directly holding onto the CBPeripheral objects in an NSArray or NSDictionary is the easiest way. Connect and disconnect as many times as you would like (without scanning each time) to a CBPeripheral once it is retained. The only thing you have to be mindful of is the CBCentralManager's state. You will receive state changes by implementing the CBCentralManagerDelegate centralManagerDidUpdateState: method. If the state changes to anything except CBCentralManagerStatePoweredOn then you need to get rid of all of your retained CBPeripheral objects and do another scan once the state turns back on.

苹果文档:


如果状态移到CBCentralManagerStatePoweredOff以下,则从此中央管理器获取的所有CBPeripheral对象将变为无效,必须重新检索或发现。

If the state moves below CBCentralManagerStatePoweredOff, all CBPeripheral objects obtained from this central manager become invalid and must be retrieved or discovered again.

但是,如果状态移到CBCentralManagerStatePoweredOn以下,我建议使所有CBPeripheral对象失效/释放。众所周知,CoreBluetooth有点漏洞,这似乎可以帮助我避免过去的无效的CBPeripheral警告。

However, I would suggest invalidating/releasing all of your CBPeripheral objects if the state moves below CBCentralManagerStatePoweredOn. It is no secret that CoreBluetooth is a little buggy and this has seemed to help me avoid "invalid CBPeripheral" warnings in the past.

如果您希望在重置应用程序或关闭设备甚至重置网络设置(缓存重置)之后识别特定的CBPeripheral,那么您将需要有一个自定义实现,可以唯一标识不基于CoreBluetooth免费提供给您的任何设备的另一台设备。

If you would like to identify a specific CBPeripheral after the app is reset or the device is shut down or even a network settings reset (cache reset) then you will need to have a custom implementation that uniquely identifies another device not based on anything CoreBluetooth offers you for free.

这是我在其中一个应用中的操作方式:

Here is how I do it in one of my apps:


  1. Alloc /首次启动应用程序时初始化NSUUID对象,并将其另存为NSString并保存在NSUserDefaults中。将此UUID视为您应用程序的伪MAC地址。此UUID在应用程序的生命周期内将保持不变。

  2. centralManager发现CBPeripheral,进行连接,然后读取特定特征的值

  3. CBPeripheral设备使用第1步中与蓝牙无关的UUID来响应请求。在这一步中,我还提供了一堆用户提供的信息以及UUID。

  4. centralManager保存了此信息如果尚未使用Core Data或NSUserDefaults,则使用UUID(以及您包含的任何其他信息)。

  5. 对所有新发现的CBPeripherals重复步骤2-4。

  1. Alloc/Init a NSUUID object the first time the app is launched and save it in NSUserDefaults as a NSString. Think of this UUID as a pseudo-MAC address for your app. This UUID will remain constant for the lifetime of the app.
  2. centralManager discovers a CBPeripheral, connects, and then reads the value for a specific characteristic
  3. CBPeripheral device responds to the request with its non-bluetooth-related UUID from step 1. I also include a whole bunch of user provided information in this step along with the UUID.
  4. centralManager saves this UUID (and any other information you include) using Core Data or NSUserDefaults if it hasn't already.
  5. Repeat steps 2-4 for all newly discovered CBPeripherals.

现在,只要CentralManager发现CBPeripheral,便可以检查其本地UUID数据库并毫无疑问地确定与该CBPeripheral相关的设备。

Now, whenever centralManager discovers a CBPeripheral it can check its local database of UUIDs and determine which device is related to this CBPeripheral without a doubt.

有关在后台如何重新连接的信息,请参阅Paulw11的答案。请注意,如果用户退出了您的应用程序(两次按下主屏幕按钮后向上滑动应用程序的预览),即使重新启动该应用程序,任何长期的连接都将被取消。

See Paulw11's answer for how reconnect while in the background. Note that if your app has been quit by the user (swipe the app's preview up after pressing the home button twice), any long term "connects" will be cancelled even if the app is restarted.

这篇关于CBPeripheral的已存储UUID多长时间可以重新连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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