This is an installment of Barcode Scanning With Microsoft Dynamics Mobile series.

This article is targeted at developers who want to start using the barcode scanner capabilities of Microsoft Dynamics Mobile 1.5 framework. It supports Motorola (formerly Symbol) and Intermec devices. After you have read this article and followed its tutorial part you will be able to implement your own tasklets with barcode scanning capabilities. It might seem a long way on first glance but once done you will realize that the heart of the functionality is doable in just a few steps.

Barcode Scanner Sample Barcode Scanner Demo Tasklet

The following tutorial describes all steps necessary to create a simple Rolepad based application which enables barcode scanning on Motorola (Symbol) and Intermec devices. The process is the same for both device classes – there are only two differences: The first one is proprietary .NET CF scanning assemblies which you must download and reference within your project and the second one is the corresponding section in the App.config file. Tutorial is written for Symbol, differences are highlighted for Intermec.

  1. Prerequisites: Microsoft Visual Studio 2008 SP1 (Professional edition or above), Microsoft Dynamics Mobile – Device Components with Device SDK feature installed
  2. We are going to create one VS solution with two projects based on Microsoft Dynamics Mobile 1.5 platform. The main purpose of the Rolepad project called BarcodeScannerSample15 is an easy deployment of all necessary binaries configuration files. The barcode scanning logic is being coded in the separated tasklet project called BarcodeScanner.
  3. Open Visual Studio 2008 (Professional edition or above).
  4. Create a Rolepad project called BarcodeScannerSample15: File > New > Project… > Project types > Visual C# > Dynamics Mobile > Rolepad project for Windows Mobile 6 (or Windows Mobile 5 if you wish).
  5. Add a new tasklet project called BarcodeScanner to the BarcodeScannerSample15 solution: Right-click on BarcodeScannerSample15 solution > Add > New Project… > Project types > Visual C# > Dynamics Mobile > Tasklet project for Windows Mobile 6 (or 5).
  6. BarcodeScanner assembly should be deployed automatically when deploying BarcodeScannerSample15 project: Add reference of the BarcodeScanner project to the BarcodeScannerSample15 project.
  7. Add following references to the BarcodeScannerSample15 project:
      Microsoft.Dynamics.Mobile.Components
      Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol
      (Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.SymbolIntermec)
      Microsoft.Dynamics.Mobile.Components.Tasklets.Menu
  8. The scanning itself is a manufacturer-specific operation, so manufacturer-specific assemblies will be needed. You have to download and extract the following assemblies: Symbol and Symbol.Barcode for Motorola (Symbol) devices and Intermec.DataCollection.CF2 for Intermec. Read the article Where to get barcode drivers to figure out where and how to get these.
  9. Open Windows Explorer and locate BarcodeScannerSample15 project folder, create BinaryReferences subfolder and copy extracted assemblies under this folder. Reference these binaries from the BarcodeScannerSample15 project.
  10. The core of barcode reader functionality provided by Microsoft Dynamics Framework is located in Microsoft.Dynamics.Mobile.Runtime assembly. Add following reference to the BarcodeScanner (tasklet) project:
      Microsoft.Dynamics.Mobile.Framework.Runtime
  11. Open BarcodeScannerTasklet.cs and add following using statements:
      using Microsoft.Dynamics.Mobile.Framework.Entities;
      using Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning;
      using Microsoft.Dynamics.Mobile.Components.Services;
  12. We will need a BarcodeReader object to perform the actual scanning. This object will be created by BarcodeScanningService defined in the App.config file. The service assures that a correct version of BarcodeReader object will be newed-up.
  13. Add BarcodeReader private field called barcodeReader.
      BarcodeReader barcodeReader;
  14. Add BarcodeScanningService public property called BarcodeScanningService and decorate it with [RolePadService] attribute.
      [RolePadService]
      public BarcodeScanningService BarcodeScanningService { get; set; }
  15. Edit OnStarted method. You must ask BarcodeScanningService to create particular BarcodeReader object and implement two event handlers with self-explaining names. This code will be called only once within the tasklet lifetime.
      protected override void OnStarted()
      {
          base.OnActivated();
       
          barcodeReader = BarcodeScanningService.GetReader(this);
       
          barcodeReader.Scanned += new EventHandler<ScannedDataEventArgs>(barcodeReader_Scanned);
          barcodeReader.Error += new EventHandler<ErrorEventArgs>(barcodeReader_Error);
      }
  16. Override OnActivate method. The scanner must be opened (enabled). This code can be called repeatedly when entering already started tasklet.
      protected override void OnActivated()
      {
          base.OnActivated();
       
          barcodeReader.Open();
      }
  17. Override OnDeactivated method. The scanner will be closed (disabled). Counterpart of the OnActivate method – can be called repeatedly.
      protected override void OnDeactivated ()
      {
          base. OnDeactivated ();
       
          barcodeReader.Close();
      }
  18. Override OnClosing method. Closing the system resources and bit of cleaning.
      protected override void OnClosing(ExitResult exitResult)
      {
          base.OnClosing(exitResult);
       
          barcodeReader.Error -= barcodeReader_Error;
          barcodeReader.Scanned -= barcodeReader_Scanned;
       
          barcodeReader.Dispose();
      }
  19. Add barcodeReader_Error event handler.
      void barcodeReader_Error(object sender, ErrorEventArgs e)
      {
          if (e != null && e.Exception != null)
              Container.Alert("Exception ocured while using barcode scanner:\n" + e.Exception.Message);
      }
  20. Add barcodeReader_Scanned event handler. In case the scanning was successful tasklet view class is going to present the scanned data. Some barcode readers return multiple lines of the scanned information. Typically we need to know only the first one – that is why we pass 0 index of e.Data array to the view.
      void barcodeReader_Scanned(object sender, ScannedDataEventArgs e)
      {
          if (e != null && e.Data != null && e.Data.Length > 0)
              view.ShowScannedBarcode(e.Data[0]);
      }
  21. You are done with the tasklet itself but we have to extend its view.
  22. Double-click BarcodeScannerView.cs and add one TextBox control called txtScannedBarcodes.
  23. Make txtScannedBarcodes control ReadOnly, Multiline and change its size to your convenience.
  24. Open IBarcodeScannerView.cs and add extend the interface by:
      void ShowScannedBarcode(string barcode);
  25. Right-click BarcodeScannerView.cs and select View Code.
  26. Implement IBarcodeScannerView by the following code:
      public void ShowScannedBarcode(string barcode)
      {
          this.txtScannedBarcodes.Text += barcode + Environment.NewLine;
      }
  27. You should be able to build BarcodeScanner tasklet project by now so let us finish the BarcodeScannerSample15 project.
  28. Modify app.config by adding of the following code at the beginning of the <services> node. The simple configuration informs Framework that it should create BarcodeScanningService of an appropriate type. Please notice that you must comment out/in the correct section for Intermec devices:
      <!-- Required for Barcode scanning on Symbol devices-->
      <add type="Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanningService, Microsoft.Dynamics.Mobile.Framework.Runtime">
          <configuration xmlns="http://schemas.microsoft.com/Dynamics/Mobile/2008/11/Services/BarcodeScanning/Configuration">
              <scannerType>Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol.ScannerDevice, Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol</scannerType>
          </configuration>
      </add>
       
      <!-- Required for Barcode scanning on Intermec devices-->
      <!--<add type="Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanningService, Microsoft.Dynamics.Mobile.Framework.Runtime">
              <configuration xmlns="http://schemas.microsoft.com/Dynamics/Mobile/2008/11/Services/BarcodeScanning/Configuration">
                  <scannerType>Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Intermec.ScannerDevice, Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Intermec</scannerType>
              </configuration>
      </add>-->
  29. Modify UserRole.xml – replace its whole content by the following code. It is a very simple orchestration with two tasklets: Menu tasklet provided by the Framework as an invitation screen and our newly created BarcodeScanner tasklet.
      <?xml version="1.0" encoding="utf-8" ?>
      <userRole minimize="false" xmlns="http://schemas.microsoft.com/Dynamics/Mobile/2008/11/Flow">
          <orchestrations>
              <orchestration name="startup">
                  <tasklets>
                      <!-- Main Menu (Menu) -->
                      <tasklet name="MenuTasklet" icon="Graphics\Dynamics_small.png" text=" Barcode Scanner Sample" context="Tasklet header;26" type="Microsoft.Dynamics.Mobile.Components.Tasklets.MenuTasklet, Microsoft.Dynamics.Mobile.Components.Tasklets.Menu">
                          <actions>
                              <group name="BackAndCancel" type="Flat" priority="98">
                                  <exitOrchestration name="Close" text="Close" />
                              </group>
                              <group name="MenuItems" priority="50" type="Flat">
                                  <open text="Barcode" icon="Graphics\Barcode_large.png" priority="49" tasklet="BarcodeScanner" />
                              </group>
                          </actions>
                      </tasklet>
                      <!-- /Main menu (Menu) -->        
       
                      <!-- This is a demo tasklet which shows how to implement barcode scanning functionality. -->
                      <tasklet name="BarcodeScanner" icon="Graphics\Barcode_small.png"  type="BarcodeScanner.BarcodeScannerTasklet, BarcodeScanner" >
                          <actions>
                              <exitTasklet text="Close" />
                          </actions>
                      </tasklet>
                  </tasklets>
              </orchestration>
          </orchestrations>
      </userRole>
  30. You should be able to build the whole solution at this moment.
  31. Double check that BarcodeScannerSample15 is your startup project. If it is not the case then right-click on BarcodeScannerSample15 project node and select “Set as StartUp Project”.
  32. Right-click Properties subfolder of BarcodeScannerSample15 project and select Open.
  33. Go to Devices and select your target device.
  34. Connect the device and press F5.

Do not forget to check MSDN documentation: BarcodeScanningService Class

By the way: The complete source code can be downloaded – see the bottom of this article. However I am sure you are glad you went through the whole example by yourself - it is the best way how to learn the stuff.

Please notice that the downloadable code is more complex and much better commented than the example you created based on the tutorial above. The difference is mainly given by more extensive error handling in the downloadable code. The tutorial code should be used for tutoring/learning only, the downloadable example on the other hand rather as a skeleton for building real applications based on 1.5 version of the Framework.

Download source code here: BarcodeScannerSample15.zip

This code will run first after you copy barcode drivers into the .\BarcodeScannerSample15\BinaryReferences folder.