Android gets usb permission

Foreword: I
have encountered several USB modules adapting to use on the android platform in my work recently, so we need to use the USB permission acquisition problem

##USB permission can be obtained in the following two ways:

1. Configure the following directly in the AndroidManifest.xml file:

        <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
the available usb devices are listed in device_filter.xml. When the usb device is connected to the mobile phone, the app will automatically ask whether to allow the permission to obtain the usb.
The device_filter.xml is placed as shown in the figure below:

The content in device_filter.xml is:

<?xml version="1.0" encoding="utf-8"?> USB devices are defined by vendor-id (vendor id) and product-id (product id) together, here is a linux usb device vendor id and The summary of product id can be used as a reference for Android usb devices.

2. Dynamic code acquisition
2.1 Acquisition from the code (provided that the usbdevice to be applied for USB permission has been located)
//Get the USB device ACTION
private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";

// Get the list of USB devices and locate the USB device to apply for permission
// mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
// HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
// List deviceList = new ArrayList();
// for (UsbDevice device: devices.values()) { // if (3540
device.getVendorId() && 567device.getProductId()) {//Get printer device vid and pid
// currentDevice=device;
//}
//}

//Start to apply for USB permissions
private void getUsbPermission(UsbDevice mUSBDevice) { UltraLog.d("Start to apply for USB permissions");

    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); // 该代码执行后,系统弹出一个对话框/等待权限

//The following code is because the pop-up box is directly modified in the system layer, so you don't need to
// 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 Register the broadcast receiver 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{
                    //用户点击拒绝了
                }
            }
        }
    }
};

Guess you like

Origin blog.csdn.net/hai_jian/article/details/102372071