I use the Android USB host to connect a USB disk, I want to read and write data to the USB disk, but when I bulkTransfer it failed.
The received buffer (strBuf)
is empty, no bytes received. I think it's maybe the bytes (bytes_w)
send to usb device is not right, but i have no idea about it.
Here is my code:
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() ;
}