Interface USBDevice


public interface USBDevice
USB device.

In order to make control requests and transfer data, the device must be opened and an interface must be claimed. In the open state, this current process has exclusive access to the device.

Information about the device can be queried in both the open and the closed state.

  • Method Details

    • productId

      int productId()
      USB product ID.
      Returns:
      product ID
    • vendorId

      int vendorId()
      USB vendor ID.
      Returns:
      vendor ID
    • product

      String product()
      Product name.
      Returns:
      product name or null if not provided by the device
    • manufacturer

      String manufacturer()
      Manufacturer name
      Returns:
      manufacturer name or null if not provided by the device
    • serialNumber

      String serialNumber()
      Serial number

      Even though this is supposed to be a human-readable string, some devices are known to provide binary data.

      Returns:
      serial number or null if not provided by the device
    • classCode

      int classCode()
      USB device class code (bDeviceClass from device descriptor).
      Returns:
      class code
    • subclassCode

      int subclassCode()
      USB device subclass code (bDeviceSubClass from device descriptor).
      Returns:
      subclass code
    • protocolCode

      int protocolCode()
      USB device protocol (bDeviceProtocol from device descriptor).
      Returns:
      protocol code
    • open

      void open()
      Opens the device for communication.
    • isOpen

      boolean isOpen()
      Indicates if the device is open.
      Returns:
      true if the device is open, false if it is closed.
    • close

      void close()
      Closes the device.
    • interfaces

      List<USBInterface> interfaces()
      Gets the interfaces of this device.
      Returns:
      a list of USB interfaces
    • claimInterface

      void claimInterface(int interfaceNumber)
      Claims the specified interface for exclusive use.
      Parameters:
      interfaceNumber - the interface number
    • releaseInterface

      void releaseInterface(int interfaceNumber)
      Releases the specified interface from exclusive use.
      Parameters:
      interfaceNumber - the interface number
    • controlTransferIn

      byte[] controlTransferIn(USBControlTransfer setup, int length)
      Requests data from the control endpoint.

      This method blocks until the device has responded or an error has occurred.

      The control transfer request is sent to endpoint 0. The transfer is expected to have a Data In stage.

      Parameters:
      setup - control transfer setup parameters
      length - maximum length of expected data
      Returns:
      received data.
    • controlTransferOut

      void controlTransferOut(USBControlTransfer setup, byte[] data)
      Executes a control transfer request and optionally sends data.

      This method blocks until the device has acknowledge the request or an error has occurred.

      The control transfer request is sent to endpoint 0. The transfer is expected to either have no data stage or a Data Out stage.

      Parameters:
      setup - control transfer setup parameters
      data - data to send, or null if the transfer has no data stage.
    • transferOut

      void transferOut(int endpointNumber, byte[] data)
      Sends data to this device.

      This method blocks until the data has been sent or an error has occurred.

      This method is suitable for bulk and interrupt endpoints.

      Parameters:
      endpointNumber - endpoint number (in the range between 1 and 127)
      data - data to send
    • transferIn

      byte[] transferIn(int endpointNumber, int maxLength)
      Receives data to this device.

      This method blocks until at least a packet has been received or an error has occurred. The minimum value for maxLength is the maximum size of packets sent on the endpoint.

      This method is suitable for bulk and interrupt endpoints.

      Parameters:
      endpointNumber - endpoint number (in the range between 1 and 127, i.e. without the direction bit)
      maxLength - the maximum data length to receive (in number of bytes)
      Returns:
      received data