question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Print failed when using USB connection

See original GitHub issue

I am creating an image and print it via this library, when I am using the Bluetooth connection everything is okay and print without any problem, but when I change the connection type to USB, the print failed as the attached image shows (it doesn’t even cut the paper). The code using to create image and print are the same and just the connection is different, I am wondering what is the problem and should I set anything special for the USB connection?

                    val connection = when (printer.type) {
                        PrinterType.Bluetooth -> {
                            val bluetoothDevice = bluetoothAdapter?.getRemoteDevice(printer.address)
                            BluetoothConnection(bluetoothDevice)
                        }
                        PrinterType.Wifi -> {
                            TcpConnection(printer.address, printer.port)
                        }
                        PrinterType.USB -> {
                            UsbConnections(context).list?.find { connection ->
                                printer.address == connection.device.deviceName &&
                                        printer.vendorId == connection.device.vendorId &&
                                        printer.productId == connection.device.productId
                            }
                        }
                    } ?: throw Exception("Cannot connect to the printer (${printer.name})")
                    var escPosPrinter: EscPosPrinter? = null
                    try {
                        escPosPrinter = EscPosPrinter(connection, 201, 72f, 48)
                        val textToPrint = StringBuilder()
                        var y = 0
                        while (y < height) {
                            val bitmap = Bitmap.createBitmap(
                                result,
                                0,
                                y,
                                width,
                                if (y + 256 >= height) height - y else 256
                            )
                            textToPrint.append("[C]<img>")
                                .append(
                                    PrinterTextParserImg.bitmapToHexadecimalString(
                                        escPosPrinter,
                                        bitmap
                                    )
                                )
                                .append("</img>\n")
                            y += 256
                        }
                        textToPrint.append("[L]\n")
                        textToPrint.append("[C]<qrcode size='20'>https://test.app/menu.html?branch_id=")
                            .append(branch.id.toString()).append("&table_id=0</qrcode>\n")
                        textToPrint.append("[L]\n")
                        textToPrint.append("[C]All Rights Reserved\n")
                        escPosPrinter.printFormattedText(textToPrint.toString())
                        escPosPrinter.printFormattedTextAndCut("[L]\n")
                        escPosPrinter.openCashBox()
                    } catch (e: EscPosConnectionException) {
                        showMessage("Cannot connect to the printer (${printer.name})")
                    } catch (ex: Exception) {
                        showMessage(ex.message.toString())
                    } finally {
                        escPosPrinter?.disconnectPrinter()
                    }

photo_2021-09-08 20 51 04

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iAmAndroidDevelopercommented, Sep 13, 2021

Ok NP @MMohamad70 let you know tomorrow morning

0reactions
AtiaDjabercommented, Oct 17, 2021

same with me i test DrawReceipt and xml to generate bitmap but failed print correctly

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Solve a Printer Not Recognized Error - Driver Support
Removing the faulty USB Hub from the Device Manager and reinstalling it may fix the problem with the specific USB port not resetting....
Read more >
Unable to Print When Connected Via USB | Dell US
Test by connecting the printer via USB to another computer. If that fails, uninstall the printer driver completely.
Read more >
How to Fix USB Printing Support Windows 10/Mac
Multiple scenarios cause this issue. One may be a faulty USB cable that prevents the printer to be detected by the computer. It...
Read more >
Cannot print while using USB printer - Microsoft Community
1) Power up the printer, if wired unplug it's USB cable from PC. 2) Go to Settings>Apps>Apps and Features to find and Uninstall...
Read more >
Data communication stops during the print from the printer ...
(1) Try turning off the printer and RIP computer, then unplug the USB cable once and reconnect the cable and turn on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found