中英双语网站怎么做,WordPress圆形图片,长椿街网站建设,知乎网站怎么做推广2019独角兽企业重金招聘Python工程师标准 原文出处#xff1a;http://han21912.lofter.com/post/c3919_51401d 接上一篇#xff1a;Android USB Host与HID通讯 (一) 从上篇已经可以枚举到HID设备#xff0c;接下来看看寻找设备的接口和通信端点#xff0c;… 2019独角兽企业重金招聘Python工程师标准 原文出处http://han21912.lofter.com/post/c3919_51401d 接上一篇Android USB Host与HID通讯 (一) 从上篇已经可以枚举到HID设备接下来看看寻找设备的接口和通信端点即上篇中的findIntfAndEpt()方法 // find interface and assign end point private void findIntfAndEpt() { if (mUsbDevice null) { return; } // find the device interface for (int i 0; i mUsbDevice.getInterfaceCount();) { // 获取设备接口一般都是一个接口你可以打印getInterfaceCount()方法查看接 // 口的个数在这个接口上有两个端点OUT 和 IN UsbInterface intf mUsbDevice.getInterface(i); Log.d(TAG, i intf); if (intf.getInterfaceClass() 8 intf.getInterfaceSubclass() 6 // intf.getInterfaceProtocol() 80) { //HID设备的相关信息 mInterface intf; } break; } if (mInterface ! null) { UsbDeviceConnection connection null; // 判断是否有权限 if(mUsbManager.hasPermission(mUsbDevice)) { // 打开设备获取 UsbDeviceConnection 对象连接设备用于后面的通讯 connection mUsbManager.openDevice(mUsbDevice); if (connection null) { return; } if (connection.claimInterface(mInterface, true)) { mDeviceConnection connection; } else { connection.close(); } } else { Toast.makeText(context, 没有权限, Toast.LENGTH_SHORT).show(); } } } 上面主要用到UsbDevice.getInterface()方法和UsbManager.openDevice()方法在连接上设备后用UsbDeviceConnection 与 UsbInterface 进行端点设置和通讯如下 private void getEndpoint(UsbDeviceConnection connection, UsbInterface intf) { if (intf.getEndpoint(1) ! null) { epOut intf.getEndpoint(1); } if (intf.getEndpoint(0) ! null) { epIn intf.getEndpoint(0); } } 在此我们获得了通讯的OUT和IN端点也就是我们常说的输入输出查看api可知一般1为OUT端点0为IN端点接下来的任务就是通讯了而最终需要的就是connectionOUT/IN 端点在加上你要发送的指令打成命令包进行发送若命令发送成功会返回相应的数据信息当然不同的设备发送/接收命令模式不同同一设备不同的命令也需具体处理这就需要根据自己手上的设备而定好好研究研究自己的HID掌握它的命令发送/接收方式我的设备发送/接收模式为发送命令out发送预发送命令发送命令接收发送成功信息接收数据in发送预接收命令接收数据接收数据成功信息。 所以到这主要的工作就是设备的通讯模式和bulkTransfer()方法的参数配置如下是我的设备进行通讯的一个发送包从下面可以看出仅仅发送一个命令到HID设备其实际进行了三次命令的发送接收两OUT一IN总共调用了三次bulkTransfer()方法 // 发送包操作发送OUT 发送COM 接收IN private void sendPackage(byte[] command) { int ret -100; int len command.length; // 组织准备命令 byte[] sendOut Commands.OUT_S; sendOut[8] (byte) (len 0xff); sendOut[9] (byte) ((len 8) 0xff); sendOut[10] (byte) ((len 16) 0xff); sendOut[11] (byte) ((len 24) 0xff); // 1,发送准备命令 ret mDeviceConnection.bulkTransfer(epOut, sendOut, 31, 10000); if(ret ! 31) { return; } // 2,发送COM ret mDeviceConnection.bulkTransfer(epOut, command, len, 10000); if(ret ! len) { return; } // 3,接收发送成功信息 ret mDeviceConnection.bulkTransfer(epIn, Commands.IN, 13, 10000); if(ret ! 13) { return; } } 可以看出调用一次bulkTransfer()方法若通讯成功返回的应该是发送命令或返回信息的数据长度一开始我的bulkTransfer()方法总是返回-1一直处于通讯失败这时我们应该做的事情第一好好的组织bulkTransfer()方法中的参数endpoint为OUT还是INbuffer也就是我们要发送的命令对此不太理解的可以反复查阅 ① google开发指南穿一手的鞋 http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html ② 我之前发的一篇关于HID通讯方法的文章 Android USB Host 与 HID 之通讯方法 第二重点还是你自己的HID设备如何通讯通讯方式等等。 转载于:https://my.oschina.net/han21912/blog/133707