Android USB Host与HID通讯



http://www.oschina.net/code/snippet_811255_21652


主要是通过USB host 与HID设备进行通讯:枚举设备->找到设备的接口->连接设备->分配相应的端点->在IN端点进行读操作,在OUT端点进行写操作。详细可查看,
http://han21912.lofter.com/post/c3919_51401d


001/**
002 * USB HOST 连接 HID
003 * @author IVAN
004 *
005 */
006public class MainActivity extends Activity {
007    private static final String TAG = "USB_HOST";
008 
009    private UsbManager myUsbManager;
010    private UsbDevice myUsbDevice;
011    private UsbInterface myInterface;
012    private UsbDeviceConnection myDeviceConnection;
013 
014    private final int VendorID = 8457;
015    private final int ProductID = 30264;
016 
017    private TextView info;
018 
019    private UsbEndpoint epOut;
020    private UsbEndpoint epIn;
021 
022    @Override
023    public void onCreate(Bundle savedInstanceState) {
024        super.onCreate(savedInstanceState);
025        setContentView(R.layout.main);
026 
027        info = (TextView) findViewById(R.id.info);
028 
029        // 获取UsbManager
030        myUsbManager = (UsbManager) getSystemService(USB_SERVICE);
031 
032        enumerateDevice();
033 
034        findInterface();
035 
036        openDevice();
037 
038        assignEndpoint();
039 
040    }
041 
042    /**
043     * 分配端点,IN | OUT,即输入输出;此处我直接用1为OUT端点,0为IN,当然你也可以通过判断
044     */
045    private void assignEndpoint() {
046        if (myInterface.getEndpoint(1) != null) {
047            epOut = myInterface.getEndpoint(1);
048        }
049        if (myInterface.getEndpoint(0) != null) {
050            epIn = myInterface.getEndpoint(0);
051        }
052 
053        Log.d(TAG, getString(R.string.text));
054    }
055 
056    /**
057     * 打开设备
058     */
059    private void openDevice() {
060        if (myInterface != null) {
061            UsbDeviceConnection conn = null;
062            // 在open前判断是否有连接权限;对于连接权限可以静态分配,也可以动态分配权限,可以查阅相关资料
063            if (myUsbManager.hasPermission(myUsbDevice)) {
064                conn = myUsbManager.openDevice(myUsbDevice);
065            }
066 
067            if (conn == null) {
068                return;
069            }
070 
071            if (conn.claimInterface(myInterface, true)) {
072                myDeviceConnection = conn; // 到此你的android设备已经连上HID设备
073                Log.d(TAG, "打开设备成功");
074            } else {
075                conn.close();
076            }
077        }
078    }
079 
080    /**
081     * 找设备接口
082     */
083    private void findInterface() {
084        if (myUsbDevice != null) {
085            Log.d(TAG, "interfaceCounts : " + myUsbDevice.getInterfaceCount());
086            for (int i = 0; i < myUsbDevice.getInterfaceCount(); i++) {
087                UsbInterface intf = myUsbDevice.getInterface(i);
088                // 根据手上的设备做一些判断,其实这些信息都可以在枚举到设备时打印出来
089                if (intf.getInterfaceClass() == 8
090                        && intf.getInterfaceSubclass() == 6
091                        && intf.getInterfaceProtocol() == 80) {
092                    myInterface = intf;
093                    Log.d(TAG, "找到我的设备接口");
094                }
095                break;
096            }
097        }
098    }
099 
100    /**
101     * 枚举设备
102     */
103    private void enumerateDevice() {
104        if (myUsbManager == null)
105            return;
106 
107        HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList();
108        if (!deviceList.isEmpty()) { // deviceList不为空
109            StringBuffer sb = new StringBuffer();
110            for (UsbDevice device : deviceList.values()) {
111                sb.append(device.toString());
112                sb.append("\n");
113                info.setText(sb);
114                // 输出设备信息
115                Log.d(TAG, "DeviceInfo: " + device.getVendorId() + " , "
116                        + device.getProductId());
117 
118                // 枚举到设备
119                if (device.getVendorId() == VendorID
120                        && device.getProductId() == ProductID) {
121                    myUsbDevice = device;
122                    Log.d(TAG, "枚举设备成功");
123                }
124            }
125        }
126    }
127 
128    @Override
129    public boolean onCreateOptionsMenu(Menu menu) {
130        getMenuInflater().inflate(R.menu.main, menu);
131        return true;
132    }
133}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值