Barcode Scanner - (Data Wedge) TC26 device reads data twice in EditText

698 views Asked by At

I am working on a barcode scanner using DataWedge API's for TC26 device. I can see sometimes the value in EditTextBox the value is shown twice randomly. For example : value 123 is scanned but in EdittextBox it shows 123123. I've tried clearing EditTextBox and set the value again but still no luck.

Code for receiving the output from laser scan.

open fun initPackageScan(
    context: Context?,
    listener: PackageScanListener
){
    this.packageScanListener = listener
}

inner class ScanBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        if (action == BuildConfig.APPLICATION_ID) {
        // Received a barcode scan
           try {
                var scanData = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_DATA_STRING)
                var symbology = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_LABEL_TYPE)
              
                Timber.i("Scanned item $scanData and it's type $symbology")
                if(!scanData.isNullOrEmpty() || !symbology.isNullOrEmpty()) {
                    var labelType = symbology?.substringAfterLast("-")
                    packageScanListener.onPackageScanned(scanData, labelType)
                }
            } 
            catch (e: Exception) {
                Timber.e(e)
                e.printStackTrace()
        }

     }
 }

In onReceive of my BroadcastReceiver() I always get the correct output like if the value scanned is 123 then 123 is returned in onReceive. Below is the code where in the callback method I am setting the scanned value in editText:

override fun onPackageScanned(packageId: String, barcodeType: String) {
     Timber.i ("Scanned value :: %s", scanEditText.text.toString())
     scanEditText.text.clear() // trying to clear before setting the value
     scanEditText.setText(packageId) // the value to be set on editText
}

I've tried on following devices TC25( works great), TC57,TC56 and TC26 shows same value sometimes randomly.

Any help would be great.

Thank you

2

There are 2 answers

1
localsixosix On

I would try to put a delay time to the function from one scan to other. Could be that the laserscan scan too much quickly.

You could make a function that read the text box input and if is already set the function will not make another scan.

0
Martin Zeitler On

Where that sporadic behavior may come from is difficult to answer, because you show the BroadcastReceiver, but not where or how you might register it - or them. Try to provoke the situation eg. by device orientation-change; if this accumulates the string, this is coming from multiple recievers - or multiple broadcasts. Activity.OnCreate() runs more often than once.

Proper logging would not log scanEditText.text value, which is rather secondary:

Timber.i("Scanned value :: %s", scanEditText.text.toString())

But the actual input string (where the name of the variable is a little confusing):

Timber.d("Scanned value :: %s", packageId)

You could also replace DataWedge with the EMDK, which comes with an Android Studio plugin.