TI DLP NIRScan Nano:通过USB连接到Android智能手机
我已经实现了一个Android应用程序,它通过蓝牙与DLP NIRscan Nano通信。它工作正常,但不幸的是,传输扫描结果需要很长时间,特别是如果您想连续执行多个单独的扫描 这就是为什么我目前正试图通过USB OTG电缆将设备连接到Android智能手机。我可以通过USB成功连接到设备,但不清楚如何读取或写入数据 到目前为止,我的理解是:TI DLP NIRScan Nano:通过USB连接到Android智能手机,android,usb,hid,Android,Usb,Hid,我已经实现了一个Android应用程序,它通过蓝牙与DLP NIRscan Nano通信。它工作正常,但不幸的是,传输扫描结果需要很长时间,特别是如果您想连续执行多个单独的扫描 这就是为什么我目前正试图通过USB OTG电缆将设备连接到Android智能手机。我可以通过USB成功连接到设备,但不清楚如何读取或写入数据 到目前为止,我的理解是: 该设备提供两个端点:一个为IN方向,另一个为OUT方向 两个端点都有中断类型,这就是为什么我需要使用queue()和requestWait()方法进行异
- 该设备提供两个端点:一个为IN方向,另一个为OUT方向
- 两个端点都有中断类型,这就是为什么我需要使用queue()和requestWait()方法进行异步通信(请参阅)
- 根据文档,设备正在使用HID 1.1来交换命令。导弹发射器就是一个例子:它正在使用
UsbRequest#queue()
调用sendCommand()
mConnection.controlTransfer(0x21、0x9、0x200、0、message、message.length、0)代码>
等待响应可用。 但这并不奏效。还将send命令更改为requestWait()
int transfer=mConnectionRead.controlTransfer(0xA1、0x01、0x00、0x01,message,message.length,0)代码>不起作用(传输=-1),请参阅
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;
}
}