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

デバイスのペア解除

ペア解除操作は、接続中のデバイスとのペアリング情報を削除し、接続を解除します。別の端末とペアリングし直す場合などに使用します。

前提条件

ペア解除を実行する前に、以下を確認してください:

  • デバイスが接続されており、安定した状態であること
  • 必要なデータ同期が完了していること
  • ペア解除後にデバイスが切断されることをユーザーが理解していること

AI を活用して実装

AI で実装

AI でこのワークフローを実装

公式の「AIBuds デバイスのペアリング解除」スキルを使い、アプリに合わせて実装します。

https://docs-aibuds.github.io/ja/skills/implement-aibuds-unpair-device を読み、その指示に従ってください。このスキルで「AIBuds デバイスのペアリング解除」をこの iOS プロジェクトに実装し、検証してください。
公式スキルを見る

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

SDK を使用するファイルで、メインヘッダーをインポートします:

Swift
import AIBuds

プロトコル

unpair メソッドは、次のプロトコルで定義されています。このプロトコルは、ベースのデバイス API プロトコルを継承しています。

Swift
/// Defines common device operations including unpairing
protocol DeviceCommonAPI: DeviceAPI {
    /// Unpairs the device
    /// - Parameters:
    ///   - completion: A completion callback that returns the operation result
    ///     - success: `true` if the operation succeeds, `false` otherwise
    ///     - error: An `NSError` object describing the error that occurred, or `nil` if the operation succeeds
    func unpair(_ completion: AIBudsCompletionHandler?)
}

インスタンスメソッド

接続されたデバイスとのペアリングを解除します。

iOS 13.0+

Swift
/// Unpairs the device
/// - Parameters:
///   - completion: A completion callback that returns the operation result
///     - success: `true` if the operation succeeds, `false` otherwise
///     - error: An `NSError` object describing the error that occurred, or `nil` if the operation succeeds
func unpair(_ completion: AIBudsCompletionHandler?)

パラメータ

パラメータタイプ説明
completionAIBudsCompletionHandler?操作完了時に呼び出されるオプションの完了コールバック。

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

名前タイプ説明
successBool操作が成功した場合は true、それ以外は false
errorNSError?操作が失敗した場合はエラー情報を含み、それ以外は nil

戻り値

このメソッドは直接値を返しません。結果は完了コールバックを介して提供されます。

使用例

Swift
import AIBuds

class DeviceManager {

    /// Connected device
    weak var device: DeviceConvertible?

    /// Unpairs the connected device
    func unpairDevice() {
        // Check if the device supports the unpair protocol
        guard let device = device as? DeviceCommonAPI else {
            print("Device does not support unpairing")
            return
        }

        // Execute unpair with completion handler
        device.unpair { [weak self] success, error in
            // Handle failure case
            if !success {
                let errorMessage = {
                    if let error = error {
                        return "\(error)"
                    }
                    return "Unknown error"
                }()
                print("Unpair failed: \(errorMessage)")
                return
            }
            // Handle success case
            print("Unpair completed successfully")
        }
    }
}

エラー処理

完了ハンドラーは、次のエラータイプを返す場合があります:

エラードメイン: AIBudsSDK.ErrorDomain

エラーコード説明復旧方法
.deviceNotConnectedデバイスが接続されていませんデバイスがペアリングされていることを確認
.bleCommandExecFailedDueToTimeout操作がタイムアウトしました操作を再試行してください
.deviceBusyデバイスが別の操作でビジーです進行中の操作が完了するまで待ってください
.deviceNotSupportこのデバイスではペア解除はサポートされていません呼び出し前にデバイスの機能を確認してください

推奨事項

  1. ユーザーに確認:ペア解除を開始する前に常に確認ダイアログを表示してください。この操作はデバイスを切断し、再ペアリングが必要になります。

  2. バックグラウンド実行の処理:UI を更新するときは、完了ハンドラーを DispatchQueue.main.async ブロックでラップしてください。

  3. 弱参照を使用する:循環参照を防ぐため、完了ハンドラーでは [weak self] を使用してください。

  4. プロトコル準拠の確認:メソッドを呼び出す前に、デバイスが DeviceCommonAPI プロトコルに準拠していることを確認してください。

  5. 切断の処理:ペア解除に成功した後、デバイスの切断を適切に処理し、再ペアリングのガイダンスを提供してください。

プラットフォームの制限

iOS の Bluetooth に関する制限

iOS では、アプリから Bluetooth デバイスのシステム上のペアリングを解除できません。これは、セキュリティとユーザーによる制御を守るための iOS の制限です。

この制限により:

  • unpair を呼び出しても、iOS 側にはデバイスの BLE ペアリング情報が残ります
  • アプリの再起動時や Bluetooth を有効にしたときに、デバイスが自動再接続する場合があります
  • デバイスは「設定」>「Bluetooth」に引き続き表示されます

推奨されるユーザーガイダンス:

iOS アプリでペア解除機能を提供する場合は、システム設定から手動で解除する手順を案内してください:

  1. システム上のペアリングを解除する:「設定」>「Bluetooth」で対象デバイスの情報ボタンを開き、「このデバイスの登録を解除」を選ぶよう案内します
  2. アプリ内で手順を示す:ユーザーがペア解除を実行したときに、説明または Bluetooth 設定へのリンクを表示します
  3. アプリレベルの切断unpairメソッドはデバイスとの接続を切断しますが、システムペアリングは維持されます
Swift
/// Prompt user to unpair from iOS Settings
func promptUserToUnpairFromSettings() {
    // Show alert with instructions
    let alert = UIAlertController(
        title: "Unpair Device",
        message:
            "To completely unpair the device, go to Settings > Bluetooth, find your device, tap the 'i' icon next to it, then select 'Forget This Device'.",
        preferredStyle: .alert
    )
    alert.addAction(
        UIAlertAction(title: "Open Settings", style: .default) { _ in
            // Deep link to Bluetooth settings
            if let url = URL(string: "App-prefs:Bluetooth") {
                UIApplication.shared.open(url)
            }
        })
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

    // Present alert
    if let viewController = UIApplication.shared.windows.first?.rootViewController {
        viewController.present(alert, animated: true)
    }
}

注意事項

  • ペア解除コマンドの実行後、デバイスは切断されます
  • デバイスを再び接続するには、再ペアリングが必要です
  • すべてのペアリング情報は両方のデバイスから削除されます
  • 進行中の操作は中断されます
  • ペア解除には数秒かかる場合があります