首页 > 解决方案 > CoreBluetooth / CBCentralManagerDelegate 中的一条奇怪消息

问题描述

在我的CBCentralManagerDelegate协议实现中,我具有以下功能。

func centralManager(_ central: CBCentralManager,
                    didDisconnectPeripheral peripheral: CBPeripheral,
                    error: Error?) {
    print(#function)
    if error != nil {
        print("Error in \(#function) :\n\(error!)")
        return
    }
    ......
    // More useful code irrelevant to the question.
}

调用上述函数时,我可以在Xcode调试控制台中看到以下消息。

centralManager(_:didDisconnectPeripheral:error:)
Error in centralManager(_:didDisconnectPeripheral:error:) :
Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." 
UserInfo={NSLocalizedDescription=The specified device has disconnected from us.}

这是我的问题:我一定遗漏了一些东西(因为太简单或太微妙),但为什么会显示错误,因为“指定的设备已与我们断开连接。

centralManager:didDisconnectPeripheral函数中,除了设备断开连接之外,我还能期待什么?

我希望一些开明的专家能带来一些启发,解释为什么会这样。

标签: iosswiftcore-bluetooth

解决方案


根据苹果文档: If the disconnection was not initiated by cancelPeripheralConnection(_:), the cause is detailed in error.

即,如果您断开连接,那么您不会收到任何错误,但如果他们断开连接,您会通过错误看到这一点。来源:https ://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/1518791-centralmanager


推荐阅读