您好,登錄后才能下訂單哦!
本篇內容介紹了“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設備”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。