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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds