Bypass android usb host permission confirmation dialog on Android 9

后端 未结 1 1447
感动是毒
感动是毒 2020-12-06 08:32

Before Android 9 I could bypass android usb host permission confirmation dialog by using root, systemizing my app and using next usb classes https://stackoverflow.com/a/1537

相关标签:
1条回答
  • 2020-12-06 09:04

    We can solve it by entering:

    adb shell
    su
    settings put global hidden_api_policy_pre_p_apps  1
    settings put global hidden_api_policy_p_apps 1
    

    Restrictions on non-SDK interfaces (Android 9): https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

    And then grantDevicePermission method will be available again through reflection on Android 9:

    public static boolean grantAutomaticUsbPermissionRoot(Context context, UsbDevice usbDevice) {
        try {
            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);
    
            final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class, int.class);
            grantDevicePermissionMethod.setAccessible(true);
            grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    

    p.s. you need root of course and systemize your app (move to /system/priv-app/)

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