Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java access USB devices

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report