メインコンテンツまでスキップ

装着検出の設定

デバイスが設定可能な装着検出に対応している場合に、機能を有効または無効にします。

前提条件

  • デバイスが接続済みで、操作可能な状態であること。
  • デバイスが DeviceWearDetectionAPI に準拠していること。
  • wearDetectionCapability.supportedAndConfigurable であること。

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

Swift
import AIBuds

プロトコル

このメソッドは DeviceWearDetectionAPI で定義されています。

Swift
/// 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?
    )
}

インスタンスメソッド

装着検出を有効または無効にします。

API リファレンスで setWearDetection を開く。
Swift
/// 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?
)

パラメータ

パラメータ説明
enabledBool / BOOLtrue/YES で装着検出を有効化し、それ以外では無効化します。
completionAIBudsCompletionHandler?処理完了時に呼び出される任意の完了ハンドラー。

コールバックのパラメータ:

名前説明
successBool / BOOL処理が成功したかどうか。
errorNSError?失敗時の詳細。成功時は nil

戻り値

このメソッドは値を直接返しません。

使用例

Swift
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")
}

エラー処理

UI を更新する前に success を確認し、失敗の詳細は error から取得します。機能が .none または .supportedNotConfigurable の場合は、このメソッドを呼び出さないでください。

ベストプラクティス

  1. 対応状況を確認する: コマンド送信前に .supportedAndConfigurable であることを確認します。
  2. 不要なコマンドを避ける: 先に要求値と isWearDetectionEnabled を比較します。
  3. 成功後に状態を更新する: 現在のプロパティを再取得するか、didWearDetectionEnabledChanged を待ってから反映済みの状態を表示します。

注意事項

  • SDK Demo でも、装着検出を切り替える前に同じ対応状況の確認を行います。
  • このメソッドは、音楽の自動一時停止や再開の動作を定義しません。