Want more features on Pastebin? Sign Up, it's FREE!
Guest

jusb application

By: a guest on Mar 22nd, 2012  |  syntax: Java  |  size: 3.21 KB  |  views: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package usb.core;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import usb.core.device;
  7.  
  8.  
  9. public class Main {
  10.     public static void main(String[]args) throws IOException
  11.     {
  12.       try
  13.         {
  14.             // Bootstrap by getting the USB Host from the HostFactory.
  15.             Host   host = HostFactory.getHost();
  16.  
  17.             // Obtain a list of the USB buses available on the Host.
  18.             Bus[]  bus  = host.getBusses();
  19.             int    total_bus = bus.length;
  20.              System.out.println(total_bus);
  21.             // Traverse through all the USB buses.
  22.             for (int i=0; i<total_bus; i++)
  23.             {
  24.                 // Access the root hub on the USB bus and obtain the
  25.                 // number of USB ports available on the root hub.
  26.                 Device root = bus[i].getRootHub();
  27.                 int total_port = root.getNumPorts();
  28.              
  29.                 // Traverse through all the USB ports available on the
  30.                 // root hub. It should be mentioned that the numbering
  31.                 // starts from 1, not 0.
  32.                 for (int j=1; j<=total_port; j++)
  33.                 {
  34.                     // Obtain the Device connected to the port.
  35.                     Device device = root.getChild(j);
  36.                     if (device != null)
  37.                     {
  38.                         // USB device available, do something here.
  39.                          // Obtain the current Configuration of the device and the number of
  40.        // Interfaces available under the current Configuration.
  41.        Configuration config = device.getConfiguration();
  42.        int total_interface = config.getNumInterfaces();
  43.  
  44.        // Traverse through the Interfaces
  45.        for (int k=0; k<total_interface; k++)
  46.        {
  47.            // Access the currently Interface and obtain the number of
  48.            // endpoints available on the Interface.
  49.            Interface itf = config.getInterface(k, 0);
  50.            int total_ep  = itf.getNumEndpoints();
  51.  
  52.            // Traverse through all the endpoints.
  53.            for (int l=0; l<total_ep; l++)
  54.            {
  55.                // Access the endpoint, and obtain its I/O type.
  56.                Endpoint ep = itf.getEndpoint(l);
  57.                String io_type = ep.getType();
  58.                boolean input  = ep.isInput();
  59.  
  60.                // If the endpoint is an input endpoint, obtain its
  61.                // InputStream and read in data.
  62.                if (input)
  63.                {
  64.                    InputStream in;
  65.                    in = ep.getInputStream();
  66.                    // Read in data here
  67.                    in.close();
  68.                }
  69.                // If the Endpoint is and output Endpoint, obtain its
  70.                // OutputStream and write out data.
  71.                else
  72.                {
  73.                    OutputStream out;
  74.                    out = ep.getOutputStream();
  75.                    // Write out data here.
  76.                    out.close();
  77.                }
  78.            }
  79.        }
  80.                     }
  81.                 }
  82.             }
  83.         } catch (Exception e)
  84.         {
  85.             System.out.println(e.getMessage());
  86.         }
  87.        
  88.     }
  89.            
  90. }
clone this paste RAW Paste Data