Working with the New Point Of Service APIs in Windows 8.1

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

Smart Devices are increasingly being used at Point of Service (POS) terminals.

Increasingly, we are seeing tablets being used as checkout devices. To capitalize on the potential, Microsoft has introduced first class support for Point of Service devices with new APIs that specialize for these devices. These APIs will work with barcode scanners and magnetic stripe readers and do not rely on manufacturer-specific solutions. Instead, Microsoft has provided manufacturer-neutral APIs, which can be used in Windows Store applications to interact with the POS devices.

Point of Service APIs

These APIs reside in the Windows.Device.PointOfService namespace and are available for both XAML developers as well as web developers (JavaScript).

The barcode scanner is represented by the BarcodeScanner class, which resides in the Windows.Device.PointOfService.

You can get a BarcodeScanner object in one of two ways:

1. Call Windows.Device.PointOfService.BarcodeScanner.GetDefaultAsync static method, which will return the first barcode scanner attached to the device on which the application will be running.

2. Call Windows.Device.PointOfService.BarcodeScanner.FromIdAsync method, which will return a specific BarcodeScanner object depending on the DeviceInformationId parameter.

To get exclusive access to the scanner device, call the ClaimScannerAsync API.

The magnetic strip reader resides in the Windows.Device.PointOfService namespace as MagneticStripeReader class. The methods on this class are similar to the ones on BarcodeScanner class except one difference; to get exclusive access, you would call the ClaimReaderAsync API.

Hands On

In our demo application, we will create a simple Windows Store application, which will connect to a magnetic stripe reader and receive data from it.

Create a new Windows Store application titled WindowsPointOfServiceDemo.

New Project
New Project

Add three Button controls. The first button, buttonGetReader, will be to get a handle to a Magnetic Stripe Reader and claim exclusive access to the magnetic stripe reader. The second button, buttonStartReadingMagneticData, will begin processing data received from the magnetic stripe reader. The last button, buttonStopReading, will release exclusive access to the magnetic stripe reader.

Since we will be working with PointOfService APIs, we need to include the Windows.Devices.PointOfService namespace  in the code-behind file.

//Mainpage.xaml.cs
using Windows.Devices.PointOfService;
Next, we will add a MagneticStripeReader instance.
public sealed partial class MainPage : Page
    {
        MagneticStripeReader myReader = null;
 ClaimedMagneticStripeReader myClaimedReader = null;
        public MainPage()
        {
            this.InitializeComponent();
        }
    }

In the event handler for the buttonGetReader click event, we will invoke the static API Windows.Device.PointOfService.MagneticStripeReader.GetDefaultAsync to get the handle to the first available strip reader.

        private async void buttonGetReader_Click(object sender, RoutedEventArgs e)

        {

            myReader = await MagneticStripeReader.GetDefaultAsync();

            if (myReader != null)

                myClaimedReader = await myReader.ClaimReaderAsync();

        }

Next, we will enable the magnetic strip reader to receive data, and also specify the callback to be invoked when a Bank Card Data Received event occurs (when a bank card is swiped).

private async void buttonStartReadingMagneticData_Click(object sender, RoutedEventArgs e)
        {
            await myClaimedReader.EnableAsync();
myClaimedReader.BankCardDataReceived += myClaimedReader_BankCardDataReceived;
           
        }

Now, our magnetic strip reader will be receiving data. To listen to the data, we add code in the myClaimedReader_BankCardDataReceived event handler.

void myClaimedReader_BankCardDataReceived(ClaimedMagneticStripeReader sender, MagneticStripeReaderBankCardDataReceivedEventArgs args)
        {
            textBoxAccountNumber.Text = args.AccountNumber;
           
        }
 

Finally, we implement the buttonStopReading button’s click event handler. In this method, we will release the exclusive access and also unregister event handlers for BankCardDataReceived event.

private async void buttonStopReading_Click(object sender, RoutedEventArgs e)
        {
            await myClaimedReader.DisableAsync();
            myClaimedReader.BankCardDataReceived -= myClaimedReader_BankCardDataReceived;
        }

We are now ready to test our code. If you have trouble following along, you can download a copy of this sample below.

If you have a POS device and attach it to a Windows machine running Windows 8.1 and run this application, you can test out the application we have built.

Summary

In this article, we learned about the built-in API support for Point of Service devices like barcode scanner and magnetic strip reader. I hope you have found this information useful.

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read