Java怎么访问USB设备
本篇内容介绍了“Java怎么访问USB设备”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
专注于为中小企业提供成都做网站、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业嘉黎免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
编者注
由于需要对某usb设备获取的数值进行转发和广播,则选择最熟悉的java作为切入语言解决。历经辛苦,测试成功。
JSR-80
通过查看Java的标准协会在制定标准的时候是具有JSR-80的制定的。API为javax.usb。但是通过当前(JDK 8作为默认),无论是J2ME、J2SE、J2EE都没有对JSR进行实现。
JSR80
API
javax.usb API
Simple
javax-usb-example
问题
通过阅读,能够得知没有Windows实现,或者Windows实现有问题,则需要其他方式。我们通过usb4java库的实现,在通过usb4java-javax的连接实现javax.usb
usb4java
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(); ListusbDevices = 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 输出
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=,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= ,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= ,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= ,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= ,activeConfigurationNumber=1,claimedInterfaceNumbers=[],port=org.usb4java.javax.Port@3f3afe78,queue=org.usb4java.javax.ControlIrpQueue@7f63425a,detachedKernelDriver=false] “Java怎么访问USB设备”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
网站标题:Java怎么访问USB设备
URL链接:http://myzitong.com/article/jdopjs.html