AndroidStudio USB: EXTRA_PERMISSION_GRANTED returns false - always
Asked Answered
E

2

5

I want to connect an USB CDC Device (an FTDI chip or a CD2010 or a custom made) to my Galaxy A12 (Android SDK 30) using the following code (AndroidStudio Chipmunk):

public class MainActivity extends AppCompatActivity {
public static final String ACTION_USB_PERMISSION = "com.example.usbtestapp.USB_PERMISSION";
static final int intentFlags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ? PendingIntent.FLAG_IMMUTABLE : 0;
static final int MY_CAMERA_PERMISSION_CODE  = 100;
private Context ContextofMainAcitivity;
public IntentFilter USBconnectdisconnect_intentFilter;
public Intent GetEXTRA_PERMISSION_GRANTEDIntent;
public PendingIntent current_usbpermissionIntent;
public UsbDevice current_usbdevice;
public UsbManager current_usbManager;
private static final String TAG = "usbtestapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ContextofMainAcitivity = this;
    current_usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    USBconnectdisconnect_intentFilter = new IntentFilter();
    USBconnectdisconnect_intentFilter.addAction(ACTION_USB_PERMISSION);
    USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(currentUSBBroasdcastReceiver, USBconnectdisconnect_intentFilter);
    GetEXTRA_PERMISSION_GRANTEDIntent = new Intent(ACTION_USB_PERMISSION);
    GetEXTRA_PERMISSION_GRANTEDIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
    Log.v(TAG, "Waiting for USB devices...");
} // create

BroadcastReceiver currentUSBBroasdcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case UsbManager.ACTION_USB_DEVICE_ATTACHED:
                current_usbdevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                Log.v(TAG,"ACTION_USB_DEVICE_ATTACHED.");
                if (current_usbManager.hasPermission(current_usbdevice)) {
                    Log.v(TAG,"Permission is already granted.");
                    return;
                }
                current_usbpermissionIntent = PendingIntent.getBroadcast(ContextofMainAcitivity, 0, GetEXTRA_PERMISSION_GRANTEDIntent , intentFlags);
                if (!current_usbManager.hasPermission(current_usbdevice)) {
                    Log.v(TAG,  "Request Permision from User...");
                    current_usbManager.requestPermission(current_usbdevice, current_usbpermissionIntent);
                }
                break;
            case UsbManager.ACTION_USB_DEVICE_DETACHED:
                Log.v(TAG,"ACTION_USB_DEVICE_DETACHED");
                break;
            case ACTION_USB_PERMISSION:
                if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false) == true) {
                    Log.v(TAG,"ACTION_USB_PERMISSION - Granted !");
                } else {
                    Log.v(TAG,"ACTION_USB_PERMISSION - Denied");
                }
                break;
        } //switch
    } // onReceive
}; // braadcast

} // class

After connecting a FTDI FT232 - CDC Device this happens:

2022-08-07 14:19:44.206 15767-15767/com.social.usbtestapp V/usbtestapp: Waiting for USB devices...

2022-08-07 14:19:50.765 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_DEVICE_ATTACHED.

2022-08-07 14:19:50.770 15767-15767/com.social.usbtestapp V/usbtestapp: Request Permision from User...

Then a Dialogbox pops open and shows the device string + asks the user for permission – no matters what is selected OK or CANCEL the result is always:

2022-08-07 14:19:52.759 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_PERMISSION – Denied

I also checked the FTDI example but finally in their internal functionality to my understanding they do pretty much same. I also checked StackOverflow java - UsbDevice requestPermission is denied without showing the request dialog prompt - Stack Overflow but this didn’t solved my issues.

For any unknown reason I got this also ones to work meaning that after selecting OK in the permission dialog the getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED was really true while adding or removing some additionally SDK’s such as Lolipop or Pie but I couldn’t reproduce this anymore – would be glad for any suggestion .

Epiblast answered 7/8, 2022 at 13:24 Comment(0)
H
14

Change PendingIntent.FLAG_IMMUTABLE to PendingIntent.FLAG_MUTABLE in your intentFlags initialization. I don't know why it has to be FLAG_MUTABLE but it solved the problem for me.

Hominy answered 26/8, 2022 at 12:43 Comment(3)
Thanks, solves it. However now it is not possible when targeting API 34 and above. "java.lang.IllegalArgumentException: de.kaprion.idealwallet: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE.." How to solve the issue when only FLAG_IMMUTABLE can be used. ?Combined
@Combined Take a look on this Android docAmadus
Would like to know this as wellGlarus
W
1

Use an explicit intent with FLAG_MUTABLE.

GetEXTRA_PERMISSION_GRANTEDIntent = new Intent(ACTION_USB_PERMISSION);
GetEXTRA_PERMISSION_GRANTEDIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
GetEXTRA_PERMISSION_GRANTEDIntent.setPackage(ContextofMainAcitivity.getPackageName());

PendingIntent.getBroadcast(ContextofMainAcitivity, 0, GetEXTRA_PERMISSION_GRANTEDIntent, PendingIntent.FLAG_MUTABLE);
Wingspan answered 1/4 at 14:48 Comment(1)
in onReceive this intent always has an empty extras mapGlarus

© 2022 - 2024 — McMap. All rights reserved.