Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
namespace "com.codingdevs.thermal_printer"
compileSdkVersion 33

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down