java使用usb4java读取usb连接的设备

1、最近在做一个java与usb通信的项目,在我原来的理解中,java是不能读取电脑底层设备信息的,所有就让人用C写一个中间程序来传递数据。但是最近在查询资料的时候,看到usb4java是写好的jar包,可以用java读取硬件设备,虽然最终不知道是什么原因一直无法读取我公司的硬件设备,但是可以查看到鼠标、键盘、手机是否连接usb。做一个参考吧。

2、首先需要查看usb连接的idVendor和idProduct;java需要根据这个来判断是哪个usb

(1)我的电脑右键--管理--设备管理器

(2)点击要查看的设备,点击详细信息,选择硬件ID;下图的idVendor=12D1;idProduct=1082的16进制数

3、代码:

(1)引入的包:

          <dependency>
            <groupId>org.usb4java</groupId>
            <artifactId>usb4java-javax</artifactId>
            <version>1.2.0</version>

          </dependency>

(2)需要在resource下加入允许使用jar包的文件javax.usb.properties;里面的内容为:javax.usb.services = org.usb4java.javax.Services


(3)class代码如下:

package com.jda.iwpss.test;

import java.util.List;

import javax.usb.UsbConfiguration;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbEndpoint;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbInterfacePolicy;
import javax.usb.UsbPipe;

public class UsbTest {
    private static short idVendor = (short)0x12D1;
    private static short idProduct = (short)0x1082;
    
    public static void main(String[] args) {
        try {
            UsbPipe sendUsbPipe = new UsbTest().useUsb();
            
            if (sendUsbPipe != null) {
                byte[] buff = new byte[64];
                for (int i = 0; i < 9; i++) {
                    buff[i] = (byte)i;
                    sendMassge(sendUsbPipe, buff);
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public UsbPipe useUsb() throws Exception{
        UsbInterface iface = linkDevice();
        if (iface == null) {
            return null;
        }
        UsbEndpoint receivedUsbEndpoint,sendUsbEndpoint;
        
        sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(0);
        if (!sendUsbEndpoint.getUsbEndpointDescriptor().toString().contains("OUT")) {
            receivedUsbEndpoint = sendUsbEndpoint;
            sendUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
        } else {
            receivedUsbEndpoint = (UsbEndpoint)iface.getUsbEndpoints().get(1);
        }
        
        //发送:
        UsbPipe sendUsbPipe =  sendUsbEndpoint.getUsbPipe();
        sendUsbPipe.open();
        
        //接收
        final UsbPipe receivedUsbPipe =  receivedUsbEndpoint.getUsbPipe();
        receivedUsbPipe.open();
        
        new Thread(new Runnable() {
            public void run() {
                try {
                    receivedMassge(receivedUsbPipe);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return sendUsbPipe;
    }
    
    public UsbInterface linkDevice() throws Exception{

        UsbDevice device = null;
        if (device == null) {
             device = findDevice(UsbHostManager.getUsbServices()
                        .getRootUsbHub());
        }
        if (device == null) {
            System.out.println("设备未找到!");
            return null;
        }
        UsbConfiguration configuration = device.getActiveUsbConfiguration();
        UsbInterface iface = null;
        if (configuration.getUsbInterfaces().size() > 0) {
            iface = configuration.getUsbInterface((byte) 1);
        } else {
            return null;
        }
        iface.claim(new UsbInterfacePolicy()
        {            
            @Override
            public boolean forceClaim(UsbInterface usbInterface)
            {
                return true;
            }
        });
        return iface;
    }
    
    public void receivedMassge(UsbPipe usbPipe) throws Exception{
        byte[] b = new byte[64];
        int length = 0;
        while (true) {
            length = usbPipe.syncSubmit(b);//阻塞
            System.out.println("接收长度:" + length);
            for (int i = 0; i < length; i++) {
                System.out.print(Byte.toUnsignedInt(b[i])+" ");
            }
        }
    }
    
    public static void sendMassge(UsbPipe usbPipe,byte[] buff)  throws Exception{
        usbPipe.syncSubmit(buff);//阻塞
        //usbPipe.asyncSubmit(buff);//非阻塞
    }
    
    public UsbDevice findDevice(UsbHub hub)
    {
        UsbDevice device = null;
        List list = (List) hub.getAttachedUsbDevices();
        for (int i = 0;i<list.size();i++)
        {
            device = (UsbDevice)list.get(i);
            UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
            System.out.println(i+"___"+desc.idVendor()+"___"+desc.idProduct());
            if (desc.idVendor() == idVendor && desc.idProduct() == idProduct) {return device;}
            if (device.isUsbHub())
            {
                device = findDevice((UsbHub) device);
                if (device != null) return device;
            }
        }
        return null;
    }
}


猜你喜欢

转载自blog.csdn.net/f552126367/article/details/80951063