Created
October 5, 2014 12:58
-
-
Save masve/dbccf7942b68cc728ae3 to your computer and use it in GitHub Desktop.
Android Host API: read and write operations (asynchronous)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets the response from the connected device. | |
* @param outRequest UsbRequest referencing the request. | |
* @return Returns the response in a byte[]. | |
* @throws UsbException | |
*/ | |
public byte[] read(UsbRequest outRequest) throws UsbException { | |
if (mUsbDeviceConnection == null) { | |
throw new UsbException("no connection available"); | |
} | |
ByteBuffer buffer = ByteBuffer.allocate(MAX_PACKAGE_SIZE); | |
// Receive data from device | |
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; | |
} | |
/** | |
* Write a command to the connected device. | |
* @param command Byte[] containing the opcode for the command. | |
* @return Returns the response in a byte[]. | |
* @throws UsbException | |
*/ | |
public UsbRequest write(byte[] command) throws UsbException { | |
if (mUsbDeviceConnection == null) { | |
throw new UsbException("no connection available"); | |
} | |
ByteBuffer buffer = ByteBuffer.allocate(MAX_PACKAGE_SIZE); | |
UsbRequest outRequest = new UsbRequest(); | |
outRequest.initialize(mUsbDeviceConnection, epOUT); | |
buffer.put(command); | |
outRequest.queue(buffer, MAX_PACKAGE_SIZE); | |
return outRequest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment