本篇内容介绍了“Java怎么访问USB设备”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
由于需要对某usb设备获取的数值进行转发和广播,则选择最熟悉的java作为切入语言解决。历经辛苦,测试成功。
通过查看Java的标准协会在制定标准的时候是具有JSR-80的制定的。API为javax.usb。但是通过当前(JDK 8作为默认),无论是J2ME、J2SE、J2EE都没有对JSR进行实现。
JSR80
javax.usb API
javax-usb-example
通过阅读,能够得知没有Windows实现,或者Windows实现有问题,则需要其他方式。我们通过usb4java库的实现,在通过usb4java-javax的连接实现javax.usb
usb4java 官网
usb4java - quick start guide
通过快速开始了解到usb4java的两种api,底层API是针对libusb的,高层API是针对javax.usb
High-level (javax-usb) API
注意:必须添加javax.usb.properties
开发环境为Windows
以下是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'
在Java项目下添加配置文件,路径为src\resources\javax.usb.properties
# src\resources\javax.usb.properties
javax.usb.services = org.usb4java.javax.Services
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<UsbDevice> 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 i=0; i<devices.size(); i++)
list.addAll(getAllUsbDevices((UsbDevice)devices.get(i)));
}
return list;
}
}
org.usb4java.javax.RootHub@7dc36524[configurations=[org.usb4java.javax.RootHubConfiguration@3cb5cdba],descriptor=Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.01
bDeviceClass 9 Hub
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0xffff
idProduct 0xffff
bcdDevice 0.00
iManufacturer 1
iProduct 2
iSerial 3
bNumConfigurations 1
,listeners=org.usb4java.javax.DeviceListenerList@d041cf,rootPorts=org.usb4java.javax.Ports@129a8472]
org.usb4java.javax.NonHub@1b0375b3[manager=org.usb4java.javax.DeviceManager@2752f6e2,id=Bus 001 Device 005: ID 138a:0090,parentId=Bus 001 Device 001: ID 8086:9d2f,speed=2,configurations=[USB configuration 01],configMapping={1=USB configuration 01},listeners=org.usb4java.javax.DeviceListenerList@1cd072a9,handle=<null>,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@7c75222b,queue=org.usb4java.javax.ControlIrpQueue@4c203ea1,detachedKernelDriver=false]
org.usb4java.javax.NonHub@27f674d[manager=org.usb4java.javax.DeviceManager@2752f6e2,id=Bus 001 Device 002: ID 17ef:6039,parentId=Bus 001 Device 001: ID 8086:9d2f,speed=2,configurations=[USB configuration 01],configMapping={1=USB configuration 01},listeners=org.usb4java.javax.DeviceListenerList@48140564,handle=<null>,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@58ceff1,queue=org.usb4java.javax.ControlIrpQueue@7c30a502,detachedKernelDriver=false]
org.usb4java.javax.NonHub@49e4cb85[manager=org.usb4java.javax.DeviceManager@2752f6e2,id=Bus 001 Device 004: ID 04f2:b5c1,parentId=Bus 001 Device 001: ID 8086:9d2f,speed=3,configurations=[USB configuration 01],configMapping={1=USB configuration 01},listeners=org.usb4java.javax.DeviceListenerList@43a25848,handle=<null>,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@3ac3fd8b,queue=org.usb4java.javax.ControlIrpQueue@5594a1b5,detachedKernelDriver=false]
org.usb4java.javax.NonHub@6a5fc7f7[manager=org.usb4java.javax.DeviceManager@2752f6e2,id=Bus 001 Device 003: ID 8087:0a2b,parentId=Bus 001 Device 001: ID 8086:9d2f,speed=2,configurations=[USB configuration 01],configMapping={1=USB configuration 01},listeners=org.usb4java.javax.DeviceListenerList@1e643faf,handle=<null>,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@6e8dacdf,queue=org.usb4java.javax.ControlIrpQueue@7a79be86,detachedKernelDriver=false]
org.usb4java.javax.NonHub@34ce8af7[manager=org.usb4java.javax.DeviceManager@2752f6e2,id=Bus 001 Device 009: ID 28de:2022,parentId=Bus 001 Device 001: ID 8086:9d2f,speed=2,configurations=[USB configuration 01],configMapping={1=USB configuration 01},listeners=org.usb4java.javax.DeviceListenerList@880ec60,handle=<null>,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@3f3afe78,queue=org.usb4java.javax.ControlIrpQueue@7f63425a,detachedKernelDriver=false]
“Java怎么访问USB设备”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/hava/blog/1577447