NOTE: Data Capture is deprecated in EMDK-X 6.10 and later. Please refer to the Basic Scanning Tutorial instead.
Developing an EMDK for Android Application Part 3
This guide will walk you through adding MSR support and multiple profiles to the Android application you made using Developing an EMDK for Android Application Part 2. This tutorial will add some more complexity by adding a second screen as well as adding a second Data Capture Profile.
Prerequisites
- Completion of Developing an EMDK for Android Application Part 1
- Completion of Developing an EMDK for Android Application Part 2
For more information about setting up the EMDK please see the EMDK Setup.
Adding MSR Activity
Let's start by defining a second activity for the application. This activity will be used to to activate a Data Capture profile that listens for MSR data.
- Select "EMDKSample" project, right click on it and create a new Activity with the name "MSRActivity".
Adding MSR Completed Activity
Next let's create a third activity that will listen for the MSR data and display the data to the user.
- Select "EMDKSample" project, right click on it and create a new Activity with the name "MSRCompletedActivity".
Updating Main Activity
Now we will update "MainActivity", adding a button to launch our "MSRActivity".
Select "activity_main.xml" from "Package Explorer" in Android Studio.
Add the following Button to "activity_main.xml". This Button will be used for opening "MSRActivity".
<Button android:id="@+id/buttonMSR" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:layout_marginLeft="50dp" android:text="MSR" />
Select "MainActivity.java" from "Package Explorer" in Android Studio.
Add the following Imports to "MainActivity.java".
import android.widget.Button; import android.view.View; import android.view.View.OnClickListener;
Declare a variable inside "MainActivity" to store "buttonMSR".
//Declare a variable to store the buttonMSR private Button buttonMSR = null;
Inside "onCreate" get a reference to "buttonMSR".
//Declare a variable to store the buttonMSR private Button buttonMSR = null;
Inside "onCreate" add an "OnClickListener" for "buttonMSR".
//Add an OnClickListener for buttonMSR buttonMSR.setOnClickListener(buttonMSROnClick);
Add a new "OnClickListener" inside "MainActivity".
//OnClickListener for buttonMSR private OnClickListener buttonMSROnClick = new OnClickListener() { public void onClick(View v) {
};}
Add the following code to "onClick" to launch "MSRActivity".
//Launch MSRActivity Intent myIntent = new Intent(MainActivity.this, MSRActivity.class); startActivity(myIntent);
Creating MSR UI
Next we will create the UI for "MSRActivity".
Select "activity_msr.xml" from "Package Explorer" in Android Studio.
Remove the default "TextView".
Add the following TextView.
<TextView android:id="@+id/textViewInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Please swipe a card to continue." android:textAppearance="?android:attr/textAppearanceLarge" />
Creating our MSR Profile
Next we will create a Data Capture profile that will activate the MSR on "MSRActivity" and send the data via a startActivity Intent to "MSRCompletedActivity".
Select "EMDKSample" project from Package Explorer.
Click "EMDK" menu present on the top-bar and select "Profile Manager" option.
The EMDK Profile Manager Window will appear.
click "Create".
Provide "DataCaptureProfileMSR" as the Profile Name for this tutorial.
Note: You can provide any Profile Name but make sure to access it with the similar name in the Android code.
select "ActivitySelection" from the list of "Available Features" and add it to "Selected Features" using the arrow.
Note: The field "Name" contains user defined name to identify a particular feature. This is required when editing any specific feature programmatically, which is outside the scope of this tutorial. So we will keep the "Name" field empty.
Select "Activity Selection".
Enter "com.symbol.emdksample" as the application name and click apply.
Enter "MSRActivity" as the activity name and click apply.
Click OK.
select "MSR" from the list of "Available Features" and add it to "Selected Features" using the arrow.
Note: The field "Name" contains user defined name to identify a particular Data Input MSR feature. This is required when editing any specific Data Input MSR feature programmatically, which is outside the scope of this tutorial. So we will keep the "Name" field empty.
Change "MSR Input Enable" to "Enable".
select "Intent" from the list of "Available Features" and add it to "Selected Features" using the arrow.
Now we will configure the "Intent" parameters.
- Switch "Intent Output Enable" to Enable".
- For "Intent Output Action" enter "com.symbol.emdksample.RECVRMSR".
- For "Intent Output Category" enter "android.intent.category.DEFAULT".
- Switch "Intent Output Delivery" to "Send via startActivity".
- Switch "Basic data formatting Enable" to Enable".
- Switch "Basic data formatting Send Data" to Enable".
Your Intent configuration should now look like this:
Click "Apply" and "Finish".
Click "Close".
>Note:
>Now the "EMDKConfig.xml" file under the "\assets" folder will be updated with your changes.
Adding the MSR Intent Filter
Now will add an Intent filter to our Manifest file to allow "MSRCompletedActivity" to listen for our new Data Capture Intent.
Open your application's "Manifest.xml" file.
Add the following configuration to the activity "com.symbol.emdksample.MSRCompletedActivity" to revive our MSR intent.
<intent-filter> <action android:name="com.symbol.emdksample.RECVRMSR"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter>
When done, your manifest.xml should look like this:
Note:
- The intent action name should match the value of "Intent Output Action" in the EMDK Profile Manager.
- The intent category name should match the value of "Intent Output Category" in the EMDK Profile Manager.
Registering the MSR EMDK profile
Next we will register our new Data Capture profile from "MainActivity".
Select "MainActivity.java" from "Package Explorer".
Inside "MainActivity" add the following code to hold the name of our MSR profile.
//Assign the profile name used in EMDKConfig.xml for MSR handling private String profileNameMSR = "DataCaptureProfileMSR";
Inside "onOpened" add the following code to register the MSR EMDK profile.
//Call processPrfoile for profile MSR results = mProfileManager.processProfile(profileNameMSR, ProfileManager.PROFILE_FLAG.SET, modifyData); if(results.statusCode == STATUS_CODE.FAILURE) { //Failed to set profile MSR }
Creating MSR Completed UI
Now we will create the UI for "MSRCompletedActivity". This UI will allow us to display the MSR data to the user.
Select "activity_msr.xml" from "Package Explorer" in Android Studio.
Remove the default "TextView".
Add the following TextView.
<TextView android:id="@+id/textViewMSRData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Data = " android:textAppearance="?android:attr/textAppearanceMedium" />
Handling MSR Intents
Next will will add the code to "MSRCompletedActivity" for capturing the startActivity Intent and displaying the result data to the user.
Select "MSRCompletedActivity.java" from "Package Explorer".
Add the following imports.
import android.content.Intent; import android.widget.TextView;
Add the following function for processing intents.
//This function is responsible for getting the data from the intent private void handleDecodeData(Intent i) { }
Add the following code to your "onCreate" function to check for a possible intent;
//In case we have been launched by the DataWedge intent plug-in Intent i = getIntent(); handleDecodeData(i);
Overide "onNewIntent" to handle incoming intents.
//We need to handle any incoming intents, so let override the onNewIntent method @Override public void onNewIntent(Intent i) { handleDecodeData(i); }
Add a global variable for the TextView.
//Declare a variable to store the textViewMSRData private TextView textViewMSRData = null;
Add the following code to your onCreate function to get a handle on the TextView.
//Get the textViewBarcode textViewMSRData = (TextView) findViewById(R.id.textViewMSRData);
Add the following code to your "handleDecodeData" function to confirm the intent was meant for us.
//Check the intent action is for us if (i.getAction().contentEquals("com.symbol.emdksample.RECVRMSR")) { }
Add the following code to your "handleDecodeData" function to check if the intent contains MSR data.
//Get the source of the data String source = i.getStringExtra("com.motorolasolutions.emdk.datawedge.source"); //Check if the data has come from the msr if(source.equalsIgnoreCase("msr")) { }
Add the following code to your "handleDecodeData" function to retrieve MSR data.
//Get the data from the intent String data = i.getStringExtra("com.motorolasolutions.emdk.datawedge.data_string"); //Check that we have received data if(data != null && data.length() > 0) { }
Add the following code to your "handleDecodeData" function to populate the TextView with the revived MSR data.
//Display the data to textViewMSRData textViewMSRData.setText("Data = " + data);
Running the Application
Lastly we will run and test our application.
Connect the device to a USB port.
Note:
Make sure the device is in USB debug.Run the application.
Press the trigger button and scan a Barcode.
Like before the scanned data will be populated in the Edit Text field Through the previous Keystroke Intent and will appear on the Text View using the previous Datawedge Intent.
Press the button "MSR".
Swipe a cad through the MSR.
Press return to go back to the MSR where you can swipe another card, or hit return again to go back to the main screen.
What's Next
The next tutorial will show you how to use the Intent Output as a Broadcast instead of starting an activity.