public boolean OpenDevice() {
_usbManager = (UsbManager) _context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = _usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
_usbDevice = null;
// Iterate all the available devices and find ours.
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
if (device.getProductId() == _productId && device.getVendorId() == _vendorId) {
_usbDevice = device;
_deviceName = _usbDevice.getDeviceName();
}
}
if (_usbDevice == null) {
Toast.makeText(_context,"Cannot find the device. Did you forgot to plug it?",Toast.LENGTH_SHORT).show();
return false;
}
// Create and intent and request a permission.
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(_context, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
_context.registerReceiver(mUsbReceiver, filter);
_usbManager.requestPermission(_usbDevice, mPermissionIntent);
UsbInterface writeIntf = _usbDevice.getInterface(0);
epOUT=writeIntf.getEndpoint(1);
epIN=writeIntf.getEndpoint(0);
mUsbDeviceConnection=_usbManager.openDevice(_usbDevice);
mUsbDeviceConnection.claimInterface(writeIntf, true);
Toast.makeText(_context,"Found the device",Toast.LENGTH_SHORT).show();
return true;
}
public byte[] read(UsbRequest outRequest) throws UsbException {
if (mUsbDeviceConnection == null) {
throw new UsbException("no connection available");
}
ByteBuffer buffer = ByteBuffer.allocate(MAX_PACKAGE_SIZE);
if (outRequest.equals(mUsbDeviceConnection.requestWait())) {
UsbRequest inRequest = new UsbRequest();
inRequest.initialize(mUsbDeviceConnection, epIN);
if (inRequest.queue(buffer, MAX_PACKAGE_SIZE)) {
mUsbDeviceConnection.requestWait();
return buffer.array();
}
}
return null;
}
public UsbRequest write(byte[] command) throws UsbException {
if (mUsbDeviceConnection == null) {
throw new UsbException("no connection available");
}
ByteBuffer buffer = ByteBuffer.allocate(1);
UsbRequest outRequest = new UsbRequest();
outRequest.initialize(mUsbDeviceConnection, epOUT);
buffer.put(command);
outRequest.queue(buffer, 1);
return outRequest;
}
public byte[] sendCommand(byte[] command) throws UsbException {
UsbRequest request = write(command);
_textInfo.setText(_textInfo.getText()+"\n" +request );
return read(request);
}
我正在尝试在endPointOut上推送一些命令并尝试从endPointIN接收数据,但是当我尝试使用mUsbDeviceConnection.requestWait()应用程序读取时没有响应
以下代码说明我如何调用这些函数
byte b=(byte)'x';
byte command[] = {b};
byte[] result= hidBr.sendCommand(command);
我得到一个空白数组