Print failed when using USB connection
See original GitHub issueI 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()
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok NP @MMohamad70 let you know tomorrow morning
same with me i test DrawReceipt and xml to generate bitmap but failed print correctly