Java访问USB设备

编者注

由于需要对某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();

        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]

附录

USB 设备描述符

USB 设备专题讨论(Windows 应用商店应用)
USB Class Codes
USB的工作原理
USB驱动之 端点 endpoint

转载于:https://my.oschina.net/hava/blog/1577447

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值