電源オフ
電源オフ操作は、プログラムによってデバイスをシャットダウンします。この操作は、リモートでデバイスをオフにする必要がある場合や、制御されたシャットダウンシーケンスの一部として使用する場合に便利です。
前提条件
電源オフを実行する前に、以下を確認してください:
- デバイスが接続されており、安定した状態にある
- 保存されていないデータがすべて保存されている
- ユーザーがシャットダウン後にデバイスが切断されることを理解している
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds の電源オフ」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-power-off を読み、その指示に従ってください。このスキルで「AIBuds の電源オフ」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
Framework
AIBuds.xcframework
Import
SDK を使用するファイルで、メインヘッダーをインポートします:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds.h>
#import <AIBuds/AIBuds-Swift.h>プロトコル
powerOff メソッドは、次のプロトコルで定義されています。このプロトコルは、ベースデバイス API プロトコルを継承しています。
- Swift
- Objective-C
/// Defines common device operations including power off
protocol DeviceCommonAPI: DeviceAPI {
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - 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 powerOff(_ completion: AIBudsCompletionHandler?)
}/// Defines common device operations including power off
@protocol AIBudsDeviceCommonAPI <AIBudsDeviceAPI>
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - 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)powerOffWithCompletion:(AIBudsCompletionHandler)completion;
@endインスタンスメソッド
アプリからデバイスの電源を切ります。
iOS 13.0+
- Swift
- Objective-C
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - 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 powerOff(_ completion: AIBudsCompletionHandler?)/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - 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)powerOffWithCompletion:(AIBudsCompletionHandler)completion;パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
| completion | AIBudsCompletionHandler? | 処理完了時に呼び出される省略可能なコールバックです。 |
コールバック引数:
| 名前 | 型 | 説明 |
|---|---|---|
| success | Bool | 処理に成功した場合は true、失敗した場合は false です。 |
| error | NSError? | 失敗時のエラー情報です。成功した場合は nil です。 |
戻り値
このメソッドに直接の戻り値はありません。結果は完了コールバックで通知されます。
使用例
- Swift
- Objective-C
import AIBuds
class DeviceManager {
/// The connected device
weak var device: DeviceConvertible?
/// Powers off the connected device
func powerOffDevice() {
// Ensure the device supports power off protocol
guard let device = device as? DeviceCommonAPI else {
print("Device does not support power off")
return
}
// Execute power off with completion handler
device.powerOff { [weak self] success, error in
// Handle failure case
if !success {
let errorMessage = {
if let error = error {
return "\(error)"
}
return "Unknown error"
}()
print("Power off failed: \(errorMessage)")
return
}
// Handle success case
print("Power off command sent successfully")
}
}
}#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
/// The connected device
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)powerOffDevice {
__weak typeof(self) weakSelf = self;
id<AIBudsDeviceCommonAPI> device = (id<AIBudsDeviceCommonAPI>)self.device;
// Ensure the device supports power off protocol
if ([device conformsToProtocol:@protocol(AIBudsDeviceCommonAPI)]) {
// Execute power off with completion handler
[device powerOffWithCompletion:^(BOOL success, NSError *_Nullable error) {
// Handle failure case
if (!success) {
NSLog(@"Power off failed: %@", error);
return;
}
// Handle success case
NSLog(@"Power off command sent successfully");
}];
}
}
@endエラー処理
完了ハンドラからは、次のエラーが返されることがあります。
エラードメイン: AIBudsSDK.ErrorDomain
- Swift
- Objective-C
| エラーコード | 説明 | 対処方法 |
|---|---|---|
| .deviceNotConnected | デバイスが接続されていません | ペアリングと接続状態を確認してください |
| .bleCommandExecFailedDueToTimeout | 処理がタイムアウトしました | もう一度実行してください |
| .deviceBusy | デバイスが別の処理を実行中です | 実行中の処理が終わるまで待ってください |
| .deviceNotSupport | デバイスが電源オフ操作に対応していません | 呼び出す前に対応機能を確認してください |
| エラーコード | 説明 | 対処方法 |
|---|---|---|
| AIBudsSdkErrorCodeDeviceNotConnected | デバイスが接続されていません | ペアリングと接続状態を確認してください |
| AIBudsSdkErrorCodeBleCommandExecFailedDueToTimeout | 処理がタイムアウトしました | もう一度実行してください |
| AIBudsSdkErrorCodeDeviceBusy | デバイスが別の処理を実行中です | 実行中の処理が終わるまで待ってください |
| AIBudsSdkErrorCodeDeviceNotSupport | デバイスが電源オフ操作に対応していません | 呼び出す前に対応機能を確認してください |
推奨事項
-
ユーザーに確認する:電源を切ると接続が解除されるため、実行前に確認ダイアログを表示してください。
-
UI はメインスレッドで更新する:完了ハンドラから UI を更新する場合は
DispatchQueue.main.asyncを使用してください。 -
弱参照を使用する:循環参照を防ぐため、完了ハンドラでは
[weak self]を使用してください。 -
プロトコル対応を確認する:呼び出し前にデバイスが
DeviceCommonAPIに準拠していることを確認してください。 -
切断を処理する:電源オフ成功後に発生する切断を、アプリ側で適切に処理してください。
注意事項
- 電源オフコマンドの実行後、デバイスとの接続は解除されます
- デバイスはユーザーの操作で再び電源を入れられます
- 実行中の処理は中断されます
- 電源が切れるまで数秒かかる場合があります