Class Usb
-
Method Summary
Modifier and TypeMethodDescriptionfindDevice(int vendorId, int productId) Gets the first connected USB device with the specified vendor and product ID.findDevice(@NotNull UsbDevicePredicate predicate) Gets the first connected USB device matching the specified predicate.findDevices(@NotNull UsbDevicePredicate predicate) Gets a list of connected USB devices matching the specified predicate.static @NotNull @Unmodifiable Collection<UsbDevice> Gets a list of all connected USB devices.static voidsetOnDeviceConnected(@Nullable Consumer<UsbDevice> handler) Sets the handler to be called when a USB device is connected.static voidsetOnDeviceDisconnected(@Nullable Consumer<UsbDevice> handler) Sets the handler to be called when a USB device is disconnected.
-
Method Details
-
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
Gets the first connected USB device matching the specified predicate.- Parameters:
predicate- device predicate- Returns:
- optional USB device
-
findDevice
-
setOnDeviceConnected
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, ornullto remove a previous handler
-
setOnDeviceDisconnected
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
UsbDeviceinstance 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, ornullto remove a previous handler
-