[英]How to connect peripheral/ BLE device by using identifier?
我想使用标识符连接外围设备。 我可以做吗?
centralManager?.connect(peripheral, options: nil)
我在一个ViewController
扫描设备,然后在另一个ViewController
连接设备。
以下是我的要求:
当我扫描设备并下次保存时,该设备可以显示在主屏幕中并直接连接。 因此,为此,我使用单例类从一个视图访问另一视图的外围数据。 但是它总是初始化为空。 所以我想使用标识符连接外围设备。 我不知道我们在哪里存储[CBPeripheral]
并在需要时访问它。
我想使用标识符连接外围设备。 我可以做吗?
简短的回答:不,您不能这样做。
长答案:
没有用于与标识符连接的规定或方法,但是,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
值。 然后您可以使用该对象直接连接
retrievePeripherals(withIdentifiers :)
通过标识符返回已知外围设备的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.