4949 }
5050)
5151public class BetaflightSerialPlugin extends Plugin implements SerialInputOutputManager .Listener {
52+ // Hold a static reference for forwarding permission callbacks from an explicit BroadcastReceiver
53+ private static java .lang .ref .WeakReference <BetaflightSerialPlugin > sInstance = new java .lang .ref .WeakReference <>(null );
5254 private static final String TAG = "BetaflightSerial" ;
5355 private static final String ACTION_USB_PERMISSION = "com.betaflight.USB_PERMISSION" ;
5456 private static final int WRITE_WAIT_MILLIS = 2000 ;
@@ -81,6 +83,7 @@ public void onReceive(Context context, Intent intent) {
8183 @ Override
8284 public void load () {
8385 super .load ();
86+ sInstance = new java .lang .ref .WeakReference <>(this );
8487 usbManager = (UsbManager ) getContext ().getSystemService (Context .USB_SERVICE );
8588
8689 // Register USB broadcast receivers
@@ -136,13 +139,32 @@ public void requestPermission(PluginCall call) {
136139 String deviceKey = getDeviceKey (device );
137140 permissionRequestedDevices .put (deviceKey , device );
138141
142+ // Create fully explicit broadcast intent with component
143+ Intent permissionAction = new Intent (ACTION_USB_PERMISSION );
144+ permissionAction .setComponent (new android .content .ComponentName (
145+ getContext (),
146+ UsbPermissionReceiver .class
147+ ));
148+ permissionAction .putExtra (UsbManager .EXTRA_DEVICE , device );
149+
150+ int requestCode = device .getDeviceId ();
151+ int flags ;
152+
153+ if (Build .VERSION .SDK_INT >= 34 ) { // Android 14+ (U / API 34)
154+ // Android 14+ requires IMMUTABLE for explicit intents
155+ flags = PendingIntent .FLAG_UPDATE_CURRENT | PendingIntent .FLAG_IMMUTABLE ;
156+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
157+ // Android 12-13 requires MUTABLE for UsbManager
158+ flags = PendingIntent .FLAG_UPDATE_CURRENT | PendingIntent .FLAG_MUTABLE ;
159+ } else {
160+ flags = PendingIntent .FLAG_UPDATE_CURRENT ;
161+ }
162+
139163 PendingIntent permissionIntent = PendingIntent .getBroadcast (
140164 getContext (),
141- 0 ,
142- new Intent (ACTION_USB_PERMISSION ),
143- Build .VERSION .SDK_INT >= Build .VERSION_CODES .S
144- ? PendingIntent .FLAG_MUTABLE | PendingIntent .FLAG_UPDATE_CURRENT
145- : PendingIntent .FLAG_UPDATE_CURRENT
165+ requestCode ,
166+ permissionAction ,
167+ flags
146168 );
147169
148170 usbManager .requestPermission (device , permissionIntent );
@@ -356,7 +378,7 @@ public void onRunError(Exception e) {
356378
357379 // ===== Private helper methods =====
358380
359- private void handlePermissionResult (Intent intent ) {
381+ public void handlePermissionResult (Intent intent ) {
360382 UsbDevice device = intent .getParcelableExtra (UsbManager .EXTRA_DEVICE );
361383 if (device == null ) return ;
362384
@@ -374,6 +396,14 @@ private void handlePermissionResult(Intent intent) {
374396 }
375397 }
376398
399+ // Static entry point for the explicit BroadcastReceiver to forward the permission result
400+ public static void onUsbPermissionResult (Context context , Intent intent ) {
401+ BetaflightSerialPlugin instance = sInstance .get ();
402+ if (instance != null ) {
403+ instance .handlePermissionResult (intent );
404+ }
405+ }
406+
377407 private void handleDeviceAttached (Intent intent ) {
378408 UsbDevice device = intent .getParcelableExtra (UsbManager .EXTRA_DEVICE );
379409 if (device == null ) return ;
0 commit comments