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

Last update: February 9, 2024

Step 2 Send Printing Data: Standard Function

Send the printing data generated in Step 1 to the printer to print.

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.

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 Step2.

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 printing data created in Step 1 in the commands variable.

Task {
    do {
        let command = ...

4 Call the open method to connect to the printer.

try await printer.open()

5 Call the close method with the defer block to disconnect from the printer after sending the printing data.

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

6 Call the print method to send the printing data to the printer.

try await printer.print(command: command)

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

Send the printing data using the procedure below.
The sample code is available at the bottom of the page.

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 Step2.

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 printing data created in Step 1 in the commands variable.

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

scope.launch {
    try {
        val commands = ...

4 Call the openAsync method to connect to the printer.

printer.openAsync().await()

5 Call the printAsync method to send the printing data to the printer.

printer.printAsync(commands).await()

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