Print Receipt
Download printr sdk api document:
Add following code to project build.gradle file:
dependencies {
implementation 'com.dspread.print:dspread_print_sdk:1.3.9-beta'
}Overview
This activity (PrintTicketActivity) handles receipt printing for a POS (Point of Sale) system. It generates a receipt bitmap and sends it to a printer device.
How the Printer Works
1. Starting the Print Process
The activity is launched with transaction data:
Intent intent = new Intent(context, PrintTicketActivity.class);
intent.putExtra("terAmount", amount);
intent.putExtra("maskedPAN", maskedPAN);
intent.putExtra("terminalTime", terminalTime);
intent.putExtra("transactionTime", transactionTime);
startActivity(intent);2. Receipt Generation Flow
- Data Collection: Transaction details are collected (amount, card PAN, timestamps)
- Bitmap Generation: A receipt image is created in the background
- Display/Print: The receipt is either displayed on screen or auto-printed (depending on device size)
3. Printing Options
For Normal Devices:
- Receipt preview is shown
- User clicks "Print Ticket" button to print
- Triggers:
viewModel.printTicket(mBitmap)
For Small Devices (320x240 or smaller):
- Auto-prints without preview
- Shows "Please Wait..." message
- User can retry with "Small Print" button
4. Print Execution
// Triggered by button click
viewModel.printTicket(mBitmap);The actual printing is handled by:
PrintTicketViewModelwhich likely calls the printer device SDK- Inherits from
PrinterBaseActivitywhich manages the printer communication - Uses the
PrinterDeviceclass from the Dspread SDK
5. Print Results
The onReturnPrintResult() method handles printer feedback:
@Override
protected void onReturnPrintResult(boolean isSuccess, String status, PrinterDevice.ResultType resultType)Success Cases:
- Shows "Print Successful" dialog
- Navigates back to MainActivity after 3 seconds
Failure Cases:
NOPAPER: No paper in printerLOWERBATTERY: Battery too lowOVERHEATING: Printer overheated- Unknown errors
Key Features
Animation
When printing on normal devices, the receipt image animates upward (simulating paper feeding):
startPrintAnimation(); // Moves receipt image upward with fade effectResource Management
Bitmaps are properly cleaned up to prevent memory leaks:
@Override
protected void onDestroy() {
if (mBitmap != null && !mBitmap.isRecycled()) {
mBitmap.recycle();
mBitmap = null;
}
}Error Handling
- Checks if bitmap is ready before printing
- Regenerates receipt if bitmap is null or recycled
- Disables print buttons during printing to prevent duplicate requests
Usage Summary
- Launch Activity with transaction data
- Wait for receipt generation (shows loading indicator)
- Preview receipt (on normal screens)
- Click Print Button to send to printer
- Handle Result (success dialog or error with retry option)
The printer hardware communication is abstracted through the PrinterDevice SDK (likely from Dspread), so you don't need to handle low-level printer commands directly.