温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何获取usb权限

发布时间:2021-07-29 14:14:51 来源:亿速云 阅读:242 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关Android中如何获取usb权限,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、直接在AndroidManifest.xml文件中进行如下配置:

<activity   android:name=".DemoCustomAndroidUSBActivity"   android:label="@string/app_name">   <intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>   <!-- USB -->   <intent-filter>     <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />   </intent-filter>   <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />    <!-- USB END -->  </activity>

需要注意的是:

其中 device_filter.xml 中列出了可用 usb 设备,当usb 设备连接手机之后,app 会自动询问是否允许获取该 usb 的权限。

device_filter.xml 放置位置如下图所示 :

device_filter.xml中的内容为:

<?xml version="1.0" encoding="utf-8"?><resources> <usb-device vendor-id="1027" product-id="24577" /> <usb-device vendor-id="3405" product-id="567" /></resources>

usb设备通过 vendor-id(厂商 id) 和 product-id (产品 id)一起来定义的,这里有一个 linux 的 usb设备厂商 id 和产品 id 的汇总,可以作为 Android usb 设备的参考。

二、动态代码获取

2.1 代码中获取(前提是已经定位到要申请USB权限的usbdevice)

//获取USB设备ACTIONprivate static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";//   获取USB设备列表及定位到要申请权限的USB设备//   mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);//   HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();//   List<UsbDevice> deviceList = new ArrayList<UsbDevice>();//   for (UsbDevice device : devices.values()) {//    if (3540==device.getVendorId() && 567==device.getProductId()) {//获取打印机设备 vid和pid//     currentDevice=device;//    }//   }//开始申请USB权限private void getUsbPermission(UsbDevice mUSBDevice) {  UltraLog.d("开始申请USB权限");  PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);  IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);  filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);  filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);  mContext.registerReceiver(mUsbReceiver, filter);  mUsbManager.requestPermission(mUSBDevice, pendingIntent); // 该代码执行后,系统弹出一个对话框/等待权限//以下代码是因为在系统层将弹出框直接修改掉了,可以不用//  long start = System.currentTimeMillis();//  while (!mUsbManager.hasPermission(mUSBDevice)) {//   long current = System.currentTimeMillis();//   if ((current - start) > 3500) {//    break;//   }//   try {//    Thread.sleep(50);//   } catch (InterruptedException e) {//    e.printStackTrace();//   }//  }// }

2.2 注册广播接受者

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {  @SuppressLint("NewApi")  public void onReceive(Context context, Intent intent) {   String action = intent.getAction();   if (ACTION_USB_PERMISSION.equals(action)) {    synchronized (this) {     mContext.unregisterReceiver(mUsbReceiver);     UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);     if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)       && currentDevice.equals(device)) {      //TODO 授权成功,操作USB设备     }else{      //用户点击拒绝了     }    }   }  } };

以上就是Android中如何获取usb权限,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI