StarXpand SDK for iOS / Android 開発者向けマニュアル Ver. 1.7.0

最終更新日: 2024年 6月 24日

Step.2 印刷データの送信 : 標準機能

Step1で生成した印刷データをプリンターに送信して印刷を行います。

手順はOSによって異なります。 表示するOSを選択してください。

iOS Android

以下の手順を参考に印刷データを送信してください。
ページ下部にサンプルコードがあります。

1プリンターの接続先情報を作ります。
通信に使用するinterfaceTypeと、接続先を指定するidentifierをセットしてください。
プリンターを検索するで接続先のプリンターを検索済みの場合、この実装は不要です。
手順2のprinter変数には、manager(_ manager: StarDeviceDiscoveryManager, didFind printer: StarPrinter)メソッドで取得したStarPrinterインスタンスをセットしてください。

let starConnectionSettings = StarConnectionSettings(interfaceType: .lan,
                                                            identifier: “00:11:62:00:00:01”)

2プリンターの接続先情報を与えて、StarPrinterインスタンスを取得します。

let printer = StarPrinter(starConnectionSettings)

3Step1で作った印刷データをcommands変数にセットします。

Task {
    do {
        let command = …

4プリンターに接続するためopenメソッドを呼び出します。

try await printer.open()

5印刷データの送信を行った後にプリンターとの接続を切断するため、deferブロックでcloseメソッドを呼び出します。

defer {
    Task {
        await printer.close()
    }
}

6プリンターに印刷データを送信するため、printメソッドを呼び出します。

try await printer.print(command: command)

7StarIO10Errorの値によりエラーを判別し、エラー時の処理を実装します。
発生する可能性のあるエラーはStarPrinterクラスのopenメソッド、printメソッドのAPIリファレンスに記載されています。

} catch StarIO10Error.notFound(message: let message, errorCode: let errorCode) {
    // Printer not found.
    // This may be due to the printer not being turned on or incorrect connection information.
} catch let error {
    print("Error: \(error)")
}
}
サンプルコード

let starConnectionSettings = StarConnectionSettings(interfaceType: .lan,
                                                            identifier: “00:11:62:00:00:01”)

let printer = StarPrinter(starConnectionSettings)

Task {
    do {
        // Set the print data created in Step 1 to the command variable.
        let command = …

        try await printer.open()
        
        defer {
            Task {
                await printer.close()
            }
        }
        
        try await printer.print(command: command)
    } catch StarIO10Error.notFound(message: let message, errorCode: let errorCode) {
        // Printer not found.
        // This may be due to the printer not being turned on or incorrect connection information.   
    } catch let error {
        print("Error: \(error)")
    }
}

以下の手順を参考に印刷データを送信してください。
ページ下部にサンプルコードがあります。

1プリンターの接続先情報を作ります。
通信に使用するinterfaceTypeと、接続先を指定するidentifierをセットしてください。
プリンターを検索するで接続先のプリンターを検索済みの場合、この実装は不要です。
手順2のprinter変数には、onPrinterFoundメソッドで取得したStarPrinterインスタンスをセットしてください。

val settings = StarConnectionSettings(InterfaceType.Lan, “00:11:62:00:00:01”)

2プリンターの接続先情報を与えて、StarPrinterインスタンスを取得します。

val printer = StarPrinter(settings, applicationContext)

3Step1で作った印刷データをcommands変数にセットします。

val job = SupervisorJob()
val scope = CoroutineScope(Dispatchers.Default + job)

scope.launch {
    try {
        val commands = …

4プリンターに接続するためopenAsyncメソッドを呼び出します。

printer.openAsync().await()

5プリンターに印刷データを送信するためprintAsyncメソッドを呼び出します。

printer.printAsync(commands).await()

6エラーの型によりエラー要因を判別し、エラー時の処理を実装します。
発生する可能性のあるエラーはStarPrinterクラスのopenAsyncメソッド、printAsyncメソッドのAPIリファレンスに記載されています。

} catch (e: StarIO10NotFoundException) {
    // Printer not found.
    // This may be due to the printer not being turned on or incorrect connection information.
} catch (e: Exception) {
    Log.d("Printing", "Error: ${e}")

7プリンターとの接続を切断するためcloseAsyncメソッドを呼び出します。

} finally {
printer.closeAsync().await()
}
}
サンプルコード

val settings = StarConnectionSettings(InterfaceType.Lan, “00:11:62:00:00:01”)
val printer = StarPrinter(settings, applicationContext)

val job = SupervisorJob()
val scope = CoroutineScope(Dispatchers.Default + job)

scope.launch {
    try {
        // Set the print data created in Step 1 to the commands variable.
        val commands = …

        printer.openAsync().await()
        printer.printAsync(commands).await()
    } catch (e: StarIO10NotFoundException) {
        // Printer not found.
        // This may be due to the printer not being turned on or incorrect connection information.   
    } catch (e: Exception) {
        Log.d("Printing", "Error: ${e}")
    } finally {
        printer.closeAsync().await()
    }
}