java获取usb设备的相关信息

本文介绍了如何在Java中获取USB设备信息,重点提及了需要创建javax.usb.properties文件并放置于resource目录下,引用usb4java库,并且在遇到USB错误时,需要安装UsbDk进行设备访问。UsbDk是一个允许用户层程序直接访问USB设备的开发套件,提供了API供USB设备操作。
摘要由CSDN通过智能技术生成
我从上上个礼拜开始接触,公司需要,所以开始在网上搜索相关资料,但是都没有找到合适的范例,但万幸终于测试出合适的代码。

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.usb.*;

public class UsbTest {
   
    //下边两个参数为系统中usb设备的VID和PID 需要自行配置
    private static short idVendor = ...;
    private static short idProduct = ...;

    public static void main(String[] args) {
   
        try {
   
            UsbPipe sendUsbPipe = new UsbTest().useUsb();
            String string = "..." ;//传输内容
            byte[] buff =string.getBytes();
            sendMassge(sendUsbPipe, buff);
        } catch (Exception e1) {
   
            e1.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值