简体   繁体   English

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

[英]Unable to reconnect after cancelling BLE peripheral connection iOS

In my project I was successful to connect to a Bluetooth LE peripheral and read CBCharacteristic value from that device.在我的项目中,我成功连接到蓝牙 LE 外设并从该设备读取CBCharacteristic值。 I am facing an issue.我正面临一个问题。 I need to disconnect the peripheral and reconnect the device again if user wishes.如果用户愿意,我需要断开外围设备并重新连接设备。

I am using the following steps.我正在使用以下步骤。

1. For Disconnecting : I am calling centralManager?.cancelPeripheralConnection(peripheral) . 1. 对于断开连接:我正在调用centralManager?.cancelPeripheralConnection(peripheral) This call is completed successfully calling delegate func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)此调用成功完成调用委托func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)

2. To Reconnect : I start scanning for peripherals, as I did when the app launched centralManager!.scanForPeripheralsWithServices(nil, options: nil) 2. 重新连接:我开始扫描外围设备,就像我在应用程序启动centralManager!.scanForPeripheralsWithServices(nil, options: nil)时所做的那样

But this call never calls the delegate func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) where I try to connect the BLE discovered peripheral.但是这个调用从来没有调用委托func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)在那里我尝试连接 BLE 发现的外围设备。

My question is what is the best practice to disconnect a BLE peripheral and reconnect it again in iOS.我的问题是在 iOS 中断开 BLE 外设并重新连接的最佳实践是什么。 Am I doing anything wrong?我做错了什么吗?

Thats not the right way to re-connect to BLE.这不是重新连接到 BLE 的正确方法。

Per Apple Documentation :根据Apple 文档

Reconnecting to Peripherals重新连接到外围设备

Using the Core Bluetooth framework, there are three ways you can reconnect to a peripheral.使用 Core Bluetooth 框架,您可以通过三种方式重新连接到外围设备。 You can:你可以:

  1. Retrieve a list of known peripherals—peripherals that you've discovered or connected to in the past—using the使用
    retrievePeripheralsWithIdentifiers: method. retrievePeripheralsWithIdentifiers:方法。 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.使用retrieveConnectedPeripheralsWithServices:方法retrieveConnectedPeripheralsWithServices:当前连接到系统的外围设备列表。 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.使用scanForPeripheralsWithServices:options:方法扫描并发现外围设备。 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.这些步骤在发现正在广播的外围设备并在发现后连接到外围设备中进行了描述。

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

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