// 返回打印机状态 public static String getStatus(short vendorId, short productId) { String status; Device dev = USB.getDevice(vendorId, productId); try { if (dev != null && !dev.isOpen()) { dev.open(1, 0, -1); } byte[] data = new byte[] {0x0D}; int timeout = 2000; dev.writeBulk(0x2, data, data.length, timeout, false); status = Const.print_online; } catch (USBException e) { e.printStackTrace(); String errMsg = e.getMessage(); if (errMsg.contains("timeout error")) { status = Const.print_papaer_out; } else if (errMsg.contains("not found on USB")) { status = Const.print_offline; } else { status = Const.print_offline; } if (dev != null) { try { dev.reset(); } catch (USBException e1) { e1.printStackTrace(); } } } return status; }
// 打印字节数组 public static String print(short vendorId, short productId, byte[] data) { String result = PrintMsg.status_print_no; Device dev = USB.getDevice(vendorId, productId); try { if (dev != null && !dev.isOpen()) { dev.open(1, 0, -1); } int timeout = 2000; dev.writeBulk(0x2, data, data.length, timeout, false); result = PrintMsg.status_print_yes; } catch (USBException e) { result = PrintMsg.status_print_err; e.printStackTrace(); if (dev != null) { try { dev.reset(); } catch (USBException e1) { e1.printStackTrace(); } } } return result; }