Android 에서 usb 권한 을 가 져 오 는 두 가지 방법

4499 단어 androidusb권한
선언:
최근 작업 중 몇 개의 USB 모듈 이 안 드 로 이 드 플랫폼 에서 적합 하 게 사용 되 는 상황 을 만 났 기 때문에 USB 권한 으로 문 제 를 가 져 와 야 합 니 다.
\#\#USB 권한 획득 은 다음 과 같은 두 가지 방법 이 있 습 니 다.
1.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>
주의해 야 할 것 은:
그 중 devicefilter.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)와 produt-id(제품 id)를 통 해 정의 되 었 습 니 다.여기 에는 Liux 의 usb 장치 제조 업 체 id 와 제품 id 의 집합 이 있 습 니 다.Android usb 장치 의 참고 가 될 수 있 습 니 다.
2.동적 코드 획득
2.1 코드 에서 가 져 오기(전 제 는 USB 권한 을 신청 할 usb device 로 지정 되 어 있 음)

//  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{
      //       
     }
    }
   }
  }
 };
총결산
위 에서 말 한 것 은 소 편 이 여러분 에 게 소개 한 안 드 로 이 드 가 usb 권한 을 얻 는 두 가지 방법 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
만약 당신 이 본문 이 당신 에 게 도움 이 된다 고 생각한다 면,전 재 를 환영 합 니 다.번 거 로 우 시 겠 지만 출처 를 밝 혀 주 십시오.감사합니다!

좋은 웹페이지 즐겨찾기