Control the Device Printer
This guide explains how to interact with the device’s physical hardware—specifically the printer—using the PrinterRepository. This allows you to check for paper availability and print both standard receipts and custom content.
Checking Paper Status
Before attempting any print job, it is best practice to verify that the printer has paper. Use the checkPaper function to query the hardware status.
Repository: PrinterRepository
Function: checkPaper
Example Implementation
Returns true if paper is available, false otherwise .
suspend fun verifyPaperAvailability() {
val result = printerRepository.checkPaper()
when (result) {
is RepositoryResult.Success -> {
val hasPaper = result.data
if (hasPaper) {
println("✅ Printer is ready.")
} else {
println("⚠️ Printer is out of paper.")
}
}
is RepositoryResult.ConnectionError -> println("❌ Connection Error")
is RepositoryResult.ProtocolError -> println("❌ Error: ${result.type}")
else -> {}
}
}Printing a Default Ticket
The SDK provides a built-in method to generate and print a standard receipt using data from a Transaction object. This is useful for quickly generating compliant receipts without manually drawing the layout.
Function: printDefaultTicket
Parameters:
context: The Android application context.headerIcon: ABitmapimage to print at the top of the receipt (e.g., your merchant logo).transaction: TheTransactionobject containing the payment details.
Example Implementation
suspend fun printReceipt(context: Context, logo: Bitmap, transaction: Transaction) {
val result = printerRepository.printDefaultTicket(
context = context,
headerIcon = logo,
transaction = transaction
)
handlePrintResult(result)
}Printing Custom Content (Bitmaps)
If you need full control over the receipt design (e.g., adding coupons, QR codes, or custom layouts), you can render your content into a Bitmap and send it directly to the printer.
Function: printBitmap
Parameters:
bitmap: TheBitmapobject to print.
Example Implementation
suspend fun printCustomDesign(myCustomBitmap: Bitmap) {
val result = printerRepository.printBitmap(
bitmap = myCustomBitmap
)
handlePrintResult(result)
}Handling Print Results
Both printing methods return a RepositoryResult<Boolean>.
fun handlePrintResult(result: RepositoryResult<Boolean>) {
when (result) {
is RepositoryResult.Success -> {
if (result.data) {
println("✅ Print job completed successfully.")
} else {
println("❌ Print job failed.")
}
}
is RepositoryResult.ConnectionError -> println("❌ Connection Error")
is RepositoryResult.ProtocolError -> println("❌ Protocol Error: ${result.type}")
else -> {}
}
}Next Steps
- Configure Bizum Payments: Integration guides for Bizum.
- Configure Alipay and WeChat QR: Integration guides for Alipay and WeChat Pay using QR-based flows.
- Manage Device Settings: Learn how to manage device settings.
- Tokenize Card Credentials: Learn how to manage tokens.