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

電源オフ

電源オフ操作は、プログラムによってデバイスをシャットダウンします。この操作は、リモートでデバイスをオフにする必要がある場合や、制御されたシャットダウンシーケンスの一部として使用する場合に便利です。

前提条件

電源オフを実行する前に、以下を確認してください:

  • デバイスが接続されており、安定した状態にある
  • 保存されていないデータがすべて保存されている
  • ユーザーがシャットダウン後にデバイスが切断されることを理解している

AI を活用して実装

AI で実装

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

公式の「AIBuds の電源オフ」スキルを使い、アプリに合わせて実装します。

https://docs-aibuds.github.io/ja/skills/implement-aibuds-power-off を読み、その指示に従ってください。このスキルで「AIBuds の電源オフ」をこの iOS プロジェクトに実装し、検証してください。
公式スキルを見る

API リファレンス

Framework

AIBuds.xcframework

Import

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

Swift
import AIBuds

プロトコル

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

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

インスタンスメソッド

アプリからデバイスの電源を切ります。

iOS 13.0+

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

パラメータ

パラメータ説明
completionAIBudsCompletionHandler?処理完了時に呼び出される省略可能なコールバックです。

コールバック引数:

名前説明
successBool処理に成功した場合は true、失敗した場合は false です。
errorNSError?失敗時のエラー情報です。成功した場合は nil です。

戻り値

このメソッドに直接の戻り値はありません。結果は完了コールバックで通知されます。

使用例

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

エラー処理

完了ハンドラからは、次のエラーが返されることがあります。

エラードメイン: AIBudsSDK.ErrorDomain

エラーコード説明対処方法
.deviceNotConnectedデバイスが接続されていませんペアリングと接続状態を確認してください
.bleCommandExecFailedDueToTimeout処理がタイムアウトしましたもう一度実行してください
.deviceBusyデバイスが別の処理を実行中です実行中の処理が終わるまで待ってください
.deviceNotSupportデバイスが電源オフ操作に対応していません呼び出す前に対応機能を確認してください

推奨事項

  1. ユーザーに確認する:電源を切ると接続が解除されるため、実行前に確認ダイアログを表示してください。

  2. UI はメインスレッドで更新する:完了ハンドラから UI を更新する場合は DispatchQueue.main.async を使用してください。

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

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

  5. 切断を処理する:電源オフ成功後に発生する切断を、アプリ側で適切に処理してください。

注意事項

  • 電源オフコマンドの実行後、デバイスとの接続は解除されます
  • デバイスはユーザーの操作で再び電源を入れられます
  • 実行中の処理は中断されます
  • 電源が切れるまで数秒かかる場合があります