通过USB在中断端点上发送数据

时间:2019-06-28 13:59:23

标签: android usb endpoint data-transfer

我正在努力通过USB将数据从Android应用发送到连接的HID设备。在检查设备的接口和端点时,我发现该设备在输入和输出方向上都有两个中断端点,最大数据包大小为64。

我已经从下面的代码中检测到USB设备及其端点,并努力将数据从应用程序发送到设备,但仍未成功。

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");
    }
}

我要让记录器直到-

Queuing operation of sending data to Device get succeeded
Queuing operation of receiving data from Device get succeeded

请纠正我在这里犯的任何错误,并指导我在这里实现自己的目标。我们非常感谢您的帮助。

谢谢

0 个答案:

没有答案