ios - 监控 CBPeripheral 状态变化

标签 ios swift swift3 core-bluetooth cbperipheral

所以有一种方法可以通过centralManagerDidUpdateState() 来监控CBCentralManager 的状态变化。但是是否有类似的方法调用来监控CBPeripheral的状态变化?我在 CoreBluetooth 库中找不到任何公开的内容,并且我想在 CBPeripheralState 更改时调用一个函数。

现在我只有一个检查外围设备状态的 switch 语句,但它总是会返回连接或断开连接。我将如何到达连接/断开连接的情况?我尝试将 switch 语句放在代码的各个部分,从 bleInit() 方法到 startScanning()、connectToPeripheral()

switch(peripheral.state){
case .disconnected:
    print("disconnected")
case .connecting:
    print("connecting")
case .connected:
    print("connected")
case .disconnecting:
    print("disconnecting")
default: break
}

最佳答案

连接和断开状态是过渡状态。您的连接只会在很短的时间内处于这些状态;这就是为什么你永远不会看到那些状态。

如果您的应用充当中心,则通过 CBCentralManagerdidConnectdidDisconnectPeripheral 方法通知外围连接状态。

关于ios - 监控 CBPeripheral 状态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239687/

相关文章:

ios - X.01 中的 iPhone 应用程序版本号

ios - Swift:以编程方式在一定距离内均匀地隔开 View ?

objective-c - 如何为手动创建的对象添加gestureRecognizer?

swift3 - 在 Swift 3 下使用 startDeviceMotionUpdates

ios - SwiftUI 未将自定义字体应用于选项卡式 View

ios - 将 firebase 数据放入数组

ios - 无法使用 Swift Closure 作为参数调用函数

ios - UITableView 重新加载数据不起作用

swift - 遍历数组的最后一个索引以外的所有索引

ios - 来自 UICollectionViewCell 中的 Class 对象的 UITableView 数据源