问题
I am working for sending data over USB from an Android app to a connected HID device. On checking for interface and endpoint of the device I have found that the device has two interrupt endpoint for both In and Out direction with Max Packet Size=64.
I have detected the USB device and its endpoint from the code below and struggling to send data from the app to device but didn't get success yet.
private void setConfiguration() {
if (mUsbDevice.getInterfaceCount() != 1) {
Log.e(TAG, "could not find interface");
return;
}
UsbInterface intf = mUsbDevice.getInterface(0);
// device should have one endpoint
if (intf.getEndpointCount() < 1) {
Log.e(TAG, "could not find endpoint");
return;
}
// endpoint should be of type interrupt
mConnection = mUsbManager.openDevice(mUsbDevice);
if(mConnection == null) {
addLogToScreen("Cannot establish a connection");
return;
}
mConnection.claimInterface(intf, true);
for (int i = 0; i < intf.getEndpointCount(); i++) {
UsbEndpoint ep = intf.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
Log.i(TAG, "Interrupt Endpoint");
addLogToScreen("Interrupt Endpoint");
if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
mEndpointOut = ep;
addLogToScreen("Found out endpoint at : " + i);
} else if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
mEndpointIn = ep;
addLogToScreen("Found In endpoint at : " + i);
}
} else {
Log.i(TAG, "Endpoint is not of Interrupt type");
addLogToScreen("Endpoint is not of Interrupt type");
return;
}
}
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
if (null == mStringBuilder) {
addLogToScreen("Need to select file first from Browse");
return;
}
byte[] getFileContent = ("AT" + '\r').getBytes();
int bufferMaxLength = mEndpointOut.getMaxPacketSize();
final ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
UsbRequest outRequest = new UsbRequest(); // create an URB
outRequest.initialize(mConnection, mEndpointOut);
buffer.put(getFileContent);
// queue the outbound request
if (outRequest.queue(buffer) == true) {
addLogToScreen("Queuing operation of sending data to Device get succeeded");
if (mConnection.requestWait() == outRequest) {
// wait for confirmation (request was sent)
final UsbRequest inRequest = new UsbRequest();
// URB for the incoming data
inRequest.initialize(mConnection, mEndpointIn);
// the direction is dictated by this initialisation to the incoming endpoint.
if (inRequest.queue(buffer) == true) {
addLogToScreen("Queuing operation of receiving data from Device get succeeded");
if (mConnection.requestWait() == inRequest) {
// wait for this request to be completed
// at this point buffer contains the data received
addLogToScreen("Response: " + buffer.toString());
}
}else{
addLogToScreen("Queuing operation of receiving data from Device get fails");
}
}
}else{
addLogToScreen("Queuing operation of sending data to Device get failed");
}
}
I am getting the logger till-
Queuing operation of sending data to Device get succeeded
Queuing operation of receiving data from Device get succeeded
Please correct me for any kind of mistake I did here and guide me to achieve my goal here. Any help is highly appreciated.
Thanks
来源:https://stackoverflow.com/questions/56808341/sending-data-over-usb-on-interrupt-endpoint