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 { 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)