2014-10-20 2 views
2

USB 연결 대화 상자가 표시되지 않도록 특정 장치를 필터링하려고합니다. 이해할 수 있도록 장치 필터를 만들고 허용 할 장치를 지정한 다음 내 의도를 처리하여이를 수행 할 수 있습니다. 활동.Android USB 장치 필터

문제는 내가 연결 한 장치 (device_filter.xml의 목록에 제한되지 않음)에 관계없이 메시지를 표시한다는 것입니다.

어떻게 device_filter.xml에 나열된 장치로 연결 프롬프트를 제한 할 수 있습니까?

device_filter.xml는 :

<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="1027" product-id="24597"> <usb-device vendor-id="1659" product-id="8963" > </resources>

이 파일은 내가 화이트리스트하고자하는 장치를 포함한다.

 <activity 
     android:name=".Player" 
     android:clearTaskOnLaunch="true" 
     android:configChanges="orientation|keyboardHidden" 
     android:exported="true" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <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" /> 
    </activity> 

그리고 내 활동 내 : AndroidManifest.xml을에서

if (intent.getAction() != null){ 
     if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { 
      Context context = getApplicationContext(); 
      try 
      { 
       PackageManager pm = context.getPackageManager(); 
       ApplicationInfo ai = pm.getApplicationInfo("com.package.name", 0); 
       if(ai != null) 
       { 
        UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); 
        IBinder b = ServiceManager.getService(Context.USB_SERVICE); 
        IUsbManager service = IUsbManager.Stub.asInterface(b); 

        HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 

        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
        while(deviceIterator.hasNext()) 
        { 
         UsbDevice device = deviceIterator.next(); 
         Log.d ("DERP", "The Vendor ID is: "+device.getVendorId()); 
         Log.d ("DERP", "The interface is: "+device.getInterfaceCount()); 

         //service.grantDevicePermission(device, ai.uid); 
         //service.setDevicePackage(device, "com.package.name"); 
        } 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 

답변

3
조금 늦게

하지만 필터가 잘 형성되지 않은 :

<usb-device vendor-id="1027" product-id="24597" /> 

당신은 잘못입니다. 슬래시 (/)를 사용하여 태그를 닫습니다.