DWDemo Sample - How to capture DataWedge output in your Android application
DataWedge on Android has the ability to output captured data via Intents.
This article describes how to receive and use intents from DataWedge in your Android application.
Knowledge of Android programming and familiarity with the Android intent mechanism are assumed. Also assumed is some familarity with DataWedge and that you have read the DataWedge section in the Integrator Guide for your device.
For this article we created an Android application with a blank activity using Eclipse, to which we added the following layout.
The things to note in the above layout are the ImageView and the EditText widgets. The ImageView widget will effectively act as a button which can be used to toggle the scanner instead of pressing the trigger button on the device. The EditText widget will be used to display the data that we receive in the intent.
The intent we will received from DataWedge will contain a bundle of data which we will be retrieving using the Intent.getStringtExtra() and Intent.getSerializableExtra() methods. The following is a list of the data item names that the bundle could contain.
- String SOURCE_TAG = "com.motorolasolutions.emdk.datawedge.source";
- String contains the source of the data i.e. scanner or MSR
- String DATA_STRING_TAG = "com.motorolasolutions.emdk.datawedge.data_string";
- String contains the output data as a String. In the case of concatenated bar codes, the decode data is concatenated and sent out as a single string. In the case of MSR output, the data from the MSR tracks is concatenated and sent out as a single string.
- String LABEL_TYPE_TAG = "com.motorolasolutions.emdk.datawedge.label_type";
- String contains the label type of the bar code.
- String DECODE_DATA_TAG = "com.motorolasolutions.emdk.datawedge.decode_data";
- Decode data returned as a list of byte arrays. In most cases there will be one byte array per decode. For barcode symbologies that support concatenation e.g. Codabar, Code128, MicroPDF, etc., the decoded data is stored in multiple byte arrays (one byte array per bar code). Clients can get data in each byte array by passing an index.
Let’s list the MSR intent names (in case we want to use these in the future).
The MSR related data added to the intent’s bundle can be retrieved using the Intent.getStringtExtra() and Intent.getSerializableExtra() calls, using the following String tags…
- String MSR_DATA_TAG = "com.motorolasolutions.emdk.datawedge.msr_data";
- The data from the MSR tracks is concatenated and sent out as a byte array.
- String MSR_TRACK1_TAG = "com.motorolasolutions.emdk.datawedge.msr_track1";
- MSR track 1 data is returned as a byte array.
- String MSR_TRACK2_TAG = "com.motorolasolutions.emdk.datawedge.msr_track2";
- MSR track 2 data is returned as a byte array.
- String MSR_TRACK3_TAG = "com.motorolasolutions.emdk.datawedge.msr_track3";
- MSR track 3 data is returned as a byte array.
- String MSR_TRACK1_STATUS_TAG = "com.motorolasolutions.emdk.datawedge.msr_track1_status";
- MSR track 1 decode status as an Integer where 0 indicates a successful decode.
- String MSR_TRACK2_STATUS_TAG = "com.motorolasolutions.emdk.datawedge.msr_track2_status";
- MSR track 2 decode status as an Integer where 0 indicates a successful decode.
- String MSR_TRACK3_STATUS_TAG = "com.motorolasolutions.emdk.datawedge.msr_track3_status";
- MSR track 3 decode status as an Integer where 0 indicates a successful decode.
- String MSR_TRACK1_ENCRYPTED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track1_encrypted";
- MSR track 1 encrypted data is returned as a byte array.
- String MSR_TRACK2_ENCRYPTED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track2_encrypted";
- MSR track 2 encrypted data is returned as a byte array.
- String MSR_TRACK3_ENCRYPTED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track3_encrypted";
- MSR track 3 encrypted data is returned as a byte array.
- String MSR_TRACK1_HASHED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track1_hashed";
- MSR track 1 hashed data is returned as a byte array.
- String MSR_TRACK2_HASHED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track2_hashed";
- MSR track 2 hashed data is returned as a byte array.
- String MSR_TRACK3_HASHED_TAG = "com.motorolasolutions.emdk.datawedge.msr_track3_hashed";
- MSR track 3 hashed data is returned as a byte array.
Here are the API intent names for the soft scan trigger…
- String ACTION_SOFTSCANTRIGGER = "com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER";
- This is the intent action for the soft scan trigger.
- String EXTRA_PARAM = "com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER";
- Parameter passed in the intent to specify the action the soft scan trigger should perform. The following actions are recognized…
- String DWAPI_START_SCANNING = "START_SCANNING";
- Start scanning.
- String DWAPI_STOP_SCANNING = "STOP_SCANNING";
- Stop scanning.
- String DWAPI_TOGGLE_SCANNING = "TOGGLE_SCANNING";
- Toggle scanning.
In the code that follows we will just be using the SOURCE_TAG, DATA_STRING_TAG and LABEL_TYPE_TAG intent data to display the intent data in the EditTextwidget.
Incoming intents can be handled by overriding the onNewIntent method.
Our handleDecodeData() method will responsible for getting the data from the intent, formatting it and adding it to the end of the edit box; something like this…
TOGGLING THE SCAN TRIGGER
Our activity layout has an ImageView which we will use as a button to toggle the scan trigger. This is done by sending an intent to DataWedge. The following code toggles the scan trigger each time the image is tapped.
PUTTING ALL THE CODE TOGETHER
Let’s put all that code together so that we can see what our final activity class should look like.
DEFINING INTENT FILTERS
Having dealt with the coding of our activity, we now need to inform the system which implicit intents our application can handle. For that we need to define an intent-filteras follows.
We will be using the com.motorolasolutions.emdk.sample.dwdemosample.RECVR action and the android.intent.category.DEFAULT category we have just defined above later on when we come to configure our DataWedge profile.
In this acticle DataWedge is configured to use the startActivity() method to send the intent. The startActivity() method causes a new activity to be launched. However since we do not want another instance of our activity launched each time we receive an intent, we need to specify the launch mode of our activity as single task.
After adding the above, our AndroidManifest.xml should look something like this…
Now we need to configure DataWedge to send the desired intent to our application.
The following steps will help you get started…
- Launch DataWedge
- Create a new profile and give it a name such as "dwdemosample"
- Edit the profile
- Go into Associated apps, tap the menu button, and add a new app/activity
- For the application select com.motorolasolutions.emdk.sample.dwdemosample
- For the activity select com.motorolasolutions.emdk.sample.dwdemosample.MainActivty
- Go back and disable the keystroke output plug-in
- Enable the intent output plug-in
- For the intent action enter com.motorolasolutions.emdk.sample.dwdemosample.RECVR
- For the intent category enter android.intent.category.DEFAULT
That’s it. You should now be able to scan a barcode and see it appear in the sample application.
In this arcticle, we covered…
- DataWedge intent strings
- Handling intents from DataWedge
- Receiving barcode/MSR data
- Sending intents to DataWedge
- Toggling of the soft scan trigger
- Defining intent filters
- Configuring DataWedge to send intents to our application