参照了http://blog.csdn.net/qweadf1/article/details/41646439链接中的文章,实现了Android和HID设备的收发数据。
直接上manifest文件。主要是usb.host权限,这边没有配置xml文件指定USB设备。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.com.hid" > <uses-feature android:name="android.hardware.usb.host"/> <uses-permission android:name="android.hardware.usb.host" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/> </intent-filter> </activity> </application> </manifest>
接下来主要代码
package com.com.hid; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbEndpoint; import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private static final String TAG = "USB_HOST"; private UsbManager myUsbManager; private UsbDevice myUsbDevice; private UsbInterface myInterface; private UsbDeviceConnection myDeviceConnection; private final int VendorID = 1042; private final int ProductID = 1; private TextView info; private UsbEndpoint epOut; private UsbEndpoint epIn; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; byte[] mybuffer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mybuffer = new byte[]{(byte)0x02, (byte)0x00, (byte)0x06, (byte)0x00, (byte)0x05, (byte)0xcc, (byte)0xcc, (byte)0xcc, (byte)0xcc, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00}; Log.i("mybuffer", bytesToHexString(mybuffer)); info = (TextView) findViewById(R.id.info); myUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); enumerateDevice(); } //枚举设备 private void enumerateDevice() { if (myUsbManager == null) return; HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList(); if (!deviceList.isEmpty()) { // deviceList不为空 StringBuffer sb = new StringBuffer(); for (UsbDevice device : deviceList.values()) { sb.append(device.toString()); sb.append("\n"); // info.setText(sb); // 输出设备信息 Log.d(TAG, "DeviceInfo: " + device.getVendorId() + " , " + device.getProductId()); // 枚举到设备 if (device.getVendorId() == VendorID && device.getProductId() == ProductID) { myUsbDevice = device; Toast.makeText(this, "枚举设备成功", Toast.LENGTH_SHORT).show(); Log.d(TAG, "枚举设备成功"); findInterface(); } else { // Toast.makeText(this, "Not Found VID and PID", Toast.LENGTH_SHORT).show(); Log.d(TAG, "Not Found VID and PID"); } } } else { new AlertDialog.Builder(this).setTitle("未枚举到设备!") .setMessage("请先连接设备") .setCancelable(false) .setNeutralButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //finish(); } }).show(); } } // 找到接口 private void findInterface() { if (myUsbDevice == null) { return; } Log.d(TAG, "interfaceCounts : " + myUsbDevice.getInterfaceCount()); for (int i = 0; i < myUsbDevice.getInterfaceCount(); i++) { UsbInterface intf = myUsbDevice.getInterface(i); // 根据手上的设备做一些判断,其实这些信息都可以在枚举到设备时打印出来 if (intf.getInterfaceClass() == 3 && intf.getInterfaceSubclass() == 0 && intf.getInterfaceProtocol() == 0) { myInterface = intf; Log.d(TAG, "找到我的设备接口"); openDevice(); } break; } } //获取权限,打开设备 private void openDevice() { if (myInterface != null) { UsbDeviceConnection conn = null; // 在open前判断是否有连接权限;对于连接权限可以静态分配,也可以动态分配权限,可以查阅相关资料 PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); if (!myUsbManager.hasPermission(myUsbDevice)) { myUsbManager.requestPermission(myUsbDevice, pi); } if (myUsbManager.hasPermission(myUsbDevice)) { conn = myUsbManager.openDevice(myUsbDevice); } else { Toast.makeText(this, "未获得权限", Toast.LENGTH_SHORT).show(); } if (conn == null) { return; } if (conn.claimInterface(myInterface, true)) { myDeviceConnection = conn; // 到此你的android设备已经连上HID设备 Log.d(TAG, "打开设备成功"); Toast.makeText(this, "打开设备成功", Toast.LENGTH_SHORT).show(); assignEndpoint(); } else { conn.close(); } } } //拿到端点,用bulkTransfer进行数据发收 private void assignEndpoint() { if (myInterface.getEndpoint(1) != null) { epOut = myInterface.getEndpoint(1); } if (myInterface.getEndpoint(0) != null) { epIn = myInterface.getEndpoint(0); } info.setText(myUsbDevice.getDeviceName() + "\nInterfaceCount:" + myUsbDevice.getInterfaceCount() + "\nEndpointCount:" + myInterface.getEndpointCount()); Log.d(TAG, "assignEndpoint"); int re = myDeviceConnection.bulkTransfer(epOut, mybuffer, mybuffer.length, 3000); byte[] reByte = new byte[64]; int re2 = myDeviceConnection.bulkTransfer(epIn, reByte, reByte.length, 3000); Log.i("reByte", "re"+re+"re2"+ re2 + "\n" + bytesToHexString(reByte)); Toast.makeText(this, bytesToHexString(reByte), Toast.LENGTH_LONG).show(); } public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
发送和接受的数据是64位的byte[]
断开设备后用releaseInterface()
和close()
关闭UsbInterface
和UsbDeviceConnection
相关推荐
可以接收单片机发送的usb hid信息,并显示在android手机上
安卓手机用usb hid与单片机进行通信
实现USB HID设备读写代码,通过C#语言来实现读取usb接口设备的数据读写
此demo实现两个android设备通过usb线进行相互通信,用于host和devices之间通信
Usb Host ADK是指不采用USB驱动,直接用java与设备编程的 这样让Android 可以无需修改内核和驱动情况下方便与各种USB外设通讯
android usb转串口数据通信示例(源代码) android usb转串口数据通信示例。物联网开发中也会经常用到usb转串口,对android手机进行通信。一般都会用otc线进行转换。我在GitHub下来一份代码,亲测可用。并进行了修改...
3.1版本之后支持的usb host API。可以通过usb接口让安卓设备作为主设备来接收来自usb的数据,比如平板上插个单片机,就可以接收单片机的数据了。该程序封装了多个包,实现了该功能。
android下USB Host开发的一些注意事项和开发步骤
主要研究usb host的数据传送问题,里面有对 相关usb传送关系进行解析,并对 关键函数 : //first field ox21 is bin 00100001 which splits into 0 01 00001 for direction(1bit)/type(2b)/recipient(5b) //To ...
struct usb_bus *busses, *bus; // usb_dev_handle *handle = NULL; //这个需定义为全局变量,在读线程中也许使用 usb_init(); usb_find_busses(); usb_find_devices(); busses = usb_get_busses(); for(bus ...
用于检测Android平板是否支持USB HOST API
该文章主要是讲解了USB协议的基础知识,包括域、包、事务、传输。USB HOST MSC类的通信过程,以及FAT32的基本知识。
Android USB host简介,中文文档
how to enable android usb host api
本源码是stm32f103官方提供的USB的HID和MSC复合设备库文件源码
本资料属原创,从USB驱动CDC设备的原理,框架,具体协议,分析波形,以及实验的平台,实验条件等等做出详细的描述。将近1个月的梳理学习整理而出,对学习人员很有帮助。
stm32 USB HID+CDC 鼠标键盘串口 组合设备配置解析
本人最近在Android的PAD上开发USB Host数据传输。(见附件) 对CH340,已经能够正常读写;但对CH341只是可以正常写出,读入总是有问题:只有当USB缓存满32字节时,才可以读入数据(数据是对的)。 不知道有无同行在CH...
此demo主要是介绍UsbHost的通信,可直接替换vendorId和productId使用
启用android usb host功能的xml文件。 需要将这个文件加到设备的/system/etc/permissions/目录下,才能开启android设备的usb host权限。