装着検出の設定
デバイスが設定可能な装着検出に対応している場合に、機能を有効または無効にします。
前提条件
- デバイスが接続済みで、操作可能な状態であること。
- デバイスが
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? | 処理完了時に呼び出される任意の完了ハンドラー。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
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 でも、装着検出を切り替える前に同じ対応状況の確認を行います。
- このメソッドは、音楽の自動一時停止や再開の動作を定義しません。