【发布时间】: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