0

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

nethra gowda
  • 290
  • 1
  • 4
  • 23
  • I'd guess, that you might register the `BroadcastReceiver` twice, which may lead to a race condition. – Martin Zeitler May 14 '22 at 09:26
  • No the BroadcastReceiver is registered only once, I've printed the logs so if at all it was registered twice then I would know in logs. – nethra gowda May 14 '22 at 10:26
  • When you write "is shown twice randomly" ...suggests that it not always log twice. – Martin Zeitler May 14 '22 at 13:32
  • Yes but the problem is with editText, the laser scan reading the data correctly even in onReceive it shows the correct value only when setting the value in editText it appears twice. – nethra gowda May 14 '22 at 15:38

2 Answers2

0

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.

  • Yes laserscan is too fast and setting a delay didn't help. The problem is whatever value that I scan is being read twice even if am repeating the scan on same value or different value is not the case, randomly when I scan any value that is being read twice. – nethra gowda May 14 '22 at 10:37
0

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216