一. Usb模式介绍
https://blog.csdn.net/Yoryky/article/details/81813717
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r41
repo sync
1. Usb Host 主模式
USB Host,中文意思是USB主模式,是相对于USB Accessory(USB副模式)来说的。如果Android工作在USB Host模式下,则连接到Android上的USB设备把Android类似的看作是一台PC机,PC机能干的事儿,Android也能干,例如将鼠标、键盘插入则可以使用键盘、鼠标来操作Android系统,如果插入U盘则,通过Android可以读写U盘上的数据。
2. Accessory 副模式
USB Accessory模式表示将Android设备类似当作一个USB的键盘、鼠标、U盘插入到电脑主机上一样使用。
本文主要讲解Usb Host 主模式
在Android中,主要通过UsbManager类去实现其功能
UsbManager mUsbManager= (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
二. 主要功能
2.1 查找设备。
HashMap deviceList = mUsbManager.getDeviceList();
if(deviceList!=null && deviceList.size()>0){
Iterator deviceIter = deviceList.values().iterator();
while(deviceIter.hasNext()){
UsbDevice _device = deviceIter.next();
//请求权限
if(!mUsbManager.hasPermission(_device)){
mUsbManager.requestPermission(_device , mPermissionIntent);
}
}
}else{
//no device
}
2.2 过滤设备。
通过供应商Id,与产品Id来过滤设备
private boolean isSupportDevice(UsbDevice device,int vendorId, int pid){
if(device.getVendorId()==vendorId && device.getProductId() == pid){
return true;
}else{
return false;
}
}
2.2 注册广播,监听状态
一般Usb需要监听状态主要分为以下几种:
- 插入usb USB_STATE_ATTACH
- 拨出usb USB_STATE_DETACH
- 系统弹出是否允许权限时,用户选择了不允许。USB_STATE_PERMISSION_DENIED
以下是检测usb状态方法类,及其使用方法代码:
package per.yue.android.blog.usbdemo;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;
import java.util.HashMap;
import java.util.Iterator;
/**
* Created by matth on 2018/7/13.
*/
public class UsbCheckUtil {
public static final int USB_STATE_PERMISSION_DENIED=1;
public static final int USB_STATE_ATTACH=2;
public static final int USB_STATE_DETACH=3;
public static final int USB_STATE_NO_DEVICE=4;
//当verdorId=-1时,表示不过滤设备
private int vendorId=-1;
private int pId=-1;
private OnUsbStateChange listener;
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent= null;
//任意字符串,一般以包名开头
private static final String ACTION_USB_PERMISSION = "per.yue.android.blog.usbdemo.USB_PERMISSION";
private Context mContext;
public UsbCheckUtil(Context context,OnUsbStateChange l){
mContext=context;
vendorId=-1;
pId=-1;
listener=l;
}
public UsbCheckUtil(Context context,OnUsbStateChange l,int vendorId,int pId){
mContext=context;
this.vendorId=vendorId;
this.pId=pId;
listener=l;
}
//初始化
public void init(){
mUsbManager= (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(mContext,0,new Intent(ACTION_USB_PERMISSION),0);
register();
findUsbDevice();
}
/**
* 遍历usb设备
*/
private void findUsbDevice(){
HashMap deviceList = mUsbManager.getDeviceList();
if(deviceList!=null && deviceList.size()>0){
Iterator deviceIter = deviceList.values().iterator();
while(deviceIter.hasNext()){
UsbDevice _device = deviceIter.next();
boolean isSupport = isSupportDevice(_device.getVendorId(),_device.getProductId());
if(!isSupport){
return;
}
if(!mUsbManager.hasPermission(_device)){
mUsbManager.requestPermission(_device , mPermissionIntent);
}
}
}else{
Log.i("vlog","no device");
if(listener!=null){
listener.stateChange(USB_STATE_NO_DEVICE,null);
}
}
}
/**
* 释放资源
*/
public void release(){
mContext.unregisterReceiver(mUsbReceiver);
}
/**
* 注册广播,监听usb的各种状态
*/
private void register(){
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
//DEVICE_ATTACHED
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
//DEVICE_DETACHED
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
mContext.registerReceiver(mUsbReceiver, filter);
}
/**
* 是不是支持的设备,通过vendorId过滤设备
* @param _vendorId
* @return
*/
private boolean isSupportDevice(int _vendorId,int _pid){
if(vendorId==-1){
//不对设备作过滤
return true;
}else{
if(vendorId==_vendorId && pId==_pid){
return true;
}else{
return false;
}
}
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean isSupport = isSupportDevice(device.getVendorId(),device.getProductId());
if(!isSupport){
//不受支持的设备,不作任何处理
return;
}
String action = intent.getAction();
switch (action){
case ACTION_USB_PERMISSION:
synchronized(this){
if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)){
Log.i("vlog","EXTRA_PERMISSION_GRANTED true");
}
else{
if(listener!=null){
listener.stateChange(USB_STATE_PERMISSION_DENIED,device);
}
}
}
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
if(listener!=null){
listener.stateChange(USB_STATE_DETACH,device);
}
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
if(!mUsbManager.hasPermission(device)){
mUsbManager.requestPermission(device , mPermissionIntent);
}
if(listener!=null){
listener.stateChange(USB_STATE_ATTACH,device);
}
break;
}
}
};
public interface OnUsbStateChange{
public void stateChange(int state,UsbDevice usbDevice);
}
}
package per.yue.android.blog.usbdemo;
import android.hardware.usb.UsbDevice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private TextView tv_msg;
private UsbCheckUtil usbCheckUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_msg=(TextView) findViewById(R.id.tv_msg);
usbCheckUtil=new UsbCheckUtil(MainActivity.this, new UsbCheckUtil.OnUsbStateChange() {
@Override
public void stateChange(int state, UsbDevice usbDevice) {
StringBuilder stringBuilder=new StringBuilder();
Log.i("vlog","state:"+state);
switch (state){
case UsbCheckUtil.USB_STATE_ATTACH:
stringBuilder.append("USB_STATE_ATTACH\n");
break;
case UsbCheckUtil.USB_STATE_DETACH:
stringBuilder.append("USB_STATE_DETACH\n");
break;
case UsbCheckUtil.USB_STATE_PERMISSION_DENIED:
stringBuilder.append("USB_STATE_PERMISSION_DENIED\n");
break;
case UsbCheckUtil.USB_STATE_NO_DEVICE:
stringBuilder.append("USB_STATE_NO_DEVICE\n");
break;
default:
stringBuilder.append("No such state\n");
break;
}
if(usbDevice==null){
stringBuilder.append("Usb device is null\n");
}else{
stringBuilder.append("VendorId:"+usbDevice.getVendorId()+"\n");
stringBuilder.append("ProductId:"+usbDevice.getProductId()+"\n");
}
Log.i("vlog","msg:"+stringBuilder.toString());
showMsg(stringBuilder.toString());
}
},5841,1057);
usbCheckUtil.init();
}
private void showMsg(String msg){
tv_msg.setText(msg);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(usbCheckUtil!=null){
usbCheckUtil.release();
}
}
}
三. 配置文件
在MainActivity里配置以下代码:
实现的效果是当usb设备接入时,会弹出权限请求框,当用户点击ok时,此时会打开MainActivity