查看佩戴检测状态
读取佩戴检测是否启用,以及当前没有佩戴、佩戴一侧还是双侧佩戴。
前置条件
- 设备已连接并就绪。
- 设备遵循
DeviceWearDetectionAPI。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
- Swift
- Objective-C
/// 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 }
}/// The protocol for device wear detection API.
@protocol AIBudsDeviceWearDetectionAPI <AIBudsDeviceAPI>
/// Indicates whether wear detection is currently enabled
@property(nonatomic, readonly) BOOL isWearDetectionEnabled;
/// The current wear status of the device.
@property(nonatomic, readonly) enum AIBudsWearStatus wearStatus;
@end属性
| 属性 | 类型 | 说明 |
|---|---|---|
isWearDetectionEnabled | Bool / BOOL | 当前是否启用佩戴检测。 |
wearStatus | WearStatus | 当前左右耳佩戴状态。 |
佩戴状态值
| Swift | Objective-C | 含义 |
|---|---|---|
.unknown | AIBudsWearStatusUnknown | 佩戴状态未知。 |
.notWearing | AIBudsWearStatusNotWearing | 两侧均未佩戴。 |
.leftWearing | AIBudsWearStatusLeftWearing | 仅佩戴左侧。 |
.rightWearing | AIBudsWearStatusRightWearing | 仅佩戴右侧。 |
.bothWearing | AIBudsWearStatusBothWearing | 两侧均已佩戴。 |
使用示例
- Swift
- Objective-C
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")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceWearDetectionAPI> device = (id<AIBudsDeviceWearDetectionAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceWearDetectionAPI)]) {
NSLog(@"Wear detection enabled: %@", device.isWearDetectionEnabled ? @"YES" : @"NO");
NSLog(@"Wear status raw value: %ld", (long)device.wearStatus);
}错误处理
这些属性是同步快照,不返回错误。请处理不支持的协议一致性和 .unknown。当前状态可能在读取后发生变化。
最佳实践
- 区分启用状态和佩戴状态:
false与.notWearing表示不同概念。 - 监听更新:UI 需要保持最新时,实现
didWearDetectionEnabledChanged和didWearStatusChanged。 - 在主线程更新 UI:需要时将 delegate 驱动的 UI 更新派发到主队列。
注意事项
- SDK 提供的是属性,而不是
getWearDetectionSwitchStatus或基于Result的查询方法。 - 不得将
.unknown显示为“未佩戴”。