/// <summary> /// /// </summary> protected virtual void OnUsbDeviceDetached(UsbDevice device) { if (m_UsbDevice.GetDeviceName() == device.GetDeviceName()) { Close(); UsbManager.main.onUsbDeviceDetached -= OnUsbDeviceDetached; } }
public bool grantAutomaticPermission(UsbDevice usbDevice) { try { Context context = Application.Context; PackageManager pkgManager = context.PackageManager; ApplicationInfo appInfo = pkgManager.GetApplicationInfo(context.PackageName, PackageInfoFlags.MetaData); Class serviceManagerClass = Class.ForName("Android.OS.ServiceManager"); Method getServiceMethod = serviceManagerClass.GetDeclaredMethod("getService", serviceManagerClass); getServiceMethod.Accessible = true; IBinder binder = (IBinder)getServiceMethod.Invoke(null, Context.UsbService); Class iUsbManagerClass = Class.ForName("android.hardware.usb.IUsbManager"); Class stubClass = Class.ForName("android.hardware.usb.IUsbManager$Stub"); Method asInterfaceMethod = stubClass.GetDeclaredMethod("asInterface", iUsbManagerClass); asInterfaceMethod.Accessible = true; object iUsbManager = asInterfaceMethod.Invoke(null, (Java.Lang.Object)binder); //System.out.println("UID : " + appInfo.uid + " " + appInfo.processName + " " + appInfo.permission); Method grantDevicePermissionMethod = iUsbManagerClass.GetDeclaredMethod("grantDevicePermission", (Java.Lang.Class)UsbDevice.GetDeviceName(0)); grantDevicePermissionMethod.Accessible = true; grantDevicePermissionMethod.Invoke((Java.Lang.Object)iUsbManager, usbDevice, appInfo.Uid); //System.out.println("Method OK : " + binder + " " + iUsbManager); return(true); } catch (System.Exception ex) { Toast.MakeText(Application.Context, ex.Message, ToastLength.Long).Show(); using (StreamWriter file = new StreamWriter(archivo)) { file.Write(ex.Message); } //System.err.println("Error trying to assing automatic usb permission : "); //ex.printStackTrace(); return(false); } }