查看佩戴检测能力
读取设备是否支持佩戴检测,以及该功能是否可以配置。
前置条件
- 设备已连接并就绪。
- 设备遵循
DeviceWearDetectionAPI。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
该能力是 DeviceWearDetectionAPI 的属性。
- Swift
- Objective-C
/// The protocol for device wear detection API.
protocol DeviceWearDetectionAPI: DeviceAPI {
/// The wear-detection capabilities supported by the device
var wearDetectionCapability: WearDetectionCapability { get }
}/// The protocol for device wear detection API.
@protocol AIBudsDeviceWearDetectionAPI <AIBudsDeviceAPI>
/// The wear-detection capabilities supported by the device
@property(nonatomic, readonly) enum AIBudsWearDetectionCapability wearDetectionCapability;
@end属性
| 属性 | 类型 | 说明 |
|---|---|---|
wearDetectionCapability | WearDetectionCapability | 设备当前报告的能力。 |
支持的值
| Swift | Objective-C | 含义 |
|---|---|---|
.none | AIBudsWearDetectionCapabilityNone | 不支持。 |
.supportedNotConfigurable | AIBudsWearDetectionCapabilitySupportedNotConfigurable | 支持,但不可配置。 |
.supportedAndConfigurable | AIBudsWearDetectionCapabilitySupportedAndConfigurable | 支持且可配置。 |
使用示例
- Swift
- Objective-C
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")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceWearDetectionAPI> device = (id<AIBudsDeviceWearDetectionAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceWearDetectionAPI)]) {
NSLog(@"Wear detection is not exposed by this device");
return;
}
switch (device.wearDetectionCapability) {
case AIBudsWearDetectionCapabilityNone:
NSLog(@"Wear detection is not supported");
break;
case AIBudsWearDetectionCapabilitySupportedNotConfigurable:
NSLog(@"Wear detection is supported but not configurable");
break;
case AIBudsWearDetectionCapabilitySupportedAndConfigurable:
NSLog(@"Wear detection is supported and configurable");
break;
}错误处理
该属性没有 completion handler。请处理协议不一致和未来未知枚举值。不要将 .none 解释为传输错误;它是有效的能力值。
最佳实践
- 配置前读取:显示启用/禁用控件前先检查能力。
- 保留所有能力级别:不要把“不可配置”合并为“不支持”。
- 处理未来值:Swift 对枚举执行 switch 时使用
@unknown default。
注意事项
- 该 API 是同步属性读取,不是异步
getWearDetectionSupport方法。 - SDK Demo 使用该属性判断是否应提供配置控件。