【问题标题】:In android usb host transfer data with usb device?在 android usb 主机中使用 USB 设备传输数据?
【发布时间】:2013-05-31 23:58:09
【问题描述】:

我用安卓USB主机连接一个U盘,我想对U盘读写数据, 但是当我 bulkTransfer 失败了。

接收到的缓冲区(strBuf) 是空的,没有接收到字节。我认为这可能是字节 (bytes_w)send 到 USB 设备不正确,但我不知道。

这是我的代码:

private String findInterface() {
    boolean foreClaim = true;
    byte[] bytes_w = null;
    byte[] bytes_r = new byte[1024];
    byte[] send_command = {(byte)0xef, 0x01,(byte) 0x80, 0x01, 0x00,0x00, 0x00,0x00,0x00,0x00 };
    byte[] receive_result = {(byte) 0xef, 0x02};
    int TIMEOUT = 0;
    StringBuffer strBuf_send = new StringBuffer();
    StringBuffer strBuf_receive = new StringBuffer();
    if ( device == null ) {
        return " " + -2;
    }else {

            intf = device.getInterface(0);

            epIN = intf.getEndpoint(0); //0 -in read
            epOUT = intf.getEndpoint(1); // 1- out write
            connection = uManager.openDevice(device);

            if ( !connection.claimInterface(intf, foreClaim)) {
                connection.close();
            }
            bytes_w = send_command;
            int w= connection.bulkTransfer(epOUT, bytes_w, bytes_w.length, 3000);

            int r = connection.bulkTransfer(epIN, bytes_r, bytes_r.length, 3000); 


            for (int i=0; i<bytes_r.length; i++) {
                if (bytes_r[i] != 0) {
                    strBuf_send.append(bytes_r[i] + "," );
                }

            }

            bytes_w = receive_result;
            int w2= connection.bulkTransfer(epOUT, bytes_w, bytes_w.length, 3000);
            int r2 = connection.bulkTransfer(epIN, bytes_r, bytes_r.length, 3000); 
            // read returned buffer 
            for (int i=0; i<bytes_r.length; i++) {
                if (bytes_r[i] != 0) {
                    strBuf_receive.append(bytes_r[i] + "," );
                }


            }
            return " write: " + w + ", read:" + r + "read buffer " + strBuf_send.toString() +
                    " write: " + w2 + ", read:" + r2 + "read buffer " + strBuf_receive.toString() ; 

}

【问题讨论】:

    标签: android usb buffer host data-transfer


    【解决方案1】:

    我看不到您是否设置了发送和接收数据的波特率。 这是我尝试在应用程序中设置通信时第一次遇到的问题。

    如果您希望波特率为 9600,请编写以下代码:

    connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); // Sets the baudrate to 9600
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多