iOS Swift 3中didDisconnectPeripheral未被调用

3

我正在尝试断开与一个BLE设备的连接。我正在执行cancelPeripheralConnection(),但它从未调用didDisconnectPeripheral()。 我正在将通知值设置为一个特征,所以在断开连接时,我将其设置为false。

{

BLEOperations.sharedInstance.connectedDevice?.setNotifyValue(false, for: BLEOperations.sharedInstance.peripheralCharacteristics[0])

BLEOperations.sharedInstance.manager.cancelPeripheralConnection(BLEOperations.sharedInstance.connectedDevice!)

}

我正在尝试在用户尝试连接新设备时断开已连接的外围设备。当前设备没有断开,但新设备与旧设备一起连接。
{

BLEOperations.sharedInstance.manager.connect(self.peripheralArray[indexPath.row] as! CBPeripheral , options: nil)

}

我在这里做错了什么?当设备连接丢失时,该方法被调用。

这段代码是否需要进行从Swift 2到3的迁移? - Andrea
有类似的问题,是否有解决方案? - Travis Griggs
@TravisGriggs 请查看已接受的回答。 - Hemanth
3个回答

2

当CentralManager在整个流程中只有一个实例时,它将正常工作。

您可以创建一个CentralManager操作类(单例)并执行操作,而不是在ViewController中使用centralmanagerdelegates。

我这样做了,它工作得很好。


1

好的,也许我们需要更多关于你的代码的信息。但是你正在寻找的方法,就是这个。

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    disconnected(central: central)
}

你应该将centralManager的代理设置为自己,每次外围设备断开连接时都应该调用代理。 经过一些测试,断开连接的过程并不像看起来那么顺利,你可以看到你的外围设备仍在断开连接,但代理已被调用(这可以通过硬件本身或外围设备上的固件进行调整)。
另外,你可以同时连接多个设备。

1
取消与外围设备的活动或挂起连接。
class ViewController: UIViewController, CBCentralManagerDelegate {

  var manager: CBCentralManager!
  var peripherals: [CBPeripheral]?

  @IBAction func actionScanBLEDevice(_ sender: AnyObject) {
     manager = CBCentralManager (delegate: self, queue: nil)
  }

  @IBAction func actionDisconnectBLE(_ sender: AnyObject) {
     //Use As per need where you want to disconnect perticular  peripheral
     manager.cancelPeripheralConnection(peripheral[0]) // Disconnent numbers of BLE Devices
  }

  func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
     //Manage Some Condition then disconnect
     print("Disconnected from peripheral")  
     peripherals?.append(peripheral)
  }  

  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    print("Discovered peripheral \(peripheral.name,peripheral.identifier.uuidString)")
    print("advertisementData\(advertisementData)")
    // Use discovered peripheral here 
  }

  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print("Checking")
    switch(central.state)
    {
    case.unsupported:
        print("BLE is not supported")
    case.unauthorized:
        print("BLE is unauthorized")
    case.unknown:
        print("BLE is Unknown")
    case.resetting:
        print("BLE is Resetting")
    case.poweredOff:
        print("BLE service is powered off")
    case.poweredOn:
        print("BLE service is powered on")
        print("Start Scanning")
        manager.scanForPeripherals(withServices: nil, options: nil)
    }
  }

}

1
我使用了所有上面建议的答案,但是当我关闭BLE设备时,didDisconnectPeripheral会被调用,但是在使用manager.cancelPeripheralConnection(connectedDevice)时没有被调用。 - Hemanth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接