Skip to content Skip to sidebar Skip to footer

Android Usb Automatically Grant Permission

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 always h

Solution 1:

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);
     returntrue;
    }
    catch(Exception e)
    {
     System.err.println("Error trying to assing automatic usb permission : ");
     e.printStackTrace();
     returnfalse;
    }
 }

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

<uses-permissionandroid: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.

Solution 2:

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. :-)

Solution 3:

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.

Post a Comment for "Android Usb Automatically Grant Permission"