デバイスのペア解除
ペア解除操作は、接続中のデバイスとのペアリング情報を削除し、接続を解除します。別の端末とペアリングし直す場合などに使用します。
前提条件
ペア解除を実行する前に、以下を確認してください:
- デバイスが接続されており、安定した状態であること
- 必要なデータ同期が完了していること
- ペア解除後にデバイスが切断されることをユーザーが理解していること
AI を活用して実装
AI でこのワークフローを実装
公式の「AIBuds デバイスのペアリング解除」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-unpair-device を読み、その指示に従ってください。このスキルで「AIBuds デバイスのペアリング解除」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBuds.xcframework
インポート
SDK を使用するファイルで、メインヘッダーをインポートします:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds.h>
#import <AIBuds/AIBuds-Swift.h>プロトコル
unpair メソッドは、次のプロトコルで定義されています。このプロトコルは、ベースのデバイス API プロトコルを継承しています。
- Swift
- Objective-C
/// 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?)
}/// Defines common device operations including unpairing
@protocol AIBudsDeviceCommonAPI <AIBudsDeviceAPI>
/// 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
- (void)unpairWithCompletion:(AIBudsCompletionHandler)completion;
@endインスタンスメソッド
接続されたデバイスとのペアリングを解除します。
iOS 13.0+
- Swift
- Objective-C
/// 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?)/// 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
- (void)unpairWithCompletion:(AIBudsCompletionHandler)completion;パラメータ
| パラメータ | タイプ | 説明 |
|---|---|---|
| completion | AIBudsCompletionHandler? | 操作完了時に呼び出されるオプションの完了コールバック。 |
コールバックパラメータ:
| 名前 | タイプ | 説明 |
|---|---|---|
| success | Bool | 操作が成功した場合は true、それ以外は false。 |
| error | NSError? | 操作が失敗した場合はエラー情報を含み、それ以外は nil。 |
戻り値
このメソッドは直接値を返しません。結果は完了コールバックを介して提供されます。
使用例
- Swift
- Objective-C
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")
}
}
}#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
/// Connected device
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)unpairDevice {
__weak typeof(self) weakSelf = self;
id<AIBudsDeviceCommonAPI> device = (id<AIBudsDeviceCommonAPI>)self.device;
// Check if the device supports the unpair protocol
if ([device conformsToProtocol:@protocol(AIBudsDeviceCommonAPI)]) {
// Execute unpair with completion handler
[device unpairWithCompletion:^(BOOL success, NSError *_Nullable error) {
// Handle failure case
if (!success) {
NSLog(@"Unpair failed: %@", error);
return;
}
// Handle success case
NSLog(@"Unpair completed successfully");
}];
}
}
@endエラー処理
完了ハンドラーは、次のエラータイプを返す場合があります:
エラードメイン: AIBudsSDK.ErrorDomain
- Swift
- Objective-C
| エラーコード | 説明 | 復旧方法 |
|---|---|---|
| .deviceNotConnected | デバイスが接続されていません | デバイスがペアリングされていることを確認 |
| .bleCommandExecFailedDueToTimeout | 操作がタイムアウトしました | 操作を再試行してください |
| .deviceBusy | デバイスが別の操作でビジーです | 進行中の操作が完了するまで待ってください |
| .deviceNotSupport | このデバイスではペア解除はサポートされていません | 呼び出し前にデバイスの機能を確認してください |
| エラーコード | 説明 | 復旧方法 |
|---|---|---|
| AIBudsSdkErrorCodeDeviceNotConnected | デバイスが接続されていません | デバイスがペアリングされていることを確認 |
| AIBudsSdkErrorCodeBleCommandExecFailedDueToTimeout | 操作がタイムアウトしました | 操作を再試行してください |
| AIBudsSdkErrorCodeDeviceBusy | デバイスが別の操作でビジーです | 進行中の操作が完了するまで待ってください |
| AIBudsSdkErrorCodeDeviceNotSupport | このデバイスではペア解除はサポートされていません | 呼び出し前にデバイスの機能を確認してください |
推奨事項
-
ユーザーに確認:ペア解除を開始する前に常に確認ダイアログを表示してください。この操作はデバイスを切断し、再ペアリングが必要になります。
-
バックグラウンド実行の処理:UI を更新するときは、完了ハンドラーを
DispatchQueue.main.asyncブロックでラップしてください。 -
弱参照を使用する:循環参照を防ぐため、完了ハンドラーでは
[weak self]を使用してください。 -
プロトコル準拠の確認:メソッドを呼び出す前に、デバイスが
DeviceCommonAPIプロトコルに準拠していることを確認してください。 -
切断の処理:ペア解除に成功した後、デバイスの切断を適切に処理し、再ペアリングのガイダンスを提供してください。
プラットフォームの制限
iOS の Bluetooth に関する制限
iOS では、アプリから Bluetooth デバイスのシステム上のペアリングを解除できません。これは、セキュリティとユーザーによる制御を守るための iOS の制限です。
この制限により:
unpairを呼び出しても、iOS 側にはデバイスの BLE ペアリング情報が残ります- アプリの再起動時や Bluetooth を有効にしたときに、デバイスが自動再接続する場合があります
- デバイスは「設定」>「Bluetooth」に引き続き表示されます
推奨されるユーザーガイダンス:
iOS アプリでペア解除機能を提供する場合は、システム設定から手動で解除する手順を案内してください:
- システム上のペアリングを解除する:「設定」>「Bluetooth」で対象デバイスの情報ボタンを開き、「このデバイスの登録を解除」を選ぶよう案内します
- アプリ内で手順を示す:ユーザーがペア解除を実行したときに、説明または Bluetooth 設定へのリンクを表示します
- アプリレベルの切断:
unpairメソッドはデバイスとの接続を切断しますが、システムペアリングは維持されます
- Swift
- Objective-C
/// 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)
}
}/// Prompt user to unpair from iOS Settings
- (void)promptUserToUnpairFromSettings {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"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:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Open Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
NSURL *url =
[NSURL URLWithString:@"App-prefs:Bluetooth"];
if (url && [[UIApplication sharedApplication]
canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url
options:@{}
completionHandler:nil];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil]];
UIViewController *viewController =
[UIApplication sharedApplication].windows.firstObject.rootViewController;
[viewController presentViewController:alert animated:YES completion:nil];
}注意事項
- ペア解除コマンドの実行後、デバイスは切断されます
- デバイスを再び接続するには、再ペアリングが必要です
- すべてのペアリング情報は両方のデバイスから削除されます
- 進行中の操作は中断されます
- ペア解除には数秒かかる場合があります