清单 2 展示了在应用程序成功地找到了 Device 的条件下,如何与 Interface 和 EndPoint 进行批量 I/O。 这个代码段也可以修改为执行控制或者中断 I/O。它对应于上述步骤 5。
清单 2. 用 jUSB API 执行批量 I/O
if (device != null) { // Obtain the current Configuration of the device and the number of // Interfaces available under the current Configuration. Configuration config = device.getConfiguration(); int total_interface = config.getNumInterfaces();
// Traverse through the Interfaces for (int k=0; k<total_interface; k++) { // Access the currently Interface and obtain the number of // endpoints available on the Interface. Interface itf = config.getInterface(k, 0); int total_ep = itf.getNumEndpoints();
// Traverse through all the endpoints. for (int l=0; l<total_ep; l++) { // Access the endpoint, and obtain its I/O type. Endpoint ep = itf.getEndpoint(l); String io_type = ep.getType(); boolean input = ep.isInput();
// If the endpoint is an input endpoint, obtain its // InputStream and read in data. if (input) { InputStream in; in = ep.getInputStream(); // Read in data here in.close(); } // If the Endpoint is and output Endpoint, obtain its // OutputStream and write out data. else { OutputStream out; out = ep.getOutputStream(); // Write out data here. out.close(); } } } } | jUSB 项目在 2000年 6月到 2001年 2月期间非常活跃。该 API 的最新的版本 0.4.4发表于 2001年 2月 14日。从那以后只提出了很少的改进,原因可能是 IBM 小组成功地成为了 Java 语言的候选扩展标准。不过,基于 jUSB 已经开发出一些第三方应用程序,包括 JPhoto 项目(这是一个用 jUSB 连接到数码照相机的应用程序)和 jSyncManager 项目(这是一个用 jUSB 与使用 Palm 操作系统的 PDA 同步的应用程序)。
|
|