Class Usb

java.lang.Object
net.codecrete.usb.Usb

public class Usb extends Object
Provides access to USB devices.
  • Method Details

    • getDevices

      @NotNull public static @NotNull @Unmodifiable Collection<UsbDevice> getDevices()
      Gets a list of all connected USB devices.

      Depending on the operating system, the list might or might not include USB hubs and USB host controllers.

      Returns:
      list of USB devices
    • findDevices

      @NotNull public static @NotNull @Unmodifiable List<UsbDevice> findDevices(@NotNull @NotNull UsbDevicePredicate predicate)
      Gets a list of connected USB devices matching the specified predicate.
      Parameters:
      predicate - device predicate
      Returns:
      list of USB devices
    • findDevice

      public static Optional<UsbDevice> findDevice(@NotNull @NotNull UsbDevicePredicate predicate)
      Gets the first connected USB device matching the specified predicate.
      Parameters:
      predicate - device predicate
      Returns:
      optional USB device
    • findDevice

      public static Optional<UsbDevice> findDevice(int vendorId, int productId)
      Gets the first connected USB device with the specified vendor and product ID.
      Parameters:
      vendorId - vendor ID
      productId - product ID
      Returns:
      optional USB device
    • setOnDeviceConnected

      public static void setOnDeviceConnected(@Nullable @Nullable Consumer<UsbDevice> handler)
      Sets the handler to be called when a USB device is connected.

      The handler is called from a background thread.

      The handler should not execute any time-consuming operations but rather return quickly. While the handler is being executed, maintaining the list of connected devices is paused, methods of this class (such as getDevices()) will possibly work with an outdated list of connected devices and handlers for connect and disconnect events will not be called.

      Parameters:
      handler - handler function, or null to remove a previous handler
    • setOnDeviceDisconnected

      public static void setOnDeviceDisconnected(@Nullable @Nullable Consumer<UsbDevice> handler)
      Sets the handler to be called when a USB device is disconnected.

      The handler is called from a background thread.

      When the handler is called, the UsbDevice instance has already been closed. Descriptive information (such as vendor and product ID, serial number, interfaces, endpoints) can still be accessed.

      If the application was communicating with the device when it was disconnected, it will also receive an error for those operations. Due to the concurrency of the USB stack, there is no particular order for the disconnect event and the transmission errors.

      The handler should not execute any time-consuming operations but rather return quickly. While the handler is being executed, maintaining the list of connected devices is paused, methods of this class (such as getDevices()) will possibly work with an outdated list of connected devices and handlers for connect and disconnect events will not be called.

      Parameters:
      handler - handler function, or null to remove a previous handler