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

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

Step.2 印刷データの送信 : テンプレート印刷機能

Step1で生成したテンプレートとフィールドデータを組み合わせてテンプレート印刷を行います。

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

iOS Android

以下の手順を参考にテンプレート印刷を行ってください。
ページ下部にサンプルコードがあります。 また、SDKのサンプルアプリも参考にしてください。

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で作ったテンプレートをtemplateプロパティ変数にセットします。

Task {
    do {
       let template = …
        printer.template = template

Memo
印刷データの送信:標準機能ではStarXpandCommandBuilderで生成した印刷データをprintAsyncメソッドの引数に渡していましたが、テンプレート印刷を行う場合はtemplateプロパティにセットします。

4Step1で用意したフィールドデータをfieldData変数にセットします。

let fieldData = …

5openメソッドを呼び出し、プリンターに接続します。

try await printer.open()

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

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

7printメソッドを呼び出し、プリンターにフィールドデータを送信します。
これにより、templateプロパティにセットされたテンプレートとフィールドデータを組み合わせてテンプレート印刷を行います。

try await printer.print(command: fieldData)

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

} catch StarIO10Error.argument(message: let message, errorCode: let errorCode) {
} catch StarIO10Error.notFound(message: let message, errorCode: let errorCode) {
} 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 template = …
        printer.template = template

        // Set the field data prepared in Step 1 to the fieldData variable.
        let fieldData = …
 
        try await printer.open()
        
        defer {
            Task {
                await printer.close()
            }
        }
        
        try await printer.print(command: fieldData)
    } catch StarIO10Error.argument (message: let message, errorCode: let errorCode) {
         // Argument is not correct.
         // This may be due to invalid template or field data.
    } 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)")
    }
}

以下の手順を参考にテンプレート印刷を行ってください。
ページ下部にサンプルコードがあります。 また、SDKのサンプルアプリも参考にしてください。

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で作ったテンプレートをtemplateプロパティ変数にセットします。

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

scope.launch {
    try {
        val template = …
        printer.template = template

Memo
印刷データの送信:標準機能ではStarXpandCommandBuilderで生成した印刷データをprintAsyncメソッドの引数に渡していましたが、テンプレート印刷を行う場合はtemplateプロパティにセットします。

4 Step1で用意したフィールドデータをfieldData変数にセットします。

val fieldData = …

5openAsyncメソッドを呼び出し、プリンターに接続します。

printer.openAsync().await()

6printAsyncメソッドを呼び出し、プリンターにフィールドデータを送信します。 これにより、templateプロパティにセットされたテンプレートとフィールドデータを組み合わせてテンプレート印刷を行います。

printer.printAsync(fieldData).await()

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

} catch (e: StarIO10ArgumentException) {
} catch (e: StarIO10NotFoundException) {
} catch (e: Exception) {
    Log.d("Printing", "Error: ${e}")
}

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

} 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 template created in Step 1 to the template variable.
        val template = …
        printer.template = template

        // Set the field data prepared in Step 1 to the fieldData variable.
        val fieldData = …

        printer.openAsync().await()
        printer.printAsync(fieldData).await()
    } catch (e: StarIO10ArgumentException) {
        // Argument is not correct.
        // This may be due to invalid template or field data.
    } 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()
    }
}