Using Broadcast Intent in DataCapture Profiles

EMDK For Android 6.3

Developing an EMDK for Android Application Part 4

This guide will walk you through adding broadcast intent support to the Android application you made using Developing an EMDK for Android Application Part 3.

Prerequisites

For more information about setting up the EMDK please see the EMDK Setup.

Using EMDK Wizard and EMDK for Android Broadcast Intents.

Adding Broadcast Intent Activity

First we will add a new activity that will be used for listening and displaying Barcode data to the user.

  1. Select "EMDKSample" from "Package Explorer" in Android Studio.

  2. Right click and create a new "Empty Activity" with the name "BroadcastIntentActivity"

Updating Main Activity

Next we will update "MainActivity", adding a button to launch our "BroadcastIntentActivity".

  1. Select "activity_main.xml" from "Package Explorer" in Android Studio.

  2. Add the following Button to "activity_main.xml". This Button will be used for opening "BroadcastIntentActivity".

    
    <Button 
    android:id="@+id/buttonBroadcastIntent"  
    android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
    android:layout_alignParentBottom="true"  
    android:layout_marginBottom="50dp"  
    android:layout_marginLeft="120dp"  
    android:text="Broadcast Intent" " /> 
    

    img

  3. Select "MainActivity.java" from "Package Explorer".

    img

  4. Declare a variable inside "MainActivity" to store "buttonBroadcastIntent".

    
    //Declare a variable to store the buttonBroadcastIntent  
    private Button buttonBroadcastIntent = null;  
    

    img

  5. Inside "onCreate" get a reference to "buttonBroadcastIntent".

    
    //Get the buttonBroadcastIntent  
    buttonBroadcastIntent = (Button) findViewById(R.id.buttonBroadcastIntent); 
    

    img

  6. Inside "onCreate" add an "OnClickListener" for "buttonMSR".

    
    //Add an OnClickListener for buttonBroadcastIntent  
    buttonBroadcastIntent.setOnClickListener(buttonBroadcastIntentOnClick);  
    

    img

  7. Add a new "OnClickListener" inside "MainActivity".

    
    //OnClickListener for buttonBroadcastIntent  
    private OnClickListener buttonBroadcastIntentOnClick = new OnClickListener() {  
        public void onClick(View v) {  
    
    
    }  
    
    };

    img

  8. Add the following code to "onClick" to launch "BroadcastIntentActivity".

    
    //Launch BroadcastIntentActivity  
    Intent myIntent = new Intent(MainActivity.this, BroadcastIntentActivity.class);  
    startActivity(myIntent);  
    

    img

Creating Broadcast Intent UI

Then we will create the UI for displaying Barcode data to the user.

  1. Select "activity_msr.xml" from "Package Explorer".

  2. Remove the default "TextView".

  3. Add the following TextView.

    
    <TextView  
        android:id="@+id/textViewData"  
        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 Scan a Barcode."  
        android:textAppearance="?android:attr/textAppearanceMedium" />  
    

    img

Creating our Broadcast Intent Profile

Next will will create a Data Capture profile that will be active on "BroadcastIntentActivity" and send Barcode data using a Broadcast Intent.

  1. Select "EMDKSample" project from Package Explorer.

  2. Click "EMDK" menu present on the top-bar and select "Profile Manager" option.

  3. The EMDK Profile Manager Window will appear.

    img

  4. click "Create".

    img

  5. Provide "DataCaptureProfileBroadcastIntent" 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.

    img

  6. 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.

    img

  7. Select "Activity Selection".

    img

  8. Enter "com.symbol.emdksample" as the application name and click apply.

    img

  9. Enter "BroadcastIntentActivity" as the activity name and click apply.

    img

  10. Click OK.

  11. select "Barcode" from the list of "Available Features" and add it to "Selected Features" using the arrow.

    img

  12. Change "Barcode Scanner Input Enable" to "Enable".

    Note: The field "Name" contains user defined name to identify a particular Barcode Scanner Input feature. This is required when editing any specific Barcode Scanner Input feature programmatically, which is outside the scope of this tutorial. So we will keep the "Name" field empty.

    img

  13. select "Intent" from the list of "Available Features" and add it to "Selected Features" using the arrow.

    img

  14. Now we will configure the "Intent" parameters.

    • Switch "Intent Output Enable" to Enable".
    • For "Intent Output Action" enter "com.symbol.emdksample.RECVRBI".
    • Switch "Intent Output Delivery" to "Broadcast Intent".
    • Switch "Basic data formatting Enable" to Enable".
    • Switch "Basic data formatting Send Data" to Enable".

    Your Intent configuration should now look like this:

    img

  15. Click "Apply" and "Finish".

    img

  16. Click "Close".
    >Note:
    >Now the "EMDKConfig.xml" file under the "\assets" folder will be updated with your changes.

Registering for the Broadcast Intent profile

Now will register our new Data Capture profile in "MainActivity".

  1. Select "MainActivity.java" from "Package Explorer".

    img

  2. Inside "MainActivity" add the following code to hold the name of our Broadcast Intent profile.

    
    //Assign the profile name used in EMDKConfig.xml  for Broadcast Intent handling  
    private String profileNameBroadcastIntent = "DataCaptureProfileBroadcastIntent";
    

    img

  3. Inside "onOpened" add the following code to register the Broadcast Intent EMDK profile.

    
    //Call processPrfoile for profile Broadcast Intent.  
    results = mProfileManager.processProfile(profileNameBroadcastIntent, ProfileManager.PROFILE_FLAG.SET, modifyData);  
    
    if(results.statusCode == STATUS_CODE.FAILURE)  
    {  
    //Failed to set profile  
    }  
    

    img

Handling Broadcast Intents

Newt we will add the code to listen for our Broadcast Intent and display the Barcode data to the user in side "BroadcastIntentActivity".

  1. Select "BroadcastIntentActivity.java" from "Package Explorer".

    img

  2. Add the following imports.

    
    import android.content.BroadcastReceiver;  
    import android.content.Context;  
    import android.content.Intent;  
    import android.content.IntentFilter;  
    import android.widget.TextView;  
    

    img

  3. Add a global variable for the TextView.

    
    //Declare a variable to store the textViewData  
    private TextView textViewData = null;  
    

    img

  4. Add the following code to your onCreate function to get a handle on the TextView.

    
    //Get the textViewData  
    textViewData = (TextView) findViewById(R.id.textViewData);  
    

    img

  5. Add a global variable to BroadcastIntentActivity to hold our Broadcast Receiver.

    
    //Declare a variable to store our Broadcast Receiver.  
    private BroadcastReceiver EMDKReceiver;  
    

    img

  6. Override "onResume".

    
    @Override  
    protected void onResume() {  
    // TODO Auto-generated method stub  
        super.onResume();  
    } 
    

    img

  7. Override "onPause".

    
    @Override  
    protected void onPause() {  
        // TODO Auto-generated method stub  
        super.onPause();  
    } 
    

    img

  8. Add the following code to "onResume" to create an Intent filter.

    
    //Create an Intent Filter  
    IntentFilter intentFilter = new IntentFilter("com.symbol.emdksample.RECVRBI");
    

    img

  9. Add the following code to "onResume" to create a Broadcast Receiver.

    
    //Create a our Broadcast Receiver.  
    EMDKReceiver = new BroadcastReceiver() {  
    
    };  
    

    img

  10. Override "onReceive" inside the Broadcast Receiver.

    
    @Override  
    public void onReceive(Context context, Intent intent) {  
    
    } 
    

    img

  11. Add the following code to "onReceive" to check if the data is coming from the Barcode scanner.

    
    //Get the source of the data  
    String source = intent.getStringExtra("com.motorolasolutions.emdk.datawedge.source");  
    
    //Check if the data has come from the barcode scanner  
    if(source.equalsIgnoreCase("scanner")){  
    
    } 
    

    img

  12. Add the following code to get the data from the intent.

    
    //Get the data from the intent  
    String data = intent.getStringExtra("com.motorolasolutions.emdk.datawedge.data_string");  
    
    //Check that we have received data  
    if(data != null && data.length() > 0){  
    
    }
    

    img

  13. Add the following code to display the data to the TextView.

    
    //Display the data to the text view  
    textViewData.setText("Data = " + data);  
    

    img

  14. Add the following code to "onResume" to register our receiver.

    
    //Register our receiver.
    this.registerReceiver(EMDKReceiver, intentFilter);
    

    img

  15. Add the following code to "onPause" to unregister our receiver.

    
    //Register our receiver.
    this.registerReceiver(EMDKReceiver, intentFilter);
    

    img

Running the Application

Lastly we will run and test our application.

  1. Connect the device (having the latest EMDK runtime) to USB port.

    Note:
    Make sure the device is in USB debug.

  2. Run the application.

    img

  3. Press the trigger button and scan a Barcode.

    img

  4. Like before the scanned data will be populated in the Edit Text field Through the previous Keystroke Intent and will appear on the TextView using the previous Datawedge Intent.

  5. Press the button "Broadcast Intent".

    img

  6. Scan a Barcode.

    img

  7. The TextView will be populated by the Broadcast Intent.

  8. Press the back button to rerun to the main screen.

    img

  9. Press the button "MSR", like before the MSR screen will come up.

    img

  10. Swipe a cad through the MSR.

    img

  11. 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.