Tutorial: Scanning with DataWedge and Xamarin

This tutorial will take you through scanning barcode data via Android Intents using DataWedge on Zebra devices with a Xamarin application.

Be aware that there is a dedicated Xamarin component for Zebra mobile devices ​providing API access through an SDK but the DataWedge service is sufficient for most applications and is simpler to use to get barcode data into your app.  This tutorial describes using DataWedge, not the Nuget package.

DataWedge configuration

The first step is to configure the DataWedge service with an INPUT (the barcode scanner) and an OUTPUT (send an Intent)

Launch the DataWedge application on the device, there is no need to install anything as it comes pre-installed on all Zebra Android mobile devices.

Select the profile which will be associated with your application, unless you configure a separate profile then DataWedge will use “Profile0 (default)”

Ensure:

  • The profile is enabled
  • Barcode input is enabled
  • Intent output is enabled
  • All other inputs and outputs can be disabled.

Configure the Intent Output as follows (as shown in the screenshot below):

  • Intent action: This is an implicit intent that will be sent by DataWedge, it is up to your application to ensure it is configured to receive this intent.  For the purposes of this tutorial, specify com.dwexample.ACTION.
  • Intent category: The category that is associated with the intent sent by DataWedge following each scan.  Leave this blank for this tutorial.
  • Intent delivery, One of

For the tutorial, your Intent output should match the screenshot below.

datawedge_settings.png

The application

Moving over to the application now, there are several key parts to ensure we are able to receive the intent data that DataWedge is sending.

First, we can pre-define some of the strings to make it easier to receive and extract the scanned data.  The intent’s action is defined as “com.dwexample.ACTION” and once we receive an intent it will contain extras representing the scanned data for source, type and data as listed in the official docs.

<?xml version="1.0" encoding="utf-8"?>

<resources>

  <string name="app_name">Datawedge Intent Example 3</string>

  <string name="activity_intent_filter_action">

        com.dwexample.ACTION</string>

  <string name="datawedge_intent_key_source">

        com.symbol.datawedge.source</string>

  <string name="datawedge_intent_key_label_type">

        com.symbol.datawedge.label_type</string>

  <string name="datawedge_intent_key_data">

        com.symbol.datawedge.data_string</string>

</resources>

Because we configured DataWedge to send a broadcast intent our application must now register a broadcast receiver.

Obviously if we had configured DW to start an activity we could register for that in our manifest and call getIntent() in onCreate() or if we had configured it as start service we could create a service to receive the intent.

For this example we will register a dynamic broadcast receiver in the onCreate() call of our application.  If you were doing this in a production application you would more likely register / unregister in the onResume() / onPause() methods.

Note that the action we are filtering on matches the action that we configured the DataWedge service to send, com.dwexample.ACTION.

protected override void OnResume()

{

    base.OnResume();

    //  Register the broadcast receiver dynamically

    RegisterReceiver(receiver,

        new IntentFilter(Resources.GetString(

        Resource.String.activity_intent_filter_action)));

}

Having registered a broadcast receiver we had better define one.  You could do this in a separate class but for this tutorial I will just define a receiver within the MainActivity.cs

//  Broadcast receiver to receive our scanned data from Datawedge

[BroadcastReceiver(Enabled = true)]

public class myBroadcastReceiver : BroadcastReceiver

{

    public override void OnReceive(Context context, Intent intent)

    {

        String action = intent.Action;

        if (action.Equals(MainActivity.Instance.Resources.GetString(

            Resource.String.activity_intent_filter_action)))

        {

            //  A barcode has been scanned

            MainActivity.Instance.RunOnUiThread(() =>

                MainActivity.Instance.DisplayResult(intent));

        }

    }

}

Plumb together the broadcast receiver and the main activity in the onCreate() method using an instance variable

//  Instance used to communicate from broadcast receiver back to main activity

public static MainActivity Instance;

myBroadcastReceiver receiver;

protected override void OnCreate(Bundle savedInstanceState)

{

    base.OnCreate(savedInstanceState);

    MainActivity.Instance = this;

    receiver = new myBroadcastReceiver();

    // Set our view from the "main" layout resource

    SetContentView(Resource.Layout.Main);

}

The logic of extracting the scanned data and displaying it on the screen is handled by its own method.  Note that the extra keys were defined earlier in the strings.xml file.  The below code assumes a UI exists in which to place the data but a production application will be driving a lot of its business logic following a scan.

public void DisplayResult(Intent intent)

{

    //  Output the scanned barcode on the screen.  Bear in mind older JB devices will use the legacy DW parameters on unbranded devices.

    String decodedSource = intent.GetStringExtra(Resources.GetString(

        Resource.String.datawedge_intent_key_source));

    String decodedData = intent.GetStringExtra(Resources.GetString(

        Resource.String.datawedge_intent_key_data));

    String decodedLabelType = intent.GetStringExtra(Resources.GetString(

        Resource.String.datawedge_intent_key_label_type));

    TextView scanSourceTxt = FindViewById<TextView>

        (Resource.Id.txtScanScource);

    TextView scanDataTxt = FindViewById<TextView>

        (Resource.Id.txtScanData);

    TextView scanLabelTypeTxt = FindViewById<TextView>

        (Resource.Id.txtScanDecoder);

    scanSourceTxt.Text = "Scan Source: " + decodedSource;

    scanDataTxt.Text = "Scan Data: " + decodedData;

    scanLabelTypeTxt.Text = "Scan Decoder: " + decodedLabelType;

}

Those are all of the key points to receive scanned data via intent, the flexibility of both Android and DataWedge allow for many other possible configurations for receiving data but the above should be a good jumping off point.

There is a sample application that accompanies this tutorial and shows all the code, available on github

2.png

dex bex

Looks like you dont support Honeywell Xenon 1900 scanner either although these are looking very similar?

Thank you


Anonymous (not verified)

We only support Xamarin at all for our mobile computer and printing products.  For mobile computing, we only support Android, for Printing we validate on iOS and Android.  I believe you are developing for a dedicated barcode scanner​ however in which case Xamarin is not supported and they only offer native SDKs for Android, iOS & Windows (full, not UWP).  Which model of Zebra scanner are you targeting?  Thanks. 


dex bex

Why do you call it Xamarin Crossplatform if you support only Android?  Are you planning or do you support Xamarin.Forms (Android, iOS, and UWP) crossplatform projects?  Where do I find relevant information to implement barcode scanning with external wire-connected barcode scanners to tablets or phones connected to them by wire?


Anonymous (not verified)

Hi, right now we only offer Xamarin APIs for our mobile computer and printing products.  I am not aware of any current plans to extend Xamarin APIs into our dedicated barcode scanner portfolio.  It looks they just redesigned the website over the weekend but you can find the software for Barcode scanners here​, there are links to SDKs for Android & iOS.


Robin West

Also, I understand you support only your own devices, thanks for clarifying that.

But do you support Xamarin.Forms with .NETStandard 2.0 for all 3 platforms (IOS, Android, and UWP)?

Or if not, which one of these 3 do you support?

Do you support these with handheld barcode readers that are attached through the wire (USB)?

Thanks


dex bex

Hi Darryl and thanks for your answers.  I am confused when you say you "only support Xamarin at all for our mobile computer and printing products.  For mobile computing, we only support Android".  It is kind of confusing "mobile computer" supports all Xamarin whereas "mobile computing" supports only Android?

I am not interested in mobile computers (which I understand are mobile devices with build in scanners such as MC45).  I am interested in handheld barcode readers such as this one https://www.barcodesinc.ca/catalogsearch/result/?q=Honeywell+Xenon+1900+&cat=&gclid=CjwKCAjwkYDbBRB6EiwAR0T_-mc4ot1aMKWqtPRrGkAdgMgdmgw8RKKG9xXMns06XodgyNPa3xyNmBoCwzwQAvD_BwE&gclsrc=aw.dshttp:// which looks very similar to your DS-series dedicated barcode scanners.

You are saying "I believe you are developing for a dedicated barcode scanner however in which case Xamarin is not supported and they only offer native SDKs for Android, iOS & Windows (full, not UWP)".  Yes, it is dedicated bc scanner.  We might be considering going with Zebra but I cannot find any example showing how to use these SDKs for Android, iOS and potentially UWP in a Xamarin.Forms project compatible with .NET Standard 2.0.

I am working in cross-platform environment which Xamarin.Forms is (also React-Native) and the last statement is really 3 questions:

(a) I understand you dont support Xamarin at all but only native Android, iOS SDKs.  However, Xamarin.Forms is a cross-platform environment supporting Android, iOS, and UWP head projects and one could add native libraries to these.  Do you have an example to show how your native libraries can be used in Xamarin.Forms with .NET Standard 2.0?

(b) Any plans to support Xamarin.Forms .NET Standard 2.0?

(c) Plans to support Xamarin.Forms including UWP?

Thank you


Anonymous (not verified)

(a) I understand you dont support Xamarin at all but only native Android, iOS SDKs.  However, Xamarin.Forms is a cross-platform environment supporting Android, iOS, and UWP head projects and one could add native libraries to these.  Do you have an example to show how your native libraries can be used in Xamarin.Forms with .NET Standard 2.0?

I am afraid there are none that I am aware of.

(b) Any plans to support Xamarin.Forms .NET Standard 2.0?

None that I am aware of, I recommend you contact Zebra technical support as they would be able to route your question appropriately.

(c) Plans to support Xamarin.Forms including UWP?

Same answer as b I'm afraid.

Apologies I can't offer specific answers but I am not aware of the public barcode scanner roadmap.


Amit Sarathe

I have downloaded the sample app from following URL

https://github.com/darryncampbell/DataWedge-Intent-Example-3

The same solution is working fine with TC57 but not working on ET 56 tablet. The ET56 tablet have USB scanner DS3608 connected. Scanner is working if we focus on any text field and scan. My requirement is to scan barcode in a label like the way it works in the sample application. Do I need to do additional settings for ET 56 as it has USB scanner connected?