Отправка данных через USB на конечную точку прерывания - PullRequest
0 голосов
/ 28 июня 2019

Я работаю для отправки данных через 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

Пожалуйста, исправьте меня за любую ошибку, которую я сделал здесь, и направьте меня к достижению моей цели здесь. Любая помощь высоко ценится.

Спасибо

...