Working with the New Point Of Service APIs in Windows 8.1

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



Related Articles

Downloads

Comments

  • Work with Camera API for Barcode Scanner - Windows 8.1 project

    Posted by Robert Hellestrae on 05/20/2014 12:22pm

    Fabulous sample on working Windows 8.1 Point of Service API's; I am writing to ask if there are any code samples working with the Windows 8.1 Camera API for scanning bar codes; Best regards - Rob

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds