StarXpand SDK for iOS/Android Developer's Manual Ver. 1.8.0

Last update: November 1, 2024

Step 2 Send Printing Data: Template Printing Function

You can combine and print the template and field data generated in Step 1.

The procedure varies depending on the OS. Select the OS to display.

iOS Android

Send the printing data using the procedure below.
The sample code is available at the bottom of the page. Also, refer to the sample application in the SDK.

1 Create the printer destination information.
Set interfaceType, which is used for communication, and identifier, which specifies the destination.
If the destination printer has already been searched for in Search for Printer, this setting is unnecessary.
Set the StarPrinter instance acquired with the manager(_ manager: StarDeviceDiscoveryManager, didFind printer: StarPrinter) method in the printer variable in Step 2.

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

2 Provide the printer destination information to acquire the StarPrinter instance.

let printer = StarPrinter(starConnectionSettings)

3 Set the template created in Step 1 in the template property variable.

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

Memo
In Send Printing Data: Standard Function, the printing data created with StarXpandCommandBuilder is passed as the argument of the printAsync method. On the other hand, to print the template, you need to set it in the template property.

4 Set the field data prepared in Step 1 in the fieldData variable.

let fieldData = …

5 Call the open method to connect to the printer.

try await printer.open()

6 Call the close method in the defer block to disconnect from the printer after sending the printing data.

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

7 Call the print method to send the field data to the printer.
The template is printed in a combination of the template set in the template property and the field data.

try await printer.print(command: fieldData)

8 Identify the error based on the StarIO10Error value and implement the process for when an error occurs.
Possible errors are described in the API reference of the open method and print method of the StarPrinter class.

} catch StarIO10Error.argument(message: let message, errorCode: let errorCode) {
} catch StarIO10Error.notFound(message: let message, errorCode: let errorCode) {
} catch let error {
    print("Error: \(error)")
}
Sample code

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

Send the printing data using the procedure below.
The sample code is available at the bottom of the page. Also, refer to the sample application in the SDK.

1 Create the printer destination information.
Set interfaceType, which is used for communication, and identifier, which specifies the destination.
If the destination printer has already been searched for in Search for Printer, this setting is unnecessary.
Set the StarPrinter instance acquired with the onPrinterFound method in the printer variable in Step 2.

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

2 Provide the printer destination information to acquire the StarPrinter instance.

val printer = StarPrinter(settings, applicationContext)

3 Set the template created in Step 1 in the template property variable.

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

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

Memo
In Send Printing Data: Standard Function, the printing data created with StarXpandCommandBuilder is passed as the argument of the printAsync method. On the other hand, to print the template, you need to set it in the template property.

4 Set the field data prepared in Step 1 in the fieldData variable.

val fieldData = …

5 Call the openAsync method to connect to the printer.

printer.openAsync().await()

6 Call the printAsync method to send the field data to the printer. The template is printed in a combination of the template set in the template property and the field data.

printer.printAsync(fieldData).await()

7 Identify the error cause based on the error type and implement the process for when an error occurs. Possible errors are described in the API reference of the openAsync method and printAsync method of the StarPrinter class.

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

8 Call the closeAsync method to disconnect from the printer.

} finally {
    printer.closeAsync().await()
}
Sample code

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()
    }
}