Search code examples
androidkotlinbarcode-scanner

Using DataWedge for Multiple Activities on Zebra Barcode Scanner doesn't work in Kotlin


I am developing an app for Zebra Barcode Scanner in Kotlin and I need to scan Barcode on multiple activities. At this moment I am trying to use DataWedge. I followed this tutorial: https://github.com/darryncampbell/DataWedge-GettingStarted-Samples which is for one Activity great. But the problem is with multiple activities. My idea was to create DatWedge profile in Main Activity and then I need to scan Barcodes in Second and Third activities (third activity is same as second). Is it even possible? When I press the hard button device beeps but nothing is shown in TextView even Logs doesn't work.

I just use Hard trigger so it is not need to use Soft scanning. This is MainActivity. DWUtilities object is the same as in tutorial.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        DWUtilities.CreateDWProfile(this)
        val btnScan = findViewById<Button>(R.id.btnScan)
        btnScan.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }

This is SecondActivity. Activity contains only button and textView.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        val btnSec = findViewById<Button>(R.id.btnScanSecond)
        btnSec.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }
    }
    override fun onNewIntent(intentSken: Intent) {
        super.onNewIntent(intentSken)
        displayScanResult(intentSken)
    }
    private fun displayScanResult(scanIntent: Intent) {
        val decodedSource =
            scanIntent.getStringExtra(resources.getString(R.string.datawedge_intent_key_source))
        val decodedData =
            scanIntent.getStringExtra(resources.getString(R.string.datawedge_intent_key_data))
        val decodedLabelType =            scanIntent.getStringExtra(resources.getString(R.string.datawedge_intent_key_label_type))
        val scan = "$decodedData [$decodedLabelType]\n\n"
        val output = findViewById<TextView>(R.id.txtOutputSecond)        
        output.text = scan + output.text
        Log.d("Scan", "$scan")
    }

This is manifest. Actually I think there should be problem with this manifest but I am not sure.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ThirdActivity"
            android:exported="false"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="com.darryncampbell.datawedge.kotlin.ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:exported="false"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="com.darryncampbell.datawedge.kotlin.ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Solution

  • In the tutorial you posted, this line: https://github.com/darryncampbell/DataWedge-GettingStarted-Samples/blob/master/Kotlin/app/src/main/java/com/darryncampbell/dwgettingstartedkotlin/DWUtilities.kt#L40 causes the DataWedge profile to apply to ALL activities in your application. You can specify an individual activity here to only apply the settings to a single activity, com.darryncampbell.dwgettingstartedkotlin.MainActivity. You could then define multiple DataWedge profiles, so each activity in your app could have its own profile with its own settings.

    The other consideration is how you have configured the Intent output plugin, the tutorial sends it as startActivity. You could use different intent filters in each of your activities, each registering for a different action or you could change the profile to invoke sendBroadcast and have a broadcast receiver in each of your activities which will receive scans.