From 083ce4a0ea0f3a3b73ca042eec522e515eea1720 Mon Sep 17 00:00:00 2001 From: nphuoc1625 Date: Mon, 16 Jun 2025 10:43:20 +0700 Subject: [PATCH 1/2] fix USBPrinterService: usbDevice.deviceName could be null resulted in app crash immediately --- .../flutter_pos_printer_platform/usb/USBPrinterService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt index 3c2fa21a..9f62646a 100644 --- a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt +++ b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt @@ -44,7 +44,7 @@ class USBPrinterService private constructor(private var mHandler: Handler?) { state = STATE_USB_CONNECTED mHandler?.obtainMessage(STATE_USB_CONNECTED)?.sendToTarget() } else { - Toast.makeText(context, mContext?.getString(R.string.user_refuse_perm) + ": ${usbDevice!!.deviceName}", Toast.LENGTH_LONG).show() + Toast.makeText(context, mContext?.getString(R.string.user_refuse_perm) + ": ${usbDevice?.deviceName ?: "Unknown Device"}", Toast.LENGTH_LONG).show() state = STATE_USB_NONE mHandler?.obtainMessage(STATE_USB_NONE)?.sendToTarget() } From cf9ae0923510776e688eb08b2363c20bbd816eb8 Mon Sep 17 00:00:00 2001 From: nphuoc1625 <82688178+nphuoc1625@users.noreply.github.com> Date: Tue, 17 Jun 2025 11:06:50 +0700 Subject: [PATCH 2/2] Update USBPrinterService.kt, use use an explicit intent with a packageName based on https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents --- .../usb/USBPrinterService.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt index 9f62646a..be5d63dd 100644 --- a/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt +++ b/android/src/main/kotlin/com/sersoluciones/flutter_pos_printer_platform/usb/USBPrinterService.kt @@ -70,10 +70,16 @@ class USBPrinterService private constructor(private var mHandler: Handler?) { fun init(reactContext: Context?) { mContext = reactContext mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager + + val GetEXTRA_PERMISSION_GRANTEDIntent = Intent(ACTION_USB_PERMISSION).apply { + putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true) + 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_IMMUTABLE) + PendingIntent.getBroadcast(mContext, 0, GetEXTRA_PERMISSION_GRANTEDIntent, PendingIntent.FLAG_MUTABLE) } else { - PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0) + PendingIntent.getBroadcast(mContext, 0, GetEXTRA_PERMISSION_GRANTEDIntent, 0) } val filter = IntentFilter(ACTION_USB_PERMISSION) filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED) @@ -269,4 +275,4 @@ class USBPrinterService private constructor(private var mHandler: Handler?) { return mInstance!! } } -} \ No newline at end of file +}