跳到主要内容

查看佩戴检测能力

读取设备是否支持佩戴检测,以及该功能是否可以配置。

前置条件

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

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds

协议

该能力是 DeviceWearDetectionAPI 的属性。

Swift
/// The protocol for device wear detection API.
protocol DeviceWearDetectionAPI: DeviceAPI {
    /// The wear-detection capabilities supported by the device
    var wearDetectionCapability: WearDetectionCapability { get }
}

属性

属性类型说明
wearDetectionCapabilityWearDetectionCapability设备当前报告的能力。

支持的值

SwiftObjective-C含义
.noneAIBudsWearDetectionCapabilityNone不支持。
.supportedNotConfigurableAIBudsWearDetectionCapabilitySupportedNotConfigurable支持,但不可配置。
.supportedAndConfigurableAIBudsWearDetectionCapabilitySupportedAndConfigurable支持且可配置。

使用示例

Swift
import AIBuds

guard let device = device as? DeviceWearDetectionAPI else {
    print("Wear detection is not exposed by this device")
    return
}

switch device.wearDetectionCapability {
case .none:
    print("Wear detection is not supported")
case .supportedNotConfigurable:
    print("Wear detection is supported but not configurable")
case .supportedAndConfigurable:
    print("Wear detection is supported and configurable")
@unknown default:
    print("Unknown wear-detection capability")
}

错误处理

该属性没有 completion handler。请处理协议不一致和未来未知枚举值。不要将 .none 解释为传输错误;它是有效的能力值。

最佳实践

  1. 配置前读取:显示启用/禁用控件前先检查能力。
  2. 保留所有能力级别:不要把“不可配置”合并为“不支持”。
  3. 处理未来值:Swift 对枚举执行 switch 时使用 @unknown default

注意事项

  • 该 API 是同步属性读取,不是异步 getWearDetectionSupport 方法。
  • SDK Demo 使用该属性判断是否应提供配置控件。