【问题标题】:Unsuccessful bulkTransfer - Android 4.0.3批量传输失败 - Android 4.0.3
【发布时间】:2012-05-06 01:27:49
【问题描述】:

我想通过 bulkTransfer 向通过 USB 连接的相机发送一个短数据包(仅 2 个字符)。我正在使用带有 Android 4.0.3 的三星 Galaxy S2 作为主机。一切似乎都很好,接受......实际上没有发送任何数据。理论上,bulkTransfer 方法返回一个正值,表示数据已经传输,但没有可见的效果。代码如下:

     char ch = (char)34;
    char[] record = {'P',ch};
    String r = record.toString();
    byte[] bytes  =  r.getBytes(Charset.forName("ASCII"));
    int TIMEOUT = 10000;
    boolean forceClaim = true;
    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    UsbInterface intf = device.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) {
              endpoint = ep;
              //Integer dir = endpoint.getDirection();
              UsbDeviceConnection connection = mUsbManager.openDevice(device); 
              if(connection!=null){devMessage+=" I connected";}
              connection.claimInterface(intf, forceClaim);
              Integer res = connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
              if (res>0){devMessage += "some data transfered.";}
              connection.releaseInterface(intf);
            break;

            } 
           }

在开始 bulkTransfer 之前,我还需要提供什么吗?在我开始 bulkTransfer 之前是否需要 controlTransfer?还有什么我可能会忘记的。 请理解,因为这是我的第一个使用 USB 通信的应用程序,并且网络上的资源并不多。我已经在 developer.android 上阅读了有关 USB 主机的所有内容......所以请不要将我指向那里。非常感谢您的帮助。

【问题讨论】:

    标签: android usb host


    【解决方案1】:

    可能是界面不对您使用的是device.getInterface(0)。所以这可能是不对的。试试这个来获取界面。

    for (int i = 0; i < device.getInterfaceCount(); i++) {
            UsbInterface usbif = device.getInterface(i);
    
            UsbEndpoint tOut = null;
            UsbEndpoint tIn = null;
    
            int tEndpointCnt = usbif.getEndpointCount();
            if (tEndpointCnt >= 2) {
                for (int j = 0; j < tEndpointCnt; j++) {
                    if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                        if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) {
                            tOut = usbif.getEndpoint(j);
                        } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) {
                            tIn = usbif.getEndpoint(j);
                        }
                    }
                }
    
                if (tOut != null && tIn != null) {
                    // This interface have both USB_DIR_OUT
                    // and USB_DIR_IN of USB_ENDPOINT_XFER_BULK
                    usbInterface = usbif;
                    endpointOut = tOut;
                    endpointIn = tIn;
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多