| 接口 | 说明 |
| Bus | 将一组 USB 设备连接到 Host 上 |
| Host | 表示具有一个或者多个 Bus 的 USB 控制器 |
| 类 | 说明 |
| Configuration | 提供对设备所支持的 USB 配置的访问,以及对与该配置关联的接口的访问 |
| Descriptor | 具有 USB 类型的描述符的实体的基类 |
| Device | 提供对 USB 设备的访问 |
| DeviceDescriptor | 提供对 USB 设备描述符的访问 |
| EndPoint | 提供对 USB 端点描述符的访问、在给定设备配置中构造设备数据输入或者输出 |
| HostFactory | 包含 bootstrapping 方法 |
| Hub | 提供对 USB hub 描述符以及一些 hub 操作的访问 |
| Interface | 描述一组端点,并与一个特定设备配置相关联 |
| PortIdentifier | 为 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. for (int j=1; j<=total_port; j++) { // Obtain the Device connected to the port. Device device = root.getChild(j); if (device != null) { // USB device available, do something here. } } } } catch (Exception e) { System.out.println(e.getMessage()); } } |
| 接口 | 说明 |
| UsbConfiguration | 表示 USB 设备的配置 |
| UsbConfigurationDescriptor | USB 配置描述符的接口 |
| UsbDevice | USB 设备的接口 |
| UsbDeviceDescriptor | USB 设备描述符的接口 |
| UsbEndpoint | USB 端点的接口 |
| UsbEndpointDescriptor | USB 端点描述符的接口 |
| UsbHub | USB hub 的接口 |
| UsbInterface | USB 接口的接口 |
| UsbInterfaceDescriptor | USB 接口描述符的接口 |
| UsbPipe | USB 管道的接口 |
| UsbPort | USB 端口的接口 |
| UsbServices | javax.usb 实现的接口 |
| 类 | 说明 |
| UsbHostManager | javax.usb 的入口点 |
| import javax.usb.*; import java.util.List; public class TraverseUSB { public static void main(String argv[]) { try { // Access the system USB services, and access to the root // hub. Then traverse through the root hub. UsbServices services = UsbHostManager.getUsbServices(); UsbHub rootHub = services.getRootUsbHub(); traverse(rootHub); } catch (Exception e) {} } public static void traverse(UsbDevice device) { if (device.isUsbHub()) { // This is a USB Hub, traverse through the hub. List attachedDevices = ((UsbHub) device).getAttachedUsbDevices(); for (int i=0; i<attachedDevices.size(); i++) { traverse((UsbDevice) attachedDevices.get(i)); } } else { // This is a USB function, not a hub. // Do something. } } } |
| public static void testIO(UsbDevice device) { try { // Access to the active configuration of the USB device, obtain // all the interfaces available in that configuration. UsbConfiguration config = device.getActiveUsbConfiguration(); List totalInterfaces = config.getUsbInterfaces(); // Traverse through all the interfaces, and access the endpoints // available to that interface for I/O. for (int i=0; i<totalInterfaces.size(); i++) { UsbInterface interf = (UsbInterface) totalInterfaces.get(i); interf.claim(); List totalEndpoints = interf.getUsbEndpoints(); for (int j=0; j<totalEndpoints.size(); j++) { // Access the particular endpoint, determine the direction // of its data flow, and type of data transfer, and open the // data pipe for I/O. UsbEndpoint ep = (UsbEndpoint) totalEndpoints.get(i); int direction = ep.getDirection(); int type = ep.getType(); UsbPipe pipe = ep.getUsbPipe(); pipe.open(); // Perform I/O through the USB pipe here. pipe.close(); } interf.release(); } } catch (Exception e) {} } |