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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FlutterUsbPrinterPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_usb_printer")
channel.setMethodCallHandler(this)
context = flutterPluginBinding.applicationContext
context = flutterPluginBinding.getApplicationContext()
adapter = USBPrinterAdapter().getInstance()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.usb.*
import android.os.Build
import android.util.Base64
import android.util.Log
import android.widget.Toast
Expand All @@ -21,7 +22,7 @@ class USBPrinterAdapter {
private val LOG_TAG = "Flutter USB Printer"
private var mContext: Context? = null
private var mUSBManager: UsbManager? = null
private var mPermissionIndent: PendingIntent? = null
private var mPermissionIntent: PendingIntent? = null
private var mUsbDevice: UsbDevice? = null
private var mUsbDeviceConnection: UsbDeviceConnection? = null
private var mUsbInterface: UsbInterface? = null
Expand All @@ -41,8 +42,8 @@ class USBPrinterAdapter {

private val mUsbDeviceReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (ACTION_USB_PERMISSION == action) {
var action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized(this) {
val usbDevice =
intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
Expand Down Expand Up @@ -73,8 +74,13 @@ class USBPrinterAdapter {
fun init(reactContext: Context?) {
mContext = reactContext
mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager
mPermissionIndent =
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
mPermissionIntent =
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
} else {
mPermissionIntent =
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT)
}
val filter = IntentFilter(ACTION_USB_PERMISSION)
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
mContext!!.registerReceiver(mUsbDeviceReceiver, filter)
Expand Down Expand Up @@ -115,7 +121,7 @@ class USBPrinterAdapter {
"Request for device: vendor_id: " + usbDevice.vendorId + ", product_id: " + usbDevice.productId
)
closeConnectionIfExists()
mUSBManager!!.requestPermission(usbDevice, mPermissionIndent)
mUSBManager!!.requestPermission(usbDevice, mPermissionIntent)
return true
}
}
Expand All @@ -126,7 +132,7 @@ class USBPrinterAdapter {

private fun openConnection(): Boolean {
if (mUsbDevice == null) {
Log.e(LOG_TAG, "USB Deivce is not initialized")
Log.e(LOG_TAG, "USB Device is not initialized")
return false
}
if (mUSBManager == null) {
Expand Down