1

I am a new flutter developer for an app which works with scan devices. I have no knowledge when it comes to Kotlin code. We now use the UROVO DT50 Scanner which uses per default the Keyboard output mode. Problem with this mode is that I am receiving the scanned data letter by letter over my keyboard which works but is to slow and not the flow we want to use. Instead you can manually choose the mode Intent Output which we prefer.

However we don't want to set this up manually for every device. Is there a way to configure that in the MainActivity? -> I want to get the Intent Output even when the scanner is set to keyboard output mode.

This is my current MainActivity file:

    private val COMMAND_CHANNEL = "com.darryncampbell.datawedgeflutter/command"
    private val SCAN_CHANNEL = "com.darryncampbell.datawedgeflutter/scan"

    private val T50_SCAN_CHANNEL = "T50_SCAN_CHANNEL"

    private val PROFILE_INTENT_ACTION = "com.darryncampbell.datawedgeflutter.SCAN"
    private val PROFILE_INTENT_BROADCAST = "2"

    private val dwInterface = DWInterface()
    private val lucaEvaluator = LucaEvaluator()

    var receiver: BarcodeReceiver? = null

    override
    protected fun onResume() {
        super.onResume()
        receiver?.onResume()
    }

    override
    protected fun onPause() {
        super.onPause()
        receiver?.onPause()
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)

        setupT50ScanChannel(flutterEngine)

        EventChannel(flutterEngine.dartExecutor, SCAN_CHANNEL).setStreamHandler(
                object : StreamHandler {
                    private var dataWedgeBroadcastReceiver: BroadcastReceiver? = null
                    override fun onListen(arguments: Any?, events: EventSink?) {
                        dataWedgeBroadcastReceiver = createDataWedgeBroadcastReceiver(events)
                        val intentFilter = IntentFilter()
                        intentFilter.addAction(PROFILE_INTENT_ACTION)
                        intentFilter.addAction(DWInterface.DATAWEDGE_RETURN_ACTION)
                        intentFilter.addCategory(DWInterface.DATAWEDGE_RETURN_CATEGORY)
                        registerReceiver(
                                dataWedgeBroadcastReceiver, intentFilter)
                    }

                    override fun onCancel(arguments: Any?) {
                        unregisterReceiver(dataWedgeBroadcastReceiver)
                        dataWedgeBroadcastReceiver = null
                    }
                }
        )

        MethodChannel(flutterEngine.dartExecutor, COMMAND_CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "sendDataWedgeCommandStringParameter")
            {
                val arguments = JSONObject(call.arguments.toString())
                val command: String = arguments.get("command") as String
                val parameter: String = arguments.get("parameter") as String
                dwInterface.sendCommandString(applicationContext, command, parameter)
                //  result.success(0);  //  DataWedge does not return responses
            }
            else if (call.method == "createDataWedgeProfile")
            {
                createDataWedgeProfile(call.arguments.toString())
            }
            else {
                result.notImplemented()
            }
        }
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, LUCA_CHANNEL).setMethodCallHandler{
            call, result ->
            when (call.method) {
                "isLucaQRCode" -> {
                    val qrCode = call.argument<String>("qrCode")
                    if(qrCode != null) {
                        result.success(lucaEvaluator.isLucaQRCode(qrCode))
                    }
                }
                "processLucaQRCode" -> {
                    val list = call.arguments<List<String>>()
                    if (list != null) {
                        val qrCode = list[0]
                        val scannerId = list[1]
                        val publicKey = list[2]
                        val locationId = list[3]
                        if(qrCode != null && scannerId != null && publicKey != null && locationId != null) {    
                            result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode,scannerId,publicKey,locationId))
                        }
                    }
                }
            }
        }
    }

    private fun createDataWedgeBroadcastReceiver(events: EventSink?): BroadcastReceiver? {
        return object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if (!(intent.action.equals(PROFILE_INTENT_ACTION))) return;
                val scanData = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_DATA_STRING)
                val symbology = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_LABEL_TYPE)
                if ((scanData != null) && (symbology != null)) { //  A barcode has been scanned
                    var date = Calendar.getInstance().getTime()
                    var df = SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
                    var dateTimeString = df.format(date)
                    var currentScan = Scan(scanData, symbology, dateTimeString);
                    events?.success(currentScan.toJson())
                }
                //  Could handle return values from DW here such as RETURN_GET_ACTIVE_PROFILE
                //  or RETURN_ENUMERATE_SCANNERS
            }
        }
    }

    private fun createDataWedgeProfile(profileName: String) {
        //  Create and configure the DataWedge profile associated with this application
        //  For readability's sake, I have not defined each of the keys in the DWInterface file
        dwInterface.sendCommandString(this, DWInterface.DATAWEDGE_SEND_CREATE_PROFILE, profileName)
        val profileConfig = Bundle()
        profileConfig.putString("PROFILE_NAME", profileName)
        profileConfig.putString("PROFILE_ENABLED", "true") //  These are all strings
        profileConfig.putString("CONFIG_MODE", "UPDATE")
        val barcodeConfig = Bundle()
        barcodeConfig.putString("PLUGIN_NAME", "BARCODE")
        barcodeConfig.putString("RESET_CONFIG", "true") //  This is the default but never hurts to specify
        val barcodeProps = Bundle()
        barcodeConfig.putBundle("PARAM_LIST", barcodeProps)
        profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig)
        val appConfig = Bundle()
        appConfig.putString("PACKAGE_NAME", packageName)      //  Associate the profile with this app
        appConfig.putStringArray("ACTIVITY_LIST", arrayOf("*"))
        profileConfig.putParcelableArray("APP_LIST", arrayOf(appConfig))
        dwInterface.sendCommandBundle(this, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
        //  You can only configure one plugin at a time in some versions of DW, now do the intent output
        profileConfig.remove("PLUGIN_CONFIG")
        val intentConfig = Bundle()
        intentConfig.putString("PLUGIN_NAME", "INTENT")
        intentConfig.putString("RESET_CONFIG", "true")
        val intentProps = Bundle()
        intentProps.putString("intent_output_enabled", "true")
        intentProps.putString("intent_action", PROFILE_INTENT_ACTION)
        intentProps.putString("intent_delivery", PROFILE_INTENT_BROADCAST)  //  "2"
        intentConfig.putBundle("PARAM_LIST", intentProps)
        profileConfig.putBundle("PLUGIN_CONFIG", intentConfig)
        dwInterface.sendCommandBundle(this, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
    }

    private fun setupT50ScanChannel(flutterEngine: FlutterEngine) {
        EventChannel(flutterEngine.dartExecutor, T50_SCAN_CHANNEL).setStreamHandler(
            object : StreamHandler {
                override fun onListen(arguments: Any?, events: EventSink?) {
                    Log.d("MainActivity", "Start T50 Listener")
                    receiver = object: BarcodeReceiver(activity) {
                        override fun onNewScanResult(scanResult: String) {
                            Log.d("MainActivity", "New Scan: $scanResult")
                            events?.success(scanResult)
                        }
                    }
                    receiver?.startBarcodeReceiver()
                    receiver?.onResume()
                }

                override fun onCancel(arguments: Any?) {
                    receiver?.onPause()
                }
            }
        )
    }
}
Yuki
  • 255
  • 1
  • 2
  • 9

0 Answers0