Two methods for Android to obtain USB permissions

Recently, I have encountered several USB modules adapted for use on Android platform, so I need to use USB permission acquisition

##There are two ways to obtain USB permissions:

 <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>

It should be noted that:

Where device_ The available USB devices are listed in filter.xml. After the USB device is connected to the mobile phone, the app will automatically ask whether it is allowed to obtain the permission of the USB.

device_ The location of filter.xml is shown in the following figure:

device_ The contents in filter.xml are:

<?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 devices are defined by vendor ID and product ID. here is a summary of Linux USB device vendor ID and product ID, which can be used as a reference for Android USB devices.

//获取USB设备ACTION
private 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,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();
//   }
//  }
// }

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{
      //用户点击拒绝了
     }
    }
   }
  }
 };

summary

The above are the two methods for Android to obtain USB permission introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>