gpt4 book ai didi

ios - iOS取消BLE外设连接后无法重连

转载 作者:搜寻专家 更新时间:2023-10-31 08:33:09 26 4
gpt4 key购买 nike

在我的项目中,我成功地连接到蓝牙 LE 外围设备并从该设备读取 CBCharacteristic 值。我面临一个问题。如果用户愿意,我需要断开外围设备并重新连接设备。

我正在使用以下步骤。

<强>1。对于断开连接:我打电话centralManager?.cancelPeripheralConnection(外设)。此调用成功完成调用委托(delegate) func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)

<强>2。重新连接:我开始扫描外围设备,就像我在应用程序启动时所做的那样 centralManager!.scanForPeripheralsWithServices(nil, options: nil)

但此调用从不调用委托(delegate) func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) 我尝试连接 BLE 发现的外围设备.

我的问题是在 iOS 中断开 BLE 外设并重新连接的最佳做法是什么。我做错了什么吗?

最佳答案

那不是重新连接到 BLE 的正确方法。

根据 Apple Documentation :

重新连接到外围设备

Using the Core Bluetooth framework, there are three ways you can reconnect to a peripheral. You can:

  1. Retrieve a list of known peripherals—peripherals that you’ve discovered or connected to in the past—using the
    retrievePeripheralsWithIdentifiers: method. If the peripheral you’re looking for is in the list, try to connect to it. This reconnection
    option is described in Retrieving a List of Known Peripherals.
  2. Retrieve a list of peripheral devices that are currently connected to the system using the retrieveConnectedPeripheralsWithServices: method. If the peripheral you’re looking for is in the list, connect
    it locally to your app. This reconnection option is described in
    Retrieving a List of Connected Peripherals.
  3. Scan for and discover a peripheral using the scanForPeripheralsWithServices:options: method. If you find it, connect to it. These steps are described in Discovering Peripheral Devices That Are Advertising and Connecting to a Peripheral Device After You’ve Discovered It.

关于ios - iOS取消BLE外设连接后无法重连,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com