착용 감지 설정
기기가 설정 가능한 지원 수준을 보고하는 경우 착용 감지를 켜거나 끕니다.
사전 요구 사항
- 기기가 연결되어 사용할 수 있는 상태입니다.
- 기기가
DeviceWearDetectionAPI를 준수합니다. wearDetectionCapability가.supportedAndConfigurable입니다.
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 {
/// Enable or disable wear-detection functionality
/// - Parameters:
/// - enabled: Whether to enable wear detection
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setWearDetection(
enabled: Bool,
completion: AIBudsCompletionHandler?
)
}/// The protocol for device wear detection API.
@protocol AIBudsDeviceWearDetectionAPI <AIBudsDeviceAPI>
/// Enable or disable wear-detection functionality
/// - Parameters:
/// - enabled: Whether to enable wear detection
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setWearDetectionEnabled:(BOOL)enabled
completion:(AIBudsCompletionHandler _Nullable)completion;
@end인스턴스 메서드
착용 감지를 켜거나 끕니다.
API 참고에서setWearDetection을 확인하세요.
- Swift
- Objective-C
/// Enable or disable wear-detection functionality
/// - Parameters:
/// - enabled: Whether to enable wear detection
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setWearDetection(
enabled: Bool,
completion: AIBudsCompletionHandler?
)/// Enable or disable wear-detection functionality
/// - Parameters:
/// - enabled: Whether to enable wear detection
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setWearDetectionEnabled:(BOOL)enabled
completion:(AIBudsCompletionHandler _Nullable)completion;매개변수
| 매개변수 | 타입 | 설명 |
|---|---|---|
enabled | Bool / BOOL | 착용 감지를 켜려면 true/YES, 끄려면 그 외 값을 사용합니다. |
completion | AIBudsCompletionHandler? | 작업이 끝날 때 호출되는 선택적 completion handler입니다. |
콜백 매개변수:
| 이름 | 타입 | 설명 |
|---|---|---|
success | Bool / BOOL | 작업 성공 여부입니다. |
error | NSError? | 실패 상세 정보이며 성공하면 nil입니다. |
반환 값
이 메서드는 값을 직접 반환하지 않습니다.
사용 예제
- Swift
- Objective-C
import AIBuds
guard let device = device as? DeviceWearDetectionAPI else {
print("Device does not support wear detection")
return
}
guard device.wearDetectionCapability == .supportedAndConfigurable else {
print("Wear detection is not configurable")
return
}
device.setWearDetection(enabled: true) { success, error in
guard success else {
print("Failed to enable wear detection: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Wear detection enabled")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceWearDetectionAPI> device = (id<AIBudsDeviceWearDetectionAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceWearDetectionAPI)]) {
return;
}
if (device.wearDetectionCapability != AIBudsWearDetectionCapabilitySupportedAndConfigurable) {
NSLog(@"Wear detection is not configurable");
return;
}
[device
setWearDetectionEnabled:YES
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Failed to enable wear detection: %@", error.localizedDescription);
return;
}
NSLog(@"Wear detection enabled");
}];오류 처리
로컬 UI를 변경하기 전에 success를 확인하고 실패 상세 정보는 error에서 확인하세요. 지원 수준이 .none 또는 .supportedNotConfigurable이면 이 메서드를 호출하지 마세요.
권장 사항
- 지원 수준 검증: 명령을 보내기 전에
.supportedAndConfigurable인지 확인하세요. - 중복 명령 방지: 먼저 요청 값과
isWearDetectionEnabled를 비교하세요. - 성공 후 갱신: 적용 상태를 표시하기 전에 현재 속성을 읽거나
didWearDetectionEnabledChanged를 기다리세요.
참고
- SDK Demo도 착용 감지를 켜거나 끄기 전에 같은 지원 수준 검사를 수행합니다.
- 이 메서드는 음악의 자동 일시 정지 또는 재개 동작을 정의하지 않습니다.