diff --git a/android/src/main/java/dev/bessems/usbserial/UsbSerialPlugin.java b/android/src/main/java/dev/bessems/usbserial/UsbSerialPlugin.java index 25d08e9..53aefae 100644 --- a/android/src/main/java/dev/bessems/usbserial/UsbSerialPlugin.java +++ b/android/src/main/java/dev/bessems/usbserial/UsbSerialPlugin.java @@ -1,5 +1,6 @@ package dev.bessems.usbserial; +import android.annotation.SuppressLint; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -13,6 +14,7 @@ import com.felhr.usbserial.UsbSerialDevice; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.ArrayList; @@ -105,6 +107,7 @@ private interface AcquirePermissionCallback { void onSuccess(UsbDevice device); void onFailed(UsbDevice device); } + @SuppressLint("PrivateApi") private void acquirePermissions(UsbDevice device, AcquirePermissionCallback cb) { class BRC2 extends BroadcastReceiver { @@ -148,7 +151,19 @@ public void onReceive(Context context, Intent intent) { flags = PendingIntent.FLAG_MUTABLE; } - PendingIntent permissionIntent = PendingIntent.getBroadcast(cw, 0, new Intent(ACTION_USB_PERMISSION), flags); + Intent intent = new Intent(ACTION_USB_PERMISSION); + + Class activityThread = null; + try { + activityThread = Class.forName("android.app.ActivityThread"); + Method method = activityThread.getDeclaredMethod("currentPackageName"); + String appPackageName = (String) method.invoke(activityThread); + intent.setPackage(appPackageName); + } catch (Exception e) { + // Not too important to throw anything + } + + PendingIntent permissionIntent = PendingIntent.getBroadcast(cw, 0, intent, flags); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);