本篇內容介紹了“Java怎么訪問USB設備”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)嘉黎免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。
由于需要對某usb設備獲取的數(shù)值進行轉發(fā)和廣播,則選擇最熟悉的java作為切入語言解決。歷經(jīng)辛苦,測試成功。
通過查看Java的標準協(xié)會在制定標準的時候是具有JSR-80的制定的。API為javax.usb。但是通過當前(JDK 8作為默認),無論是J2ME、J2SE、J2EE都沒有對JSR進行實現(xiàn)。
JSR80
javax.usb API
javax-usb-example
通過閱讀,能夠得知沒有Windows實現(xiàn),或者Windows實現(xiàn)有問題,則需要其他方式。我們通過usb4java庫的實現(xiàn),在通過usb4java-javax的連接實現(xiàn)javax.usb
usb4java 官網(wǎng)
usb4java - quick start guide
通過快速開始了解到usb4java的兩種api,底層API是針對libusb的,高層API是針對javax.usb
High-level (javax-usb) API
注意:必須添加javax.usb.properties
開發(fā)環(huán)境為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設備”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質量的實用文章!
分享標題:Java怎么訪問USB設備
鏈接URL:http://weahome.cn/article/jdopjs.html