Hello RFID MAUI Application

RFID SDK For MAUI

Overview

This guide provides a walk-through of the steps for creating an application that uses MAUI RFID SDK to perform RFID operations

Note: The demo app in this guide is intended for tutorial purposes only and should not be used in production environments.


Prerequisites

  1. Developer machine with latest Visual studio 2022 with MAUI Support

Create the Project

Start by creating a new project in Visual Studio. Call it HelloRFID to match later references in this guide. For help, see the Create the Project tutorial

This project has been created with Shared Projectcode sharing strategy

Adding essentials

Modify the application's MainPage.xaml.cs file to use the MAUI RFID SDK library.

  1. Declare RFID readers fields:

    
    private static Readers readers;
    private static IList availableRFIDReaderList;
    private static ReaderDevice readerDevice;
    private static RFIDReader Reader;
    private EventHandler eventHandler;
    
  2. Create SDK Readers instance

    
    // SDK
    if (readers == null)
    {
        readers = new Readers(Android.App.Application.Context, ENUM_TRANSPORT.ServiceSerial);
    }
    GetAvailableReaders();

    Note : For USB instance

    
     readers = new Readers(this.context, ENUM_TRANSPORT.ServiceUsb);
        

    Note : For Bluetooth instance

    
    readers = new Readers(this.context, ENUM_TRANSPORT.Bluetooth);
        

  3. Create ThreadPool.QueueUserWorkItem to retrieve available readers using AvailableRFIDReaderList

    
    private void GetAvailableReaders()
    {
        ThreadPool.QueueUserWorkItem(o =>
        {
            try
            {
                if (readers != null && readers.AvailableRFIDReaderList != null)
                {
                    availableRFIDReaderList =
                        readers.AvailableRFIDReaderList;
                    if (availableRFIDReaderList.Count > 0)
                    {
                        if (Reader == null)
                        {
                            // get first reader from list
                            readerDevice = availableRFIDReaderList[0];
                            Reader = readerDevice.RFIDReader;
                            // Establish connection to the RFID Reader
                            Reader.Connect();
                            if (Reader.IsConnected)
                            {
                                Console.Out.WriteLine("Reader connected");
                                Status = "Reader connected";
                                ConfigureReader();
                            }
    
                        }
                    }
                }
            }
            catch (InvalidUsageException e)
            {
                e.PrintStackTrace();
            }
            catch (OperationFailureException e)
            {
                e.PrintStackTrace();
                Console.Out.WriteLine("OperationFailureException " + e.VendorMessage);
                Status = "OperationFailureException " + e.VendorMessage;
            }
        });
    }
    
  4. Add ConfigureReader method to configure the reader for trigger type and adding event handler / listener AddEventsListener

    
    private void ConfigureReader()
    {
        if (Reader.IsConnected)
        {
            TriggerInfo triggerInfo = new TriggerInfo();
            triggerInfo.StartTrigger.TriggerType = START_TRIGGER_TYPE.StartTriggerTypeImmediate;
            triggerInfo.StopTrigger.TriggerType = STOP_TRIGGER_TYPE.StopTriggerTypeImmediate;
            try
            {
                // receive events from reader
                if (eventHandler == null)
                {
                    eventHandler = new EventHandler(Reader);
                }
    
                Reader.Events.AddEventsListener(eventHandler);
                // HH event
                Reader.Events.SetHandheldEvent(true);
                // tag event with tag data
                Reader.Events.SetTagReadEvent(true);
                Reader.Events.SetAttachTagDataWithReadEvent(false);
                // set trigger mode as rfid so scanner beam will not come
                Reader.Config.SetTriggerMode(ENUM_TRIGGER_MODE.RfidMode, true);
                // set start and stop triggers
                Reader.Config.StartTrigger = triggerInfo.StartTrigger;
                Reader.Config.StopTrigger = triggerInfo.StopTrigger;
            }
            catch (InvalidUsageException e)
            {
                e.PrintStackTrace();
            }
            catch (OperationFailureException e)
            {
                e.PrintStackTrace();
            }
        }
    }
    
  5. Add EventHandler class to handle Reader operation events - EventReadNotify and Reader status events - EventStatusNotify

    
    // Read/Status Notify handler
    // Implement the RfidEventsLister class to receive event notifications
    public class EventHandler : Java.Lang.Object, IRfidEventsListener
    {
        public EventHandler(RFIDReader Reader)
        {
    
        }
        // Read Event Notification
        public void EventReadNotify(RfidReadEvents e)
        {
            TagData[] myTags = Reader.Actions.GetReadTags(100);
        }
    
        // Status Event Notification
        public void EventStatusNotify(RfidStatusEvents rfidStatusEvents)
        {
    
        }
    }
    
  6. Assign Label Text="{Binding Status}" to - Status to print some useful information on screen
  7. 
    
    
    
    
        
            
                
    
                
    
                
    
                
    
    
            
        
    
    
    
    

Running the application

Connection with device

  1. Make sure project is already built successfully

  2. Click on Run app (F5) to run the applicationr

  3. Now Application should be launched on Zebra Android device fine

  4. Application should be showing Reader connected on screen

  5. img

Programming tips

  1. GetAvailableRFIDReaderList must be called from background thread, in current example uses ThreadPool.QueueUserWorkItem for same purpise

  2. At time of exit, application shound disconnect with reader using Disconnect API and free up SDK instance.

  3. In case of failures or exceptions refer messages returned by API call to get more details

What's Next

  1. Add code to show the tag reporting unique count and total tag count

  2. Add code to configure various reader parameters e.g. Antenna power level and Singulation control to change opertional behavior as per need

  3. Refer various guides Guides and RFID demo application source for various features

Application full source



using Com.Zebra.Rfid.Api3;

namespace MauiHelloWorld
{
    public partial class MainPage : ContentPage
    {
        Readers readers;
        private IList availableRFIDReaderList;
        private ReaderDevice readerDevice;
        private static RFIDReader Reader;
        private EventHandler eventHandler;
        private string _status;
        public string Status { get => _status; set { _status = value; OnPropertyChanged(); } }
        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;

            if (readers == null)
            {
                readers = new Readers(Android.App.Application.Context, ENUM_TRANSPORT.ServiceUsb);
            }
            GetAvailableReaders();
            Status = "connecting..";
        }

       

        private void GetAvailableReaders()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                try
                {
                    if (readers != null && readers.AvailableRFIDReaderList != null)
                    {
                        availableRFIDReaderList =
                            readers.AvailableRFIDReaderList;
                        if (availableRFIDReaderList.Count > 0)
                        {
                            if (Reader == null)
                            {
                                // get first reader from list
                                readerDevice = availableRFIDReaderList[0];
                                Reader = readerDevice.RFIDReader;
                                // Establish connection to the RFID Reader
                                Reader.Connect();
                                if (Reader.IsConnected)
                                {
                                    Console.Out.WriteLine("Reader connected");
                                    Status = "Reader connected";
                                    ConfigureReader();
                                }

                            }
                        }
                    }
                }
                catch (InvalidUsageException e)
                {
                    e.PrintStackTrace();
                }
                catch (OperationFailureException e)
                {
                    e.PrintStackTrace();
                    Console.Out.WriteLine("OperationFailureException " + e.VendorMessage);
                    //_status = "OperationFailureException " + e.VendorMessage;
                }
            });
        }

        private void ConfigureReader()
        {
            if (Reader.IsConnected)
            {
                TriggerInfo triggerInfo = new TriggerInfo();
                triggerInfo.StartTrigger.TriggerType = START_TRIGGER_TYPE.StartTriggerTypeImmediate;
                triggerInfo.StopTrigger.TriggerType = STOP_TRIGGER_TYPE.StopTriggerTypeImmediate;
                try
                {
                    // receive events from reader
                    if (eventHandler == null)
                    {
                        eventHandler = new EventHandler(Reader);
                    }

                    Reader.Events.AddEventsListener(eventHandler);
                    // HH event
                    Reader.Events.SetHandheldEvent(true);
                    // tag event with tag data
                    Reader.Events.SetTagReadEvent(true);
                    Reader.Events.SetAttachTagDataWithReadEvent(false);
                    // set trigger mode as rfid so scanner beam will not come
                    Reader.Config.SetTriggerMode(ENUM_TRIGGER_MODE.RfidMode, true);
                    // set start and stop triggers
                    Reader.Config.StartTrigger = triggerInfo.StartTrigger;
                    Reader.Config.StopTrigger = triggerInfo.StopTrigger;
                }
                catch (InvalidUsageException e)
                {
                    e.PrintStackTrace();
                }
                catch (OperationFailureException e)
                {
                    e.PrintStackTrace();
                }
            }
        }

        // Read/Status Notify handler
        // Implement the RfidEventsLister class to receive event notifications
        public class EventHandler : Java.Lang.Object, IRfidEventsListener
        {
            public EventHandler(RFIDReader Reader)
            {

            }
            // Read Event Notification
            public void EventReadNotify(RfidReadEvents e)
            {
                TagData[] myTags = Reader.Actions.GetReadTags(100);
            }

            // Status Event Notification
            public void EventStatusNotify(RfidStatusEvents rfidStatusEvents)
            {

            }
        }

    }

}