ios - 如何使用标识符连接外设/BLE 设备?

标签 ios swift bluetooth-lowenergy core-bluetooth

我想通过标识符连接外围设备。 我可以吗?

centralManager?.connect(peripheral, options: nil)

我在一个 ViewController 中扫描设备并在另一个 ViewController 中连接它。

以下是我的要求:

当我扫描设备并保存下一次可以出现在主屏幕上并直接连接的设备时。 因此,为此,我使用单例类将外围数据从一个 View 访问到另一个 View 。但它总是初始化为空。 所以我想使用标识符连接外围设备。 我不知道我们在哪里存储 [CBPeripheral] 并在需要时访问它。

最佳答案

我想使用标识符连接外围设备。我可以吗?

简答: 不,你不能这样做。

长答案:

Refer apple official document.

There are no provision or method which is used to connect with an identifier But, Apple has provision to retrieve peripherals by using the identifier.

让我们讨论一些其他的选择

CBPeripheral 没有实现 NSCoding。因此,几乎不可能将 BLEPeripheral 对象存储到 NSUserDefaults 或任何其他容器中。

最好的解决方案是存储 CBPeripheral 的属性,例如名称或标识符等。

UserDefaults.standard.set(Device_name, forKey: "Device_name")
UserDefaults.standard.set(Device_Identifier forKey: "Device_Identifier")

通过使用retrievePeripherals(withIdentifiers:),您可以再次获取BLEPeripheral 值。然后你可以直接使用这个对象连接

Here is the documentation

检索外设(withIdentifiers:)

Returns a list of known peripherals by their identifiers.

关于ios - 如何使用标识符连接外设/BLE 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897542/

相关文章:

ios - 使用 JSON 的 uitableview 部分出现问题

ios - TVOS Focus 在 collectionview cell 和另一个 cell 之间

ios - iOS 7 和 iOS8 中的 Alert View 与 UIAlertController

android - 如何知道 BLE 指示是否在 Android 中被确认

ios - 图像从不同区域上传到 AWS S3 立即失败,但如果与存储桶位于同一区域则成功

swift - 在 CALayer 中绘制 UIBezierPath 时上下文无效

ios - 在 iOS 设备上运行 clubbed swift & react-native 项目时出错

Android ble 设备有时不会断开连接

ios - 为什么第二部手机与设备配对后,CBCentralManager 会向第一部手机报告 "Peer removed pairing information"?

ios - 关于转义 URL 参数中的特殊字符的困惑