调用bulktansfer有时候返回-1
我写了一段程序,通过支持USB OTG的设备发送数据,代码如下,但是发现调用bulkTransfer发送数据的时候,第一次执行成功,第二次会返回-1.如果我把设备拔下来再插入android设备的时候,调用bulktransfer就正常,第二次就返回-1.难得这个bulktransfer反复调用还会有什么问题吗?
UsbInterface usbInterface = usbDevice.getInterface(0);
//USBEndpoint为读写数据所需的节点
int count=usbInterface.getEndpointCount();
for (int k = 0; k < count; k++) {
UsbEndpoint localUsbEndpoint = usbInterface.getEndpoint(k);
if (localUsbEndpoint.getDirection() == UsbConstants.USB_DIR_OUT ) {
outEndpoint = localUsbEndpoint;
} else if (localUsbEndpoint.getDirection() == UsbConstants.USB_DIR_IN) {
inEndpoint = localUsbEndpoint;
}
}
connection = usbManager.openDevice(usbDevice);
Log.d("usbDevice", usbDevice.toString());
connection.claimInterface(usbInterface, true);
Log.d("usbInterface", usbInterface.toString());
//发送数据
byte[] byte2 = new byte[8];
int out = connection.bulkTransfer(outEndpoint, bcmd, bcmd.length, 3000);
Log.e("out", "out:"+out);
int inMax = inEndpoint.getMaxPacketSize();
ByteBuffer byteBuffer = ByteBuffer.allocate(inMax);
UsbRequest usbRequest = new UsbRequest();
if (false==usbRequest.initialize(connection, inEndpoint))
{
Log.d("initialize ", "usbRequest initialize fail");
}
else
{
Log.d("initialize ", "usbRequest initialize OK");
}
if (false==usbRequest.queue(byteBuffer, inMax))
{
Log.d("queue ", "usbRequest queue fail");
}
else
{
Log.d("queue ", "usbRequest queue OK");
}
if(connection.requestWait() == usbRequest){
byte[] retData = byteBuffer.array();
for (int i=0;i<8;i++)
{
rand[i]=retData[i];
}
for(Byte byte1 : retData){
System.err.println(byte1);
}
}