iOS SWIFT - 从中央检测外围设备关闭

问题描述 投票:0回答:2

Central 应用程序是否有任何方法可以检测外围设备何时耗尽电量并因此断开连接?

我试过用这个:

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { bleCentralManagerDelegate?.disconnectFromDeviceBLEResult(result: true)}

但是只有当外围设备请求实际断开连接时才会调用此事件,如果外围设备随机关闭则不会调用此事件。

谢谢

ios swift bluetooth-lowenergy core-bluetooth
2个回答
2
投票

可惜没有。一般的 BLE 断开连接通常将“断开连接原因”作为断开连接事件的一部分(请参阅this),但这并不可靠,并且在任何情况下 CoreBluetooth 都不会直接公开这一点。我说“不直接暴露这个”是因为你确实得到了 error 参数作为事件的一部分,但这不是直接映射到堆栈上发生的实际断开连接原因。

您唯一可以做的解决方法是自己添加智能。换句话说,当外围设备即将关闭或电池电量非常低时,它可以将该信息发送到中央设备(通过 GATT 写入/通知),让它知道它即将断开连接,因为电池电量不足低

查看以下链接以获取更多信息:-


0
投票

这是你要找的吗?

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    let cState = central.state
    
    switch cState {
    case .unknown:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .unknown")
      }
      
    case .resetting:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .resetting")
      }
      
    case .unsupported:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .unsupported")
      }
      
    case .unauthorized:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .unauthorized")
      }
      
    case .poweredOff:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .poweredOff")
      }
      
    case .poweredOn:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is .poweredOn")
      }
      centralManager.scanForPeripherals(withServices: servicesInterested)
      
    @unknown default:
      if ( preLogLevel == "LOGLEVEL" ) {
        CSVfuncs.writeLog(">>BT central.state is Unknown Default")
      }
      break
    // unknown default
    }
  }
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.