Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does Java access USB devices

Shulou Source: shulou.com Published: 2022-06-01 04:45:48 09月23日 Update

This article introduces the knowledge of "how Java accesses USB devices". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Editor's note

Because of the need to forward and broadcast the values obtained by a usb device, the most familiar java is chosen as the cut-in language solution. After hard work, the test was successful.

JSR-80

By looking at the standards association of Java, it is established by JSR-80 when setting standards. API is javax.usb. However, with the current (JDK 8 as the default), neither J2ME, J2SE, or J2EE has implemented JSR.

JSR80

API

Javax.usb API

Simple

Javax-usb-example

problem

By reading, you can see that there is no Windows implementation, or that there is a problem with the Windows implementation, which requires another way. We implement javax.usb through the connection of usb4java-javax through the implementation of usb4java library.

Usb4java

Usb4java official website

Usb4java-quick start guide

Through a quick start to learn about the two kinds of api of usb4java, the underlying API is for libusb and the high-level API is for javax.usb

High-level (javax-usb) API

Note: javax.usb.properties must be added

Implement

The development environment is Windows

Reference library

Here is a chained reference to Gradle

Compile group: 'javax.usb', name:' usb-api', version: '1.0.2' compile group: 'org.usb4java', name:' usb4java', version: '1.2.0' compile group: 'org.usb4java', name:' usb4java-javax', version: '1.2.0' configuration file

Add a configuration file under the Java project with the path src\ resources\ javax.usb.properties

# src\ resources\ javax.usb.propertiesjavax.usb.services = org.usb4java.javax.Services test code package org.acifve.previz.broadcastserver.hid;import org.apache.commons.lang3.builder.ReflectionToStringBuilder;import org.junit.Test;import javax.usb.*;import java.util.ArrayList;import java.util.List;import static org.junit.Assert.*;public class UsbHIDSenderTest {@ Test public void getUsbs () throws UsbException {UsbServices usbServices = UsbHostManager.getUsbServices (); UsbHub usbHub = usbServices.getRootUsbHub () List usbDevices = getAllUsbDevices (usbHub); for (UsbDevice usbDevice: usbDevices) {System.out.println (ReflectionToStringBuilder.toString (usbDevice));} public static List getAllUsbDevices (UsbDevice usbDevice) {List list = new ArrayList (); list.add (usbDevice); / * this is just normal recursion. Nothing special. * / if (usbDevice.isUsbHub ()) {List devices = ((UsbHub) usbDevice) .getAttachedUsbDevices (); for (int iTuno; I

Tags: Equipment content files more standards knowledge problems tests output configuration utility success learning next code associations dilemmas actual underlying situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Xiaomi Microsoft Shulou Technology