zebra-technologies / Scanner-SDK-for-Android

The Zebra Scanner SDK for Android enables cordless scanners to be connected and controlled by a tablet / smart phone without using a cradle, after pairing over Bluetooth.
https://www.zebra.com/us/en/support-downloads/software/developer-tools/scanner-sdk-for-android.html
MIT License
41 stars 17 forks source link

Crashes when plugging in scanner via USB on Android 12 #8

Closed sobels closed 1 year ago

sobels commented 1 year ago

You can reproduce this by plugging in a scanner for which the app doesn't already have permission. On my device it instantly crashes 100% of the time.

According to the stack trace and decompiled code, the problem is in the USBManager.getAvailableScannersList() method. If it does not already have permission to open the attached device, it constructs an intent to get permission for that device with PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0).

While this has been a warning for a while now, starting in Android 12 this will crash every time - callers now have to specify either PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE when calling this method. In your case, you should probably replace this with PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE).

tl;dr Use PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)

sobels commented 1 year ago

This was caused by me using an outdated library version (specifically, the one located in this repository). You might want to update that version to eliminate confusion, but anyway updating to the latest SDK fixed my problem.