http://www.oschina.net/code/snippet_811255_21652
主要是通过USB host 与HID设备进行通讯:枚举设备->找到设备的接口->连接设备->分配相应的端点->在IN端点进行读操作,在OUT端点进行写操作。详细可查看,
http://han21912.lofter.com/post/c3919_51401d
006 | public class MainActivity extends Activity { |
007 | private static final String TAG = "USB_HOST" ; |
009 | private UsbManager myUsbManager; |
010 | private UsbDevice myUsbDevice; |
011 | private UsbInterface myInterface; |
012 | private UsbDeviceConnection myDeviceConnection; |
014 | private final int VendorID = 8457 ; |
015 | private final int ProductID = 30264 ; |
017 | private TextView info; |
019 | private UsbEndpoint epOut; |
020 | private UsbEndpoint epIn; |
023 | public void onCreate(Bundle savedInstanceState) { |
024 | super .onCreate(savedInstanceState); |
025 | setContentView(R.layout.main); |
027 | info = (TextView) findViewById(R.id.info); |
030 | myUsbManager = (UsbManager) getSystemService(USB_SERVICE); |
043 | * 分配端点,IN | OUT,即输入输出;此处我直接用1为OUT端点,0为IN,当然你也可以通过判断 |
045 | private void assignEndpoint() { |
046 | if (myInterface.getEndpoint( 1 ) != null ) { |
047 | epOut = myInterface.getEndpoint( 1 ); |
049 | if (myInterface.getEndpoint( 0 ) != null ) { |
050 | epIn = myInterface.getEndpoint( 0 ); |
053 | Log.d(TAG, getString(R.string.text)); |
059 | private void openDevice() { |
060 | if (myInterface != null ) { |
061 | UsbDeviceConnection conn = null ; |
063 | if (myUsbManager.hasPermission(myUsbDevice)) { |
064 | conn = myUsbManager.openDevice(myUsbDevice); |
071 | if (conn.claimInterface(myInterface, true )) { |
072 | myDeviceConnection = conn; |
073 | Log.d(TAG, "打开设备成功" ); |
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); |
089 | if (intf.getInterfaceClass() == 8 |
090 | && intf.getInterfaceSubclass() == 6 |
091 | && intf.getInterfaceProtocol() == 80 ) { |
093 | Log.d(TAG, "找到我的设备接口" ); |
103 | private void enumerateDevice() { |
104 | if (myUsbManager == null ) |
107 | HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList(); |
108 | if (!deviceList.isEmpty()) { |
109 | StringBuffer sb = new StringBuffer(); |
110 | for (UsbDevice device : deviceList.values()) { |
111 | sb.append(device.toString()); |
115 | Log.d(TAG, "DeviceInfo: " + device.getVendorId() + " , " |
116 | + device.getProductId()); |
119 | if (device.getVendorId() == VendorID |
120 | && device.getProductId() == ProductID) { |
121 | myUsbDevice = device; |
122 | Log.d(TAG, "枚举设备成功" ); |
129 | public boolean onCreateOptionsMenu(Menu menu) { |
130 | getMenuInflater().inflate(R.menu.main, menu); |