我尝试将命令发送到我的USB设备并获得响应。这是一个自定义扫描仪。我与它有联系,这很好。我现在需要做的是发送命令DA AD 02 74 00来打开设备。我定义了这样的命令:
private final byte[] send = new byte[]{
(byte) 0xDA, (byte) 0xAD, // const
(byte) 0x02, (byte) 0x74, (byte) 0x00, // com
};
我有UsbConnector.class,我设置连接并做其他奇怪的事情。我还有两种方法可以发送和接收数据:
public int send(byte[] bytes) {
return currentUsbDeviceConnection.bulkTransfer(currentUsbEndpointOut, bytes, bytes.length, SEND_TIMEOUT);
}
public byte[] receive(int length) {
byte[] bytes = new byte[length];
int result = currentUsbDeviceConnection.bulkTransfer(currentUsbEndpointIn, bytes, bytes.length, RECEIVE_TIMEOUT);
if (result >= 0) {
return bytes;
}
return null;
}
我想点击按钮后发送数据,所以它是这样的:
mTestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UsbConnector connector = new UsbConnector(getApplicationContext(), device, listener);
connector.run();
connector.send(send);
Toast.makeText(MainActivity.this, "BYTE" + send, Toast.LENGTH_SHORT).show();
mRunning = true;
while (mRunning) {
byte[] received = connector.receive(36);
if (received == null) {
Toast.makeText(MainActivity.this, "NULL", Toast.LENGTH_SHORT).show();
mRunning=false;
}
if (received != null) {
Toast.makeText(MainActivity.this, "NIE NULL", Toast.LENGTH_SHORT).show();
mRunning=false;
}
}
}
但是我收到的时候仍然是空的。请帮助:/