跳到主要内容

查看佩戴检测状态

读取佩戴检测是否启用,以及当前没有佩戴、佩戴一侧还是双侧佩戴。

前置条件

  • 设备已连接并就绪。
  • 设备遵循 DeviceWearDetectionAPI

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds

协议

Swift
/// The protocol for device wear detection API.
protocol DeviceWearDetectionAPI: DeviceAPI {
    /// Indicates whether wear detection is currently enabled
    var isWearDetectionEnabled: Bool { get }

    /// The current wear status of the device.
    var wearStatus: WearStatus { get }
}

属性

属性类型说明
isWearDetectionEnabledBool / BOOL当前是否启用佩戴检测。
wearStatusWearStatus当前左右耳佩戴状态。

佩戴状态值

SwiftObjective-C含义
.unknownAIBudsWearStatusUnknown佩戴状态未知。
.notWearingAIBudsWearStatusNotWearing两侧均未佩戴。
.leftWearingAIBudsWearStatusLeftWearing仅佩戴左侧。
.rightWearingAIBudsWearStatusRightWearing仅佩戴右侧。
.bothWearingAIBudsWearStatusBothWearing两侧均已佩戴。

使用示例

Swift
import AIBuds

guard let device = device as? DeviceWearDetectionAPI else {
    print("Device does not support wear detection")
    return
}

print("Wear detection enabled: \(device.isWearDetectionEnabled)")

switch device.wearStatus {
case .unknown:
    print("Wear state is unknown")
case .notWearing:
    print("Neither side is worn")
case .leftWearing:
    print("Only the left side is worn")
case .rightWearing:
    print("Only the right side is worn")
case .bothWearing:
    print("Both sides are worn")
@unknown default:
    print("Unsupported wear-state value")
}

错误处理

这些属性是同步快照,不返回错误。请处理不支持的协议一致性和 .unknown。当前状态可能在读取后发生变化。

最佳实践

  1. 区分启用状态和佩戴状态false.notWearing 表示不同概念。
  2. 监听更新:UI 需要保持最新时,实现 didWearDetectionEnabledChangeddidWearStatusChanged
  3. 在主线程更新 UI:需要时将 delegate 驱动的 UI 更新派发到主队列。

注意事项

  • SDK 提供的是属性,而不是 getWearDetectionSwitchStatus 或基于 Result 的查询方法。
  • 不得将 .unknown 显示为“未佩戴”。