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
-
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.
-
Declare RFID readers fields:
private static Readers readers; private static IList
availableRFIDReaderList; private static ReaderDevice readerDevice; private static RFIDReader Reader; private EventHandler eventHandler; -
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);
-
Create
ThreadPool.QueueUserWorkItem
to retrieve available readers usingAvailableRFIDReaderList
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; } }); }
-
Add
ConfigureReader
method to configure the reader for trigger type and adding event handler / listenerAddEventsListener
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(); } } }
-
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) { } }
- Assign
Label Text="{Binding Status}"
to -Status
to print some useful information on screen
/?xml version="1.0" encoding="utf-8" ?>
/ContentPage
x:Class="MauiHelloWorld.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
/ScrollView>
/VerticalStackLayout Padding="30,0" Spacing="25">
/Image
Aspect="AspectFit"
HeightRequest="185"
SemanticProperties.Description="dot net bot in a race car number eight"
Source="dotnet_bot.png" />
/Label
SemanticProperties.HeadingLevel="Level1"
Style="{StaticResource Headline}"
Text="Hello, World!" />
/Label
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
SemanticProperties.HeadingLevel="Level2"
Style="{StaticResource SubHeadline}"
Text="Welcome to
.NET Multi-platform App UI" />
/Label
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
SemanticProperties.HeadingLevel="Level3"
HorizontalOptions="Center"
Text="{Binding Status}" />
/VerticalStackLayout>
/ScrollView>
/ContentPage>
Running the application
Connection with device
-
Make sure project is already built successfully
-
Click on
Run app (F5)
to run the applicationr -
Now Application should be launched on Zebra Android device fine
-
Application should be showing
Reader connected
on screen
Programming tips
-
GetAvailableRFIDReaderList
must be called from background thread, in current example usesThreadPool.QueueUserWorkItem
for same purpise -
At time of exit, application shound disconnect with reader using
Disconnect
API and free up SDK instance. -
In case of failures or exceptions refer messages returned by API call to get more details
What's Next
-
Add code to show the tag reporting unique count and total tag count
-
Add code to configure various reader parameters e.g. Antenna power level and Singulation control to change opertional behavior as per need
-
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)
{
}
}
}
}