日期:2014-05-20  浏览次数:20617 次

求用jsr 80写的java程序访问usb设备
怎么让下面的程序把usb设备打印出来?或者你给一段别的程序也行。
import javax.usb.*;
import java.util.List;
public class TraverseUSB
{
        public static void main(String argv[])
        {
          try
          {
              UsbServices services = UsbHostManager.getUsbServices();
              UsbHub rootHub = services.getRootUsbHub();
              traverse(rootHub);
          } catch (Exception e) {}
        }
        public static void traverse(UsbDevice device)
        {
          if (device.isUsbHub())
          {   
             
             List attachedDevices = 
                 ((UsbHub) device).getAttachedUsbDevices();
             for (int i=0; i<attachedDevices.size(); i++)
             {
               traverse((UsbDevice) attachedDevices.get(i));
                
             }
          }
          else
          {
             
          }
        }
}
java usb

------解决方案--------------------

import usb.core.*;
 
public class ListUSB
{
    public static void main(String[] args)
    {
        try
        {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
 
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            int    total_bus = bus.length;
 
            // Traverse through all the USB buses.
            for (int i=0; i<total_bus; i++)
            {
                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();
 
                // Traverse through all the USB ports available on the 
                // root hub. It should be mentioned that the numbering 
                // starts from 1, not 0.