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

Last update: February 9, 2024

Use Cash Drawer

Generate control data for opening the cash drawer, and then send it to the printer.

The procedure and the reference vary depending on the OS. Select the OS to display.

iOS Android

The following description is based on the sample code. Control the cash drawer 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 Generate control data for opening the cash drawer, and then set it in the commands variable.

Memo
To control the cash drawer together with printing, add PrinterBuilder and DrawerBuilder to DocumentBuilder according to Generating Printing Data: Standard Function.

Task {
    do {
        let builder = StarXpandCommand.StarXpandCommandBuilder()
        _ = builder.addDocument(StarXpandCommand.DocumentBuilder.init()
                                    .addDrawer(StarXpandCommand.DrawerBuilder()
                                        .actionOpen(StarXpandCommand.Drawer.OpenParameter()
                                            .setChannel(.no1)
                                        )
                                    )
                                )

        let commands = builder.getCommands()

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 control data for opening the cash drawer to the printer.

try await printer.print(command: command)

7 Implement the process for when an error occurs. You can identify the error based on the StarIO10Error value.
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 {
        let builder = StarXpandCommand.StarXpandCommandBuilder()
        _ = builder.addDocument(StarXpandCommand.DocumentBuilder.init()
                            .addDrawer(StarXpandCommand.DrawerBuilder()
                                .actionOpen(StarXpandCommand.Drawer.OpenParameter()
                                    .setChannel(.no1)
                                )
                            )
        )
		
        let commands = builder.getCommands()

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

The following description is based on the sample code. Control the cash drawer 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 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 Generate control data for opening the cash drawer, and then set it in the commands variable.

Memo
To control the cash drawer together with printing, add PrinterBuilder and DrawerBuilder to DocumentBuilder according to Generating Printing Data: Standard Function.

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

scope.launch {
    try {
        val builder = StarXpandCommandBuilder()
        builder.addDocument(
            DocumentBuilder()
                .addDrawer(
                    DrawerBuilder()
                        .actionOpen(OpenParameter()
                            .setChannel(Channel.No1)
                        )
                )
        )

    val commands = builder.getCommands()

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 Implement the process for when an error occurs. You can identify the error cause based on the error type.
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}")
    } 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 {
        val builder = StarXpandCommandBuilder()
        builder.addDocument(
            DocumentBuilder()
            .addDrawer(
                DrawerBuilder()
                .actionOpen(OpenParameter()
                    .setChannel(Channel.No1)
                )
            )
        )

        val commands = builder2.getCommands()

        printer.openAsync().await()
        printer.printAsync(commands).await()

        Log.d("Printing", "Success")
    } 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()
    }
}