From 17307f02999bee7365f4b175b458bbca98452ef6 Mon Sep 17 00:00:00 2001 From: Muhamad Bagus Prasetyo Date: Wed, 6 Nov 2024 13:21:41 +0700 Subject: [PATCH 1/2] Fix Android 14 SDK 34 --- .../thermal_printer/adapter/USBPrinterAdapter.kt | 12 +++++++++--- .../thermal_printer/usb/USBPrinterService.kt | 12 +++++++++--- .../codingdevs/thermal_printer/usb/UsbReceiver.kt | 7 ++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/android/src/main/kotlin/com/codingdevs/thermal_printer/adapter/USBPrinterAdapter.kt b/android/src/main/kotlin/com/codingdevs/thermal_printer/adapter/USBPrinterAdapter.kt index dc93fb5..8b0333b 100644 --- a/android/src/main/kotlin/com/codingdevs/thermal_printer/adapter/USBPrinterAdapter.kt +++ b/android/src/main/kotlin/com/codingdevs/thermal_printer/adapter/USBPrinterAdapter.kt @@ -27,14 +27,20 @@ class USBPrinterAdapter private constructor() { fun init(reactContext: Context?) { mContext = reactContext mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager + val explicitIntent = Intent(ACTION_USB_PERMISSION); + explicitIntent.setPackage(mContext?.packageName); 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) + PendingIntent.getBroadcast(mContext, 0, explicitIntent, PendingIntent.FLAG_MUTABLE) } else { - PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0) + PendingIntent.getBroadcast(mContext, 0, explicitIntent, 0) } val filter = IntentFilter(ACTION_USB_PERMISSION) filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED) - mContext!!.registerReceiver(mUsbDeviceReceiver, filter) + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { + mContext!!.registerReceiver(mUsbDeviceReceiver, filter, Context.RECEIVER_NOT_EXPORTED); + } else { + mContext!!.registerReceiver(mUsbDeviceReceiver, filter); + } Log.v(LOG_TAG, "ESC/POS Printer initialized") } diff --git a/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/USBPrinterService.kt b/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/USBPrinterService.kt index a9c3499..b61aaf5 100644 --- a/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/USBPrinterService.kt +++ b/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/USBPrinterService.kt @@ -70,14 +70,20 @@ class USBPrinterService private constructor(private var mHandler: Handler?) { fun init(reactContext: Context?) { mContext = reactContext mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager + val explicitIntent = Intent(ACTION_USB_PERMISSION); + explicitIntent.setPackage(mContext?.packageName); 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) + PendingIntent.getBroadcast(mContext, 0, explicitIntent, PendingIntent.FLAG_MUTABLE) } else { - PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0) + PendingIntent.getBroadcast(mContext, 0, explicitIntent, 0) } val filter = IntentFilter(ACTION_USB_PERMISSION) filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED) - mContext!!.registerReceiver(mUsbDeviceReceiver, filter) + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { + mContext!!.registerReceiver(mUsbDeviceReceiver, filter, Context.RECEIVER_NOT_EXPORTED); + } else { + mContext!!.registerReceiver(mUsbDeviceReceiver, filter); + } Log.v(LOG_TAG, "ESC/POS Printer initialized") } diff --git a/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/UsbReceiver.kt b/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/UsbReceiver.kt index e1faf75..0d538f1 100644 --- a/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/UsbReceiver.kt +++ b/android/src/main/kotlin/com/codingdevs/thermal_printer/usb/UsbReceiver.kt @@ -16,11 +16,12 @@ class UsbReceiver : BroadcastReceiver() { if (UsbManager.ACTION_USB_DEVICE_ATTACHED == action) { val usbDevice: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) - + val explicitIntent = Intent("com.flutter_pos_printer.USB_PERMISSION"); + explicitIntent.setPackage(context?.packageName); val mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - PendingIntent.getBroadcast(context, 0, Intent("com.flutter_pos_printer.USB_PERMISSION"), PendingIntent.FLAG_MUTABLE) + PendingIntent.getBroadcast(context, 0, explicitIntent, PendingIntent.FLAG_MUTABLE) } else { - PendingIntent.getBroadcast(context, 0, Intent("com.flutter_pos_printer.USB_PERMISSION"), 0) + PendingIntent.getBroadcast(context, 0, explicitIntent, 0) } val mUSBManager = context?.getSystemService(Context.USB_SERVICE) as UsbManager? mUSBManager?.requestPermission(usbDevice, mPermissionIndent) From ecb7858fd39a7476a3e79b891cbb50901d3e9e54 Mon Sep 17 00:00:00 2001 From: Muhamad Bagus Prasetyo Date: Tue, 15 Jul 2025 10:19:59 +0700 Subject: [PATCH 2/2] Added namespace on build.gradle --- android/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/android/build.gradle b/android/build.gradle index 909ce0a..23a16b8 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -25,6 +25,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { + namespace "com.codingdevs.thermal_printer" compileSdkVersion 33 compileOptions {