コード例 #1
0
  private void setDevice(UsbDevice device) {
    mConnection = mUsbManager.openDevice(device);
    intf = device.getInterface(0);
    UsbEndpoint epOut = null;
    UsbEndpoint epIn = null;
    UsbEndpoint epEv = null;
    // Looking for Bulk Endpoints
    for (int i = 0; i < intf.getEndpointCount(); i++) {
      UsbEndpoint ep = intf.getEndpoint(i);
      if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
        if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
          epOut = ep;
        } else {
          epIn = ep;
        }
      }
      if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
        epEv = ep;
      }
    }
    if (epOut == null || epIn == null || epEv == null) {
      throw new IllegalArgumentException("not all endpoints found");
    }

    mEndpointBulkOut = epOut;
    mEndpointBulkIn = epIn;
    mEndpointIntr = epEv;
    inMaxPS = mEndpointBulkIn.getMaxPacketSize();
  }
コード例 #2
0
    @Override
    public int read(byte[] dest, int timeoutMillis) throws IOException {
      final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0);

      if (ENABLE_ASYNC_READS) {
        final int readAmt;
        synchronized (mReadBufferLock) {
          // mReadBuffer is only used for maximum read size.
          readAmt = Math.min(dest.length, mReadBuffer.length);
        }

        final UsbRequest request = new UsbRequest();
        request.initialize(mConnection, endpoint);

        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, readAmt)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.requestWait();
        if (response == null) {
          throw new IOException("Null response");
        }

        final int payloadBytesRead = buf.position() - MODEM_STATUS_HEADER_LENGTH;
        if (payloadBytesRead > 0) {
          Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
          return payloadBytesRead;
        } else {
          return 0;
        }
      } else {
        final int totalBytesRead;

        synchronized (mReadBufferLock) {
          final int readAmt = Math.min(dest.length, mReadBuffer.length);
          totalBytesRead = mConnection.bulkTransfer(endpoint, mReadBuffer, readAmt, timeoutMillis);

          if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) {
            throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes");
          }

          return filterStatusBytes(mReadBuffer, dest, totalBytesRead, endpoint.getMaxPacketSize());
        }
      }
    }