我想通过usb连接到zebra pribter并打印一些内容。但是,当我单击按钮打印时,什么也没发生。我看到我的设备连接到了打印机,我看到了:
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mDeviceList = mUsbManager.getDeviceList();
if (mDeviceList.size() > 0) {
mDeviceIterator = mDeviceList.values().iterator();
Toast.makeText(this, "Device List Size: " + String.valueOf(mDeviceList.size()), Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.usbDevice);
String usbDevice = "";
while (mDeviceIterator.hasNext()) {
UsbDevice usbDevice1 = mDeviceIterator.next();
usbDevice += "\n" +
"DeviceID: " + usbDevice1.getDeviceId() + "\n" +
"DeviceName: " + usbDevice1.getDeviceName() + "\n" +
"Protocol: " + usbDevice1.getDeviceProtocol() + "\n" +
// "Product Name: " + usbDevice1.getProductName() + "\n" +
// "Manufacturer Name: " + usbDevice1.getManufacturerName() + "\n" +
"DeviceClass: " + usbDevice1.getDeviceClass() + " - " + translateDeviceClass(usbDevice1.getDeviceClass()) + "\n" +
"DeviceSubClass: " + usbDevice1.getDeviceSubclass() + "\n" +
"VendorID: " + usbDevice1.getVendorId() + "\n" +
"ProductID: " + usbDevice1.getProductId() + "\n";
int interfaceCount = usbDevice1.getInterfaceCount();
Toast.makeText(this, "INTERFACE COUNT: " + String.valueOf(interfaceCount), Toast.LENGTH_SHORT).show();
mDevice = usbDevice1;
Toast.makeText(this, "Device is attached", Toast.LENGTH_SHORT).show();
textView.setText(usbDevice);
}
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
mUsbManager.requestPermission(mDevice, mPermissionIntent);
} else {
Toast.makeText(this, "Please attach printer via USB", Toast.LENGTH_SHORT).show();
}
private void print(final UsbDeviceConnection connection, final UsbInterface usbInterface) {
final String test = ed_txt.getText().toString() + "\n\n";
testBytes = test.getBytes();
if (usbInterface == null) {
Toast.makeText(this, "INTERFACE IS NULL", Toast.LENGTH_SHORT).show();
} else if (connection == null) {
Toast.makeText(this, "CONNECTION IS NULL", Toast.LENGTH_SHORT).show();
} else if (forceCLaim == null) {
Toast.makeText(this, "FORCE CLAIM IS NULL", Toast.LENGTH_SHORT).show();
} else {
connection.claimInterface(usbInterface, forceCLaim);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
byte[] cut_paper = {0x1D, 0x56, 0x41, 0x10};
connection.bulkTransfer(mEndPoint, testBytes, testBytes.length, 0);
connection.bulkTransfer(mEndPoint, cut_paper, cut_paper.length, 0);
}
});
thread.run();
}
}
我已连接到设备,但是当我单击按钮打印后,该按钮无法打印时,我不知道自己做错了什么