阅读此问题后:Android USB Permissions Dialog never appears and doing some testing,我有以下问题:
我也需要打印到 USB 打印机(仅纯文本),我建立连接,出现对话框,但我现在不知道在这之后做什么。任何人都可以帮助我在 openPort(aDevice) 中必须做什么?
这是我尝试的:
................
if (mUsbManager.hasPermission(mDevice)){
final UsbDeviceConnection mConnection = mUsbManager.openDevice(mDevice);
intf = mDevice.getInterface(0);
for (int i = 0; i < intf.getEndpointCount(); i++) {
UsbEndpoint ep = intf.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
mEndpointBulkOut = ep;
UsbDeviceConnection connection = mUsbManager.openDevice(mDevice);
if(connection!=null)
{
Log.e(TAG," Conectado");
}
connection.claimInterface(intf, forceClaim);
//Integer res = connection.bulkTransfer(mEndpointBulkOut, bytes, bytes.length, TIMEOUT);
new Thread(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
Log.i(TAG, "in run thread");
int b = mConnection.bulkTransfer(mEndpointBulkOut, bytes, bytes.length, 100000);
Log.i(TAG, "b-->" + b);
}
}).start();
connection.releaseInterface(intf);
break;
}
}
}
}
else
mUsbManager.requestPermission(mDevice, mPermissionIntent);
break;`
…………
mConnection.bulkTransfer 总是返回 -1