废话:对于这个USB Host完全是不了解,昨天才开始写,看的很多资料,知识总结吧 (我的理解还是有很大缺陷,只是提供一些资料,希望和对这个USBhost理解好了大牛,有一些交流).
个人建议在你决定写的前,一定要你的设备Root,并且使用adb connect +ip 进行链接设备,因为一般设备就一个口,如果你连电脑,就无法看log信息,用命令链接,可以很好了,链接log,dug.
这是一些网上好的资料链接(下面的文章会有这些文章的内容,都是在学习):
1.http://blog.csdn.net/centralperk/article/details/9312401
2.http://www.20864.com/201323/329.html 这里对官当文档做的很好的翻译,解释的很清楚
3.http://wiki.eoeandroid.com/Host#.E4.BD.BF.E7.94.A8.E4.B8.80.E4.B8.AA.E6.84.8F.E5.9B.BE.E8.BF.87.E6.BB.A4.E5.99.A8 官方文档翻译
4.http://blog.csdn.net/centralperk/article/details/9312401 读取数据
注意:本文的步骤,可能需要你具备Root的权限,否则有些操作可能会无法完成。强烈建议你先root设备。
步骤一:你必须确定你的Android设备支持USB Host,具体支持与否请参考自己的说明书。确定了才有必要看本文章。
步骤二:确认Android是否已经开放了USB访问权限,这一步非常重要。操作是:进入系统,找到目录“/system/etc/permissions”,可以用ES或者RE文件管理器进行操作。查看该目录下,是否有一个文件"android.hardware.usb.host.xml",如果没有,则自己创建一个同名文件,内容如下:
<permissions>
<feature name="android.hardware.usb.host"/>
</permissions>
步骤三:其实呢,有了步骤三基本也就可以了,但是我自己也不是很确定,于是有了步骤四。继续检查目录“/system/etc/permissions”下,将其中的“handheld_core_hardware.xml (手机)或者 tablet_core_hardware.xml(平板)”拖出来,打开文件,看看<permissions>结点下面有没有下面这个结点:
<feature name="android.hardware.usb.host" />
这个大家用注意,这个权限一定要加上,有的不加也是可以,但是有的设备不加,就出现问题,也有网友的程序就出现这个问题.
这里有一个vendor-id
=
"2316"
product-id
=
"4096"
这个值,有时你的设备你怎么知道这些信息呢??第一个网址里面有很详细的解释,这里指做一些个人使用的技巧:
<span style="white-space:pre"> </span>if(mManager!=null){
System.out.println("我获取到USB管理器:我的管理器:"+mManager.toString());
}else{
System.out.println("我没有获取到USB管理器");
}
HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
System.out.println("我的设备数量:"+deviceList.size());
Iterator<UsbDevice> iterator = deviceList.values().iterator();
while (iterator.hasNext()) {
因为我就关连一个设备,我这里拿到Devices 但其实就一个设备,往下就是遍历很简单,你拿到Device里面的信息,你就可以取到
UsbDevice mDevice = iterator.next();
int deviceId = mDevice.getDeviceId();
int productId = mDevice.getProductId();
String deviceName = mDevice.getDeviceName();
这里有一个问题,大家可以理解一下,就是对UsbManager这个对象的获取,个人建议使用:
<span style="white-space:pre"> </span>UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
private UsbDevice usbDevice;
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("USB Host", action);
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
//允许权限申请
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(usbDevice != null){
int deviceId = usbDevice.getDeviceId();
System.out.println("设备ID:"+deviceId);
int count = usbDevice.getInterfaceCount();
System.out.println("设备连接点数:"+count);
/**
* UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread
*/
if(count >0){
<span style="white-space:pre"> </span>这个地方,因为是在弄原理和试试,没有做好,官方文档说,这个有同步和异步两种方法,可定应该用异步,如果用异步又要使用一个类,USBRequest,官方只是一个说明,我这个是同步,log给我提示过,在主线程做的很多的事,但是程序没有挂掉,值得大家研究.
</span><span style="color:#ff0000;">UsbInterface intf = usbDevice.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);</span><span style="color:#0000ff;">
byte[] buffer = new byte[1024];
if(usbManager.hasPermission(usbDevice)){
UsbDeviceConnection openDevice = usbManager.openDevice(usbDevice);
openDevice.claimInterface(intf, true);</span>
<span style="color:#0000ff;">这里推荐大家看一下这篇文章:http://blog.sina.com.cn/s/blog_7f66fbac01015yo2.html
<span style="color:#0000ff;">但我对这个地方也有很多不能理解了地方,大家一起学习一下</span>
<span style="color:#0000ff;">我但是读取到数据了,但是很乱,自己也在做整理
// </span><span style="color:#ff0000;">openDevice.bulkTransfer(endpoint, buffer, buffer.length, 5000);
for (byte b : buffer) {
System.out.println("我的值:"+b);
}</span>
bulkTransfer 这个类还好理解一些,但是controlTransfer绝对让人吐血,每个参数你都没法查:
http://blog.sina.com.cn/s/blog_7f66fbac01015yo2.html说明的一些,但是对我不是很试用,但是对大家理解很好