【问题标题】:How to connect peripheral/ BLE device by using identifier?如何使用标识符连接外围设备/ BLE 设备?
【发布时间】:2019-04-29 05:38:37
【问题描述】:

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

centralManager?.connect(peripheral, options: nil)

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

以下是我的要求:

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

【问题讨论】:

    标签: ios swift bluetooth-lowenergy core-bluetooth


    【解决方案1】:

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

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

    长答案:

    Refer apple official document.

    没有任何规定或方法可用于连接 标识符 但是,Apple 已经规定通过使用检索外围设备 标识符。

    让我们讨论一些其他的替代方案

    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

    retrievePeripherals(withIdentifiers:)

    按标识符返回已知外围设备的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多