Android USB Automatically Grant Permission

后端 未结 3 842
孤城傲影
孤城傲影 2020-12-16 08:58

Is there anyway to access the IUsbManager Interface class to call the service.grantDevicePermission() method to automatically set the permission for the USB device without a

相关标签:
3条回答
  • 2020-12-16 09:11

    Of course not. That would invalidate the whole point of having permissions in the first place. But the user can elect always to allow specific connections.

    0 讨论(0)
  • 2020-12-16 09:23

    Of course yes! you can use this code, worked for me:

    public boolean grantAutomaticPermission(UsbDevice usbDevice)
     {
        try
        {
         Context context=YourActivityOrApplication;
         PackageManager pkgManager=context.getPackageManager();
         ApplicationInfo     appInfo=pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    
         Class serviceManagerClass=Class.forName("android.os.ServiceManager");
         Method getServiceMethod=serviceManagerClass.getDeclaredMethod("getService",String.class);
         getServiceMethod.setAccessible(true);
         android.os.IBinder binder=(android.os.IBinder)getServiceMethod.invoke(null, Context.USB_SERVICE);
    
         Class iUsbManagerClass=Class.forName("android.hardware.usb.IUsbManager");
         Class stubClass=Class.forName("android.hardware.usb.IUsbManager$Stub");
         Method asInterfaceMethod=stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
         asInterfaceMethod.setAccessible(true);
         Object iUsbManager=asInterfaceMethod.invoke(null, binder);
    
    
         System.out.println("UID : " + appInfo.uid + " " + appInfo.processName + " " + appInfo.permission);
         final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class,int.class);
         grantDevicePermissionMethod.setAccessible(true);
         grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
    
    
         System.out.println("Method OK : " + binder + "  " + iUsbManager);
         return true;
        }
        catch(Exception e)
        {
         System.err.println("Error trying to assing automatic usb permission : ");
         e.printStackTrace();
         return false;
        }
     }
    

    What i did is implement the way android does this using reflection. In order to use this code you must have the:

    <uses-permission android:name="android.permission.MANAGE_USB"/>
    

    However there is an issue, i don't known exactly, correct me if i am wrong: The USER holds the permission, not the application, then if you try to start the application from applications dash board you will fail. Is necessary to ensure than android begins your aplication, for example, setting it as launcher and let it start in a HOME intent.

    Greetings, expect can help someone.

    0 讨论(0)
  • 2020-12-16 09:31

    Yes you can,

    if you take the code that was posted by @CristhianB you will be able to get it done BUT and it's a hell of a BUT you will need to get it working with one of the following:

    1. move your app to the priv-app folder and set the permission to run as system app. (with the userId and user group)
    2. do the same as @CristhianB did ( set the app to run as system service, which is actually set it to run as launcher.
    3. re-sign the Os with your keys and then set the permission properties in your app manifest android:protectionLevel="signatureOrSystem" and your are golden !!

    the third method allows your app to do anything you want because it will be signed with the same key as the system keys which means you are running as system app but you dont have to be installing your app in the priv-app folder.

    if you have any question regarding this matter i will be happy to help, i just implemented the same thing and it took me three weeks to get it done so i can save you a lot of time. :-)

    0 讨论(0)
提交回复
热议问题