usb-host一步一步学(二)安卓在usb-host模式下列出当前连接的usb设备
之前写了一个简单的例子usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备,下面的这个例子是获取各种usb设备、usb接口以及usb连接点(endpoint)

正如上一章节提到的那样,为了指定应用程序以usb-host模式运行,在AndroidManifest.xml文件中需要添加<uses-feature
android:name="android.hardware.usb.host">并且修改安卓的minSDKVersion="12"
MainActivity.java
package com.example.androidusbhost; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator; import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context; public class MainActivity extends Activity { Button btnCheck;
TextView textInfo;
TextView textInfoInterface;
TextView textEndPoint; Spinner spDeviceName;
ArrayList<String> listDeviceName;
ArrayList<UsbDevice> listUsbDevice;
ArrayAdapter<String> adapterDevice; Spinner spInterface;
ArrayList<String> listInterface;
ArrayList<UsbInterface> listUsbInterface;
ArrayAdapter<String> adapterInterface; Spinner spEndPoint;
ArrayList<String> listEndPoint;
ArrayList<UsbEndpoint> listUsbEndpoint;
ArrayAdapter<String> adapterEndpoint; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); spDeviceName = (Spinner)findViewById(R.id.spinnerdevicename);
spInterface = (Spinner)findViewById(R.id.spinnerinterface);
spEndPoint = (Spinner)findViewById(R.id.spinnerendpoint);
textInfo = (TextView) findViewById(R.id.info);
textInfoInterface = (TextView)findViewById(R.id.infointerface);
textEndPoint = (TextView)findViewById(R.id.infoendpoint); btnCheck = (Button) findViewById(R.id.check);
btnCheck.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
checkDeviceInfo();
}
});
} private void checkDeviceInfo() { listDeviceName = new ArrayList<String>();
listUsbDevice = new ArrayList<UsbDevice>(); UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
listDeviceName.add(device.getDeviceName());
listUsbDevice.add(device); } textInfo.setText("");
textInfoInterface.setText("");
textEndPoint.setText(""); adapterDevice = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listDeviceName);
adapterDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDeviceName.setAdapter(adapterDevice);
spDeviceName.setOnItemSelectedListener(deviceOnItemSelectedListener);
} OnItemSelectedListener deviceOnItemSelectedListener =
new OnItemSelectedListener(){ @Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
UsbDevice device = listUsbDevice.get(position); String i = device.toString() + "\n" +
"DeviceID: " + device.getDeviceId() + "\n" +
"DeviceName: " + device.getDeviceName() + "\n" +
"DeviceClass: " + device.getDeviceClass() + " - "
+ translateDeviceClass(device.getDeviceClass()) + "\n" +
"DeviceSubClass: " + device.getDeviceSubclass() + "\n" +
"VendorID: " + device.getVendorId() + "\n" +
"ProductID: " + device.getProductId() + "\n" +
"InterfaceCount: " + device.getInterfaceCount();
textInfo.setText(i); checkUsbDevicve(device);
} @Override
public void onNothingSelected(AdapterView<?> parent) {} }; private void checkUsbDevicve(UsbDevice d) {
listInterface = new ArrayList<String>();
listUsbInterface = new ArrayList<UsbInterface>(); for(int i=0; i<d.getInterfaceCount(); i++){
UsbInterface usbif = d.getInterface(i);
listInterface.add(usbif.toString());
listUsbInterface.add(usbif);
} adapterInterface = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listInterface);
adapterDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spInterface.setAdapter(adapterInterface);
spInterface.setOnItemSelectedListener(interfaceOnItemSelectedListener);
} OnItemSelectedListener interfaceOnItemSelectedListener =
new OnItemSelectedListener(){ @Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) { UsbInterface selectedUsbIf = listUsbInterface.get(position); String sUsbIf = "\n" + selectedUsbIf.toString() + "\n"
+ "Id: " + selectedUsbIf.getId() + "\n"
+ "InterfaceClass: " + selectedUsbIf.getInterfaceClass() + "\n"
+ "InterfaceProtocol: " + selectedUsbIf.getInterfaceProtocol() + "\n"
+ "InterfaceSubclass: " + selectedUsbIf.getInterfaceSubclass() + "\n"
+ "EndpointCount: " + selectedUsbIf.getEndpointCount(); textInfoInterface.setText(sUsbIf);
checkUsbInterface(selectedUsbIf);
} @Override
public void onNothingSelected(AdapterView<?> parent) {} }; private void checkUsbInterface(UsbInterface uif) {
listEndPoint = new ArrayList<String>();
listUsbEndpoint = new ArrayList<UsbEndpoint>(); for(int i=0; i<uif.getEndpointCount(); i++){
UsbEndpoint usbEndpoint = uif.getEndpoint(i);
listEndPoint.add(usbEndpoint.toString());
listUsbEndpoint.add(usbEndpoint);
} adapterEndpoint = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listEndPoint);
adapterEndpoint.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEndPoint.setAdapter(adapterEndpoint);
spEndPoint.setOnItemSelectedListener(endpointOnItemSelectedListener);
} OnItemSelectedListener endpointOnItemSelectedListener =
new OnItemSelectedListener(){ @Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) { UsbEndpoint selectedEndpoint = listUsbEndpoint.get(position); String sEndpoint = "\n" + selectedEndpoint.toString() + "\n"
+ translateEndpointType(selectedEndpoint.getType()); textEndPoint.setText(sEndpoint);
} @Override
public void onNothingSelected(AdapterView<?> parent) {} }; private String translateEndpointType(int type){
switch(type){
case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
return "USB_ENDPOINT_XFER_CONTROL (endpoint zero)";
case UsbConstants.USB_ENDPOINT_XFER_ISOC:
return "USB_ENDPOINT_XFER_ISOC (isochronous endpoint)";
case UsbConstants.USB_ENDPOINT_XFER_BULK :
return "USB_ENDPOINT_XFER_BULK (bulk endpoint)";
case UsbConstants.USB_ENDPOINT_XFER_INT:
return "USB_ENDPOINT_XFER_INT (interrupt endpoint)";
default:
return "unknown";
}
} private String translateDeviceClass(int deviceClass){
switch(deviceClass){
case UsbConstants.USB_CLASS_APP_SPEC:
return "Application specific USB class";
case UsbConstants.USB_CLASS_AUDIO:
return "USB class for audio devices";
case UsbConstants.USB_CLASS_CDC_DATA:
return "USB class for CDC devices (communications device class)";
case UsbConstants.USB_CLASS_COMM:
return "USB class for communication devices";
case UsbConstants.USB_CLASS_CONTENT_SEC:
return "USB class for content security devices";
case UsbConstants.USB_CLASS_CSCID:
return "USB class for content smart card devices";
case UsbConstants.USB_CLASS_HID:
return "USB class for human interface devices (for example, mice and keyboards)";
case UsbConstants.USB_CLASS_HUB:
return "USB class for USB hubs";
case UsbConstants.USB_CLASS_MASS_STORAGE:
return "USB class for mass storage devices";
case UsbConstants.USB_CLASS_MISC:
return "USB class for wireless miscellaneous devices";
case UsbConstants.USB_CLASS_PER_INTERFACE:
return "USB class indicating that the class is determined on a per-interface basis";
case UsbConstants.USB_CLASS_PHYSICA:
return "USB class for physical devices";
case UsbConstants.USB_CLASS_PRINTER:
return "USB class for printers";
case UsbConstants.USB_CLASS_STILL_IMAGE:
return "USB class for still image devices (digital cameras)";
case UsbConstants.USB_CLASS_VENDOR_SPEC:
return "Vendor specific USB class";
case UsbConstants.USB_CLASS_VIDEO:
return "USB class for video devices";
case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER:
return "USB class for wireless controller devices";
default: return "Unknown USB class!"; }
} }
布局文件activity_main.xml文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" /> <Button
android:id="@+id/check"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check USB devices" /> <Spinner
android:id="@+id/spinnerdevicename"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinnerinterface"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Spinner
android:id="@+id/spinnerendpoint"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/infointerface"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic" /> <TextView
android:id="@+id/infoendpoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
usb-host一步一步学(二)安卓在usb-host模式下列出当前连接的usb设备的更多相关文章
- usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备
在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...
- Service系统服务(二):补充应用技巧、软连接与硬连接、man手册、zip备份、vim效率操作、自定义yum软件仓库、发布及测试yum仓库、编译安装软件包
一.补充应用技巧 目标: 本例要求掌握在运维中比较常用的一些扩展命令技巧的使用,完成下列小技巧操作: 1> 采用数值形式将目录/root的权限调整为 rwx------ 2> 将记录的 ...
- 深圳市共创力咨询CEO杨学明的最新演讲:互联网模式下的企业创新管理
2018年11月14日, 深圳市共创力咨询董事长.深圳市汇成研发管理咨询公司董事长杨学明先生受邀参加由深圳图书馆主办,深圳手讯视频承办的“倾听行业之声”2018第二届世界CED智慧大会,此次分享的主题 ...
- 一步一步跟我学DeviceOne开发 - 仿微信应用(一,二,三)
这是一个系列的文档,长期目标是利用DeviceOne开发一些目前使用广泛的优质手机应用,我们会最大化的实现这些应用的每一个功能和细节,不只停留在简单的UI模仿和Demo阶段,而是一个基本可以使用的实际 ...
- 一步一步学Vue(十二)
为了提升代码的逼格,之后代码改为Vue文件组件,之前代码虽然读起来容易理解,而且适合在小的项目中使用,但是有如下缺点: 全局定义(Global definitions) 强制要求每个 componen ...
- 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计
本帖最后由 xinxincaijq 于 2013-1-9 10:27 编辑 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转自博客:http:// ...
- 【DG】[三思笔记]一步一步学DataGuard
[DG][三思笔记]一步一步学DataGuard 它有无数个名字,有人叫它dg,有人叫它数据卫士,有人叫它data guard,在oracle的各项特性中它有着举足轻理的地位,它就是(掌声)..... ...
- 【Linux】一步一步学Linux——Linux发展史(01)
目录 00. 目录 01. Linux概述 02. Linux简史 03. Linux主要特性 04. Linux之父 05. Linux相关术语 06. Linux其它 07. Linux应用领域 ...
- xmppmini 项目详解:一步一步从原理跟我学实用 xmpp 技术开发 2.登录的实现
第二章登录的实现 金庸<倚天屠龙记> 张三丰缓缓摇头,说道:“少林派累积千年,方得达成这等绝技,决非一蹴而至,就算是绝顶聪明之人,也无法自创.”他顿了一顿,又道:“我当年在少林寺中住过,只 ...
随机推荐
- R: vector 向量的创建、操作等。
################################################### 问题:创建.操作向量 18.4.27 怎么创建向量 vector,,及其相关操作 ??? 解 ...
- Linux系统挂载FAT32的U盘
1:将U盘插入USB接口,检查是否插好 2:用fdisk命令检查分区和USB设备信息 [root@yhwang ~]# fdisk -l Disk /dev/sda: 1000.2 GB, 10002 ...
- Code Page Identifiers - Copy from Microsoft
Code Page Identifiers 78 out of 94 rated this helpful - Rate this topic The following table define ...
- 第三周作业-课本&&视频学习
<网络攻防技术与实践>第三周作业 Part I 寻找自己留在互联网上的足迹并消除隐私 1.1 google/baidu搜索自己的qq号 搜索结果如图,搜到的有用信息其实就是图上这么几条,能 ...
- 每次选中数组中的N条数据, 如果让每条数据被选中的次数做到平均??
经常有这样的需求, 有一组数据, 每次展示其中的1条或N条,希望每条数据展示量可以做到平均. 一开始想依次展示每条数据并做记录,整组数据全展示一遍之后清除记录, 然后一直循环下去. 实现的过程中又觉得 ...
- CentOS6.5 Cloud-Init使用
使用cloud-init实现虚拟机信息管理 http://blog.marvelworld.tk/?p=575 谈谈Openstack的CentOS镜像 http://www.chenshake.co ...
- 51nod 1405【DFS】
思路: 对于结点 u 的子节点 v, 如果已经一直到结点 u 的答案ans[u],那么转移到对于结点 v,num[v] 为 v为根的树的结点个数,那么对于结点v的答案相对于结点u的答案来说, ans[ ...
- 如何给自己的开源项目选择和添加 License
License 的作用:开源 == 为所欲为? 开源并不等于为所欲为! 代码的用途,修改之后的代码有什么要求,开源程序对于原作者的权利和责任等等,都是需要明确的. 开源协议 License 就是这么一 ...
- MCP|ZCM|Investigating Lactococcus lactis MG1363 response to phage p2 infection at the proteome level(研究乳酸乳球菌MG1363在噬菌体p2感染后的蛋白质组水平变化)
一.概述: 噬菌体是特异性感染并最终杀死其细菌宿主的病毒.他们在所有生态系统中发挥着关键的生态作用.尽管经过了几十年的研究,噬菌体与细菌宿主之间的相互作用仍然知之甚少.本研究使用无标记定量蛋白质组学来 ...
- RequireJS 2.0 API之配置项
转载自http://blog.csdn.net/kevinwon1985/article/details/8155267 RequireJS 把每一个依赖项当做一个script标签,使用 head.a ...