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