Android 獲取 usb 權限

前言:

最近工作上遇到幾個USB模塊在android平臺上適配使用的情況,所以要用到USB權限獲取問題

##USB權限獲取有以下2種方式:

一、直接在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設備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, 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{
                        //用戶點擊拒絕了
                    }
                }
            }
        }
    };
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章