USB OTG连接Epson L130打印PDF异常:输出字节数组求助
Hey there! Let's figure out why your Epson L130 is printing raw byte arrays instead of your actual PDF content when using USB OTG. I’ve broken down the key issues and fixes to help you resolve this:
Your Epson L130 is an inkjet printer that uses ESC/P-R (Epson Standard Code for Printers/ Raster)—it can’t interpret raw PDF file bytes directly. When you send the PDF’s raw byte array via bulkTransfer, the printer just sees unrecognizable data and prints it as garbage.
You’ve got two solid paths to get your PDF printing correctly:
Option 1: Convert PDF to Printer-Compatible Raster Data
First, render your PDF pages into bitmaps, then convert those bitmaps into ESC/P-R commands the L130 understands. Here’s a simplified example using Android’s built-in PdfRenderer:
// Render a PDF page to a Bitmap ParcelFileDescriptor pdfDescriptor = ParcelFileDescriptor.open(yourPdfFile, ParcelFileDescriptor.MODE_READ_ONLY); PdfRenderer pdfRenderer = new PdfRenderer(pdfDescriptor); PdfRenderer.Page page = pdfRenderer.openPage(0); // Open first page // Create a bitmap matching the page size Bitmap pageBitmap = Bitmap.createBitmap( page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888 ); page.render(pageBitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT); // Clean up resources page.close(); pdfRenderer.close(); pdfDescriptor.close(); // Now convert the bitmap to ESC/P-R commands and send via bulkTransfer // Start with a printer reset command: ESC @ (0x1B, 0x40) byte[] initCommand = {0x1B, 0x40}; usbDeviceConnection.bulkTransfer(outEndpoint, initCommand, initCommand.length, TIMEOUT); // Add code here to convert bitmap to ESC/P-R raster data (match L130's 576x1440 DPI) // Send the raster data via bulkTransfer next
You’ll need to reference the Epson ESC/P-R Command Manual for the L130 to get the exact commands for sending raster data, setting paper size, and triggering prints.
Option 2: Use Android’s Built-In Print Framework (Easier!)
If your device supports it, let Android handle the heavy lifting with the PrintManager. This framework automatically converts PDFs to printer-compatible data and manages USB OTG communication:
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); String printJobName = getString(R.string.app_name) + " PDF Print"; // Create a print adapter for your PDF file PrintDocumentAdapter pdfPrintAdapter = new PdfDocumentAdapter(yourPdfFile); // Start the print job printManager.print(printJobName, pdfPrintAdapter, new PrintAttributes.Builder().build());
This skips dealing with raw USB commands entirely—Android takes care of translating the PDF into something the L130 can print.
- Double-check your USB endpoint: Printers use an OUT endpoint for sending data. Use
UsbInterface.getEndpoint(index)to confirm you’re sending to the right one. - Verify
bulkTransferreturns a positive number: If it returns-1, there’s a USB communication error (check permissions, device connection, or endpoint selection). - Send initialization commands first: Always send the
ESC @reset command (0x1B, 0x40) before sending print data to set the printer to default settings.
Before tackling PDFs, test with a simple text print to confirm your USB setup works. Send this byte array via bulkTransfer:
byte[] testTextCommand = {0x1B, 0x40, 'H', 'e', 'l', 'l', 'o', '!', 0x0A}; usbDeviceConnection.bulkTransfer(outEndpoint, testTextCommand, testTextCommand.length, TIMEOUT);
If this prints "Hello!", your USB communication is working, and the issue is definitely with PDF-to-printer-data conversion.
Hope these steps get your printer churning out proper PDFs in no time!
内容的提问来源于stack exchange,提问作者Ravina

