繁体   English   中英

iOS是否可以通过指定)(CBPeripheral *)peripheral而不是retrievePeripherals来重新连接到Bluetooth LE外围设备?

[英]Can iOS re-connect to Bluetooth LE peripheral by specifying )CBPeripheral *)peripheral instead of doing retrievePeripherals?

当我得到委托didConnectPeripheral:(CBPeripheral *)peripheral时,我可以只将外围设备存储在一个数组中,然后用它稍后重新连接,而不是使用retrievePeripherals及其后续的didRetrievePeripherals吗? 如果可行且没有风险,似乎会更容易。

(CBPeripheral *)peripheral可以再使用(CBPeripheral *)peripheral 与该外围设备断开连接后,它仍然有效吗?

工作流程:

  1. scanForPeripheralsWithServices() -扫描外围设备
  2. didDiscoverPeripheral:(CBPeripheral *)peripheral检测到connectPeripheral:peripheral
  3. didConnectPeripheral:(CBPeripheral *)peripheral stopScan并存储(CBPeripheral *)peripheral供以后使用。
  4. ...读取或写入特征...
  5. cancelPeripheralConnection
  6. didDisconnectPeripheral

稍后,重新连接...

  1. connectPeripheral:peripheral来自具有外围设备的阵列
  2. didConnectPeripheral:(CBPeripheral *)peripheral ...

是的,它会起作用(但这是很糟糕的做法)。 retrievePeripherals:方法是专门创建的,因此您可以在应用程序的后续启动之间重新连接到外围设备。 您可以使用您的方法,但是一旦应用程序关闭,您将永远无法再次连接到外围设备(无需将其置于广告模式并基本上从头开始)。 您可以在CBPeripheral启动之间存储uuid,但不能存储CBPeripheral对象。 因此,这里有很大的缺点。

综上所述:它可以工作,但并不能真正为您带来任何好处。 它不比调用retrievePeripherals:然后连接它们快。 建议的方法仅限制CoreBluetooth连接能力。 但是仍然有一个有趣的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM