取消 BLE 外设连接后无法重新连接 iOS
Unable to reconnect after cancelling BLE peripheral connection iOS
在我的项目中,我成功连接到蓝牙 LE 外围设备并从该设备读取 CBCharacteristic
值。我面临一个问题。如果用户愿意,我需要断开外围设备并重新连接设备。
我正在使用以下步骤。
1.对于断开连接:
我打电话
centralManager?.cancelPeripheralConnection(peripheral)
。此调用已成功调用委托 func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)
2。重新连接:我开始扫描外围设备,就像应用程序启动时所做的那样centralManager!.scanForPeripheralsWithServices(nil, options: nil)
但是这个调用从不调用委托 func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
,我在其中尝试连接 BLE 发现的外围设备。
我的问题是在 iOS 中断开 BLE 外围设备并重新连接的最佳做法是什么。我做错了什么吗?
这不是重新连接到 BLE 的正确方法。
正在重新连接到外围设备
Using the Core Bluetooth framework, there are three ways you can
reconnect to a peripheral. You can:
- 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.
- 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.
- 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.
在我的项目中,我成功连接到蓝牙 LE 外围设备并从该设备读取 CBCharacteristic
值。我面临一个问题。如果用户愿意,我需要断开外围设备并重新连接设备。
我正在使用以下步骤。
1.对于断开连接:
我打电话
centralManager?.cancelPeripheralConnection(peripheral)
。此调用已成功调用委托 func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)
2。重新连接:我开始扫描外围设备,就像应用程序启动时所做的那样centralManager!.scanForPeripheralsWithServices(nil, options: nil)
但是这个调用从不调用委托 func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
,我在其中尝试连接 BLE 发现的外围设备。
我的问题是在 iOS 中断开 BLE 外围设备并重新连接的最佳做法是什么。我做错了什么吗?
这不是重新连接到 BLE 的正确方法。
正在重新连接到外围设备
Using the Core Bluetooth framework, there are three ways you can reconnect to a peripheral. You can:
- 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.- 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.- 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.