2013-05-14 44 views
1

的USB打印机阅读这个问题后:Android USB Permissions Dialog never appears和做一些测试,我有以下问题:发送纯文本到Android中

我需要打印到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 alwais返回-1

+0

你能做到吗? – 2013-11-10 01:22:01

回答

1

您的mConnection为空,因为它是另一个线程,并且新线程无权访问此UsbDeviceConnection。 重建你的应用程序,更仔细地关注USB host,或者至少尝试将mConnection放在Singleton中。

你也可以尝试连接像应用程序的代理:Net2usb