溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中如何獲取usb權限

發布時間:2021-07-29 14:14:51 來源:億速云 閱讀:294 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女