Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permission denied after update to Android 12 #494

Open
topiux opened this issue Apr 30, 2023 · 1 comment
Open

Permission denied after update to Android 12 #494

topiux opened this issue Apr 30, 2023 · 1 comment

Comments

@topiux
Copy link

topiux commented Apr 30, 2023

I was successfully using this library with Android 10 on Unity 2021.3.4f1. An update by Meta to Android 12L broke several things including USB communications. This is what I get via logcat:

04-30 18:30:16.748  4824  4849 D UnityUsbSerial: connectToDevice: Start
04-30 18:30:16.750  4824  4849 D UnityUsbSerial: connectToDevice: Drivers found
04-30 18:30:16.752  1110  1123 W Bundle  : Key android.hardware.usb.action.USB_DEVICE_ATTACHED expected Integer but value was a java.lang.String.  The default value 0 was returned.
04-30 18:30:16.752  1110  1123 W Bundle  : 	at com.android.server.usb.UsbUserSettingsManager.canBeDefault(UsbUserSettingsManager.java:105)
04-30 18:30:16.752  1110  1123 W Bundle  : 	at com.android.server.usb.UsbUserPermissionManager.requestPermission(UsbUserPermissionManager.java:744)
04-30 18:30:16.752  1110  1123 W Bundle  : 	at com.android.server.usb.UsbService.requestDevicePermission(UsbService.java:521)
04-30 18:30:16.752  1110  1123 W Bundle  : 	at android.hardware.usb.IUsbManager$Stub.onTransact(IUsbManager.java:703)
04-30 18:30:16.754  1110  1123 I ActivityTaskManager: START u0 {flg=0x10000000 cmp=com.oculus.os.vrusb/.UsbPermissionActivity (has extras)} from uid 1000
04-30 19:10:02.902 11262 11262 D UnityUsbSerial: permission denied for device UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=4292,mProductId=60000,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Silicon Labs,mProductName=CP2102 USB to UART Bridge Controller,mVersion=1.00,mSerialNumberReader=android.hardware.usb.IUsbSerialReader$Stub$Proxy@8db262, mHasAudioPlayback=false, mHasAudioCapture=false, mHasMidi=false, mHasVideoCapture=false, mHasVideoPlayback=false, mConfigurations=[

This is my code for handling USB permission request:

public String connectToDevice() {
        Log.d(TAG, "connectToDevice: Start");

        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);

        if (availableDrivers.isEmpty()) {
            Log.d(TAG, "connectToDevice: No available drivers");
            return "No available drivers";
        } else {
            Log.d(TAG, "connectToDevice: Drivers found");
        }

        // Use the first available driver.
        driver = availableDrivers.get(0);

        if (!usbManager.hasPermission(driver.getDevice())) {
            PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
            usbManager.requestPermission(driver.getDevice(), permissionIntent);
            return "Requesting permission";
        } else {
            // ...

            return "Connected to the device";
        }
    }
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            // Call method to set up device communication
                            UsbDeviceConnection connection = usbManager.openDevice(device);

                            if (connection == null) {
                                Log.d(TAG, "connectToDevice: Connection is null");
                            } else {
                                Log.d(TAG, "connectToDevice: Connection established");
                            }

                            usbSerialPort = driver.getPorts().get(0);
                            try {
                                usbSerialPort.open(connection);
                                usbSerialPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
                                Log.d(TAG, "connectToDevice: Port opened successfully");
                            } catch (IOException e) {
                                Log.e(TAG, "connectToDevice: Error opening port", e);
                            } finally {
                                Log.d(TAG, "connectToDevice: End");
                            }
                        }
                    } else {
                        Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };
@kai-morich
Copy link
Collaborator

as of Android 12 you have to specify MUTABLE flag when creating an Intent as done here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants