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

装着検出の状態

装着検出が有効かどうかと、左右どちらのイヤホンが現在装着されているかを確認します。

前提条件

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

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

Swift
import AIBuds

プロトコル

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

プロパティ

プロパティ説明
isWearDetectionEnabledBool / BOOL装着検出が現在有効かどうか。
wearStatusWearStatus左右イヤホンの現在の装着状態。

装着状態の値

SwiftObjective-C意味
.unknownAIBudsWearStatusUnknown装着状態を判定できません。
.notWearingAIBudsWearStatusNotWearing左右とも未装着です。
.leftWearingAIBudsWearStatusLeftWearing左側のみ装着されています。
.rightWearingAIBudsWearStatusRightWearing右側のみ装着されています。
.bothWearingAIBudsWearStatusBothWearing左右とも装着されています。

使用例

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

エラー処理

これらは同期的に現在値を返すプロパティであり、エラーは返しません。プロトコル非対応と .unknown を明示的に処理してください。取得後に状態が変化する場合があります。

ベストプラクティス

  1. 有効状態と装着状態を分ける: false.notWearing は異なる状態を表します。
  2. 更新を監視する: UI を最新状態に保つ場合は、didWearDetectionEnabledChangeddidWearStatusChanged を実装します。
  3. メインスレッドで UI を更新する: 必要に応じて、デリゲートからの UI 更新をメインキューへ切り替えます。

注意事項

  • SDK は getWearDetectionSwitchStatusResult ベースの問い合わせメソッドではなく、プロパティを公開しています。
  • .unknown を「未装着」として表示しないでください。