我想通过标识符连接外围设备。 我可以吗?
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
值。然后你可以直接使用这个对象连接
检索外设(withIdentifiers:)
Returns a list of known peripherals by their identifiers.
关于ios - 如何使用标识符连接外设/BLE 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897542/