Skip to content

USB Printing Issue in SDK 34+ #26

@hamzamon

Description

@hamzamon

When I am trying to plug USB printer in Android 14+

Shutting down VM
E/AndroidRuntime( 1655): FATAL EXCEPTION: main
E/AndroidRuntime( 1655): Process: com.printer.app, PID: 1655
E/AndroidRuntime( 1655): java.lang.RuntimeException: Unable to start receiver com.codingdevs.thermal_printer.usb.UsbReceiver: java.lang.IllegalArgumentException: com.printer.app: 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.

So i have changed the code in

com.codingdevs.thermal_printer.USBPrinterAdapter.kt, com.codingdevs.thermal_printer.usb.USBPrinterService.kt and com.codingdevs.thermal_printer.usb.UsbReceiver.kt

from

mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0)
}

to

mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE)
}

But still getting same error so again changed as

mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0)
}
This time app not crashing when plugged usb cable, now asking for permission, done.

But when trying to connect USB printer its not get connected callback.
Means, the status in your USB listener (PrinterManager.instance.stateUSB.listen) is not reaching USBStatus.connected

How to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions