[英]In android usb host transfer data with usb device?
我使用Android USB主机连接USB磁盘,我想读写数据到USB磁盘,但是当我bulkTransfer失败。
接收的缓冲区(strBuf)
为空,没有接收到字节。 我认为可能是发送到USB设备的字节(bytes_w)
不正确,但我对此一无所知。
这是我的代码:
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() ;
}
我看不到您是否设置了用于发送和接收数据的波特率。 这是我尝试在应用程序中设置通信时首先遇到的问题。
如果您希望波特率为9600,请编写以下代码:
connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); // Sets the baudrate to 9600
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.