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

デバイスアプリの操作

アプリを起動または停止する前に、デバイスが提供するアプリ一覧を正しい情報源として確認します。

前提条件

  • デバイスが接続済みで、DeviceAppsAPI に準拠していること。
  • deviceApps から返された raw value だけを DeviceApp の値へ変換すること。

AI を活用して実装

AI で実装

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

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

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

API Reference

フレームワーク

AIBuds.xcframework

プロトコル

Swift
/// The protocol for device apps API. (Device Side Applications)
protocol DeviceAppsAPI: DeviceAPI {
    /// List of apps installed on the device, each element is an `NSNumber` wrapping the raw value of `DeviceApp`.
    var deviceApps: [NSNumber]? { get }

    /// Start the device side application.
    /// - Parameters:
    ///   - app: The app to start.
    ///   - completion: Reports success, the device status code, and an optional error.
    func startApp(_ app: DeviceApp, completion: AIBudsStatusCodeCompletionHandler?)

    /// Stop the device side application.
    /// - Parameters:
    ///   - app: The app to stop.
    ///   - completion: Reports success, the device status code, and an optional error.
    func stopApp(_ app: DeviceApp, completion: AIBudsStatusCodeCompletionHandler?)

    /// Stop all running applications and return to the home screen.
    /// - Parameter completion: Reports success and an optional error.
    func stopAllAppsAndReturnToHomeScreen(_ completion: AIBudsCompletionHandler?)

    /// Get the current foreground application.
    /// - Parameter completion: Reports success, the foreground app raw value, and an optional error.
    func getCurrentForegroundApp(
        _ completion: (
            (
                _ success: Bool,
                _ app: NSNumber?,
                _ error: NSError?
            ) -> Void
        )?
    )
}

シンボル

シンボル用途
deviceApps利用可能なデバイスアプリの raw value。
startApp1 つのアプリを起動。
stopApp1 つのアプリを停止。
stopAllAppsAndReturnToHomeScreenホーム画面へ戻る。
getCurrentForegroundAppフォアグラウンドアプリを取得。

アプリの値

SwiftObjective-Craw value意味
.homeScreenAIBudsDeviceAppHomeScreen0x00デバイスのホーム画面。startAppstopApp には渡さないでください。
.teleprompterAIBudsDeviceAppTeleprompter0x01テレプロンプターアプリ。
.aiChatAIBudsDeviceAppAiChat0x02AI 対話アプリ。
.navigationAIBudsDeviceAppNavigation0x03ナビゲーションアプリ。
.clockAIBudsDeviceAppClock0x04時計アプリ。
.translationAIBudsDeviceAppTranslation0x05翻訳アプリ。

使用例

Swift
guard let device = device as? DeviceAppsAPI else { return }

let apps = (device.deviceApps ?? []).compactMap {
    DeviceApp(rawValue: $0.intValue)
}

guard let app = apps.first else {
    print("No device application is available")
    return
}

device.startApp(app) { success, statusCode, error in
    guard success else {
        print(error?.localizedDescription ?? "Application failed to start")
        return
    }
    print("Application started: \(statusCode?.stringValue ?? "Unavailable")")
}

device.getCurrentForegroundApp { success, rawApp, error in
    let foreground = rawApp.flatMap { DeviceApp(rawValue: $0.intValue) }
    print(
        success
            ? "Foreground: \(String(describing: foreground))"
            : (error?.localizedDescription ?? "Query failed"))
}

エラー処理

すべての DeviceApp が各デバイスに存在すると仮定しないでください。利用可否は deviceApps、確定的なフォアグラウンド状態は getCurrentForegroundApp で確認し、製品固有の処理に備えてデバイスステータスコードを保持します。