I have implemented an Android app which communicates with the DLP NIRscan Nano via Bluetooth LE. It is working fine, but unfortunately it takes a long time to transfer the scan results - especially if you want to do several individual scans in a row.
That's why I'm currently trying to connect the device via an USB OTG cable to the Android smartphone. I can successfully connect to the device via USB but it is unclear how to read or write data.
What I have understood so far:
the device provides two endpoints: one with IN direction and one with OUT direction.
both endpoints have interrupt type, that's why I need to communicate asynchronously (see https://developer.android.com/reference/android/hardware/usb/UsbRequest.html) by using queue() and requestWait() methods.
based on the documentation the device is using HID 1.1 to exchange commands. An example project would be the Missile Launcher: https://github.com/aosp-mirror/platform_development/blob/master/samples/USB/MissileLauncher/src/com/android/missilelauncher/MissileLauncherActivity.java which is using
UsbRequest#queue()
sendCommand()
which is callingmConnection.controlTransfer(0x21, 0x9, 0x200, 0, message, message.length, 0);
requestWait()
to wait until a response is available. But this is not working as well. Also changing the sendCommand toint transfer = mConnectionRead.controlTransfer(0xA1, 0x01, 0x00, 0x01, message, message.length, 0);
doesn't work (transfer = -1), see Using Android to Communicate with a USB HID Device
When I want to read e.g. the Tiva version information, I thought it will work like the code mentioned below. The specification of the device: http://www.ti.com/lit/ug/dlpu030g/dlpu030g.pdf see page 83
Why is it not working? I never get any response from the sensor.
Do I have to use controlTransfer? Is the data packet wrong? Is it possible at all to connect the scanner via USB to an Android smartphone?
Any help appreciated :-)
Thanks Ben
UsbConfiguration usbConfiguration = usbDevice.getConfiguration(0);
UsbInterface usbInterface = usbConfiguration.getInterface(0);
inEndpoint = usbInterface.getEndpoint(0);
outEndpoint = usbInterface.getEndpoint(1);
connection = usbManager.openDevice(usbDevice);
connection.claimInterface(usbInterface, true);
byte[] data = new UsbPacket()
.setGroupByte((byte) 0x02)
.setCommandByte((byte) 0x16)
.setFlagRW(UsbPacket.RW.READ)
.setFlagReady(UsbPacket.READY.READY)
.toByteArray();
UsbRequest request = new UsbRequest();
request.initialize(connection, outEndpoint);
request.queue(ByteBuffer.wrap(data), data.length);
connection.requestWait();
UsbRequest request1 = new UsbRequest();
request1.initialize(connection, inEndpoint);
byte[] result = new byte[28];
request1.queue(ByteBuffer.wrap(result), result.length);
connection.requestWait(); // Actual: never terminates!
// Expected: result byte array contains Tiva version information
class UsbPacket {
private byte flags;
private int sequence = 0;
private byte commandByte;
private byte groupByte;
private byte[] data;
enum RW {
WRITE,
READ
}
enum READY {
BUSY,
READY
}
enum ERROR {
SUCCESS,
ERROR,
BUSY
}
UsbPacket setFlagRW(RW flag) {
if (flag == RW.READ) {
this.flags = (byte) (this.flags | 0x80);
}
return this;
}
UsbPacket setFlagReady(READY flag) {
if (flag == READY.READY) {
this.flags = (byte) (this.flags | 0x40);
}
return this;
}
UsbPacket setFlagError(ERROR flag) {
if (flag == ERROR.ERROR) {
this.flags = (byte) (this.flags | 0x20);
}
if (flag == ERROR.BUSY) {
this.flags = (byte) (this.flags | 0x10);
}
return this;
}
UsbPacket setSequence(int sequence) {
if (0 > sequence || sequence > 255) {
throw new IllegalArgumentException("Only values from 0 to 255 are allowed");
}
this.sequence = sequence;
return this;
}
UsbPacket setCommandByte(byte commandByte) {
this.commandByte = commandByte;
return this;
}
UsbPacket setGroupByte(byte groupByte) {
this.groupByte = groupByte;
return this;
}
public UsbPacket setData(byte[] data) {
this.data = data;
return this;
}
byte[] toByteArray() {
byte[] dataLength;
int lengthOfCommandBytes = 2;
if (data != null) {
dataLength = ConvertUtility.convertTo2ByteArray(lengthOfCommandBytes + data.length);
}
else {
dataLength = ConvertUtility.convertTo2ByteArray(lengthOfCommandBytes);
}
byte[] header = new byte[] {
0x00,
flags,
(byte) sequence,
dataLength[0],
dataLength[1],
commandByte,
groupByte
};
byte[] packet;
if (data != null) {
packet = new byte[7 + data.length];
System.arraycopy(header, 0, packet, 0, header.length);
System.arraycopy(data, 0, packet, 7, data.length);
}
else {
packet = header;
}
return packet;
}
}