Obtain and understand magnetic stripe data

This topic describes how to obtain and interpret the data from a magnetic stripe reader using Universal Windows Platform (UWP) Point of Service (POS) APIs.

Subscribe to *DataReceived events

Once you've set up your magnetic stripe reader in your application using the steps outlined in Getting started with Point of Service, you're ready to start getting data from it.

Whenever the reader recognizes a swiped card, it will raise one of three events:

Your application only needs to subscribe to the events that are supported by the magnetic stripe reader. You can see what types of cards are supported with MagneticStripeReader.SupportedCardTypes.

The following code demonstrates subscribing to the three *DataReceived events:

private void SubscribeToEvents(ClaimedMagneticStripeReader claimedReader, MagneticStripeReader reader)
{
    foreach (var type in reader.SupportedCardTypes)
    {
        if (item == MagneticStripeReaderCardTypes.Aamva)
        {
            claimedReader.AamvaCardDataReceived += Reader_AamvaCardDataReceived;
        }
        else if (item == MagneticStripeReaderCardTypes.Bank)
        {
            claimedReader.BankCardDataReceived += Reader_BankCardDataReceived;
        }
        else if (item == MagneticStripeReaderCardTypes.ExtendedBase)
        {
            claimedReader.VendorSpecificDataReceived += Reader_VendorSpecificDataReceived;
        }
    }
}

The event handler will be passed the ClaimedMagneticStripeReader and an args object, whose type will vary depending on the event:

Get the data

For the AamvaCardDataReceived and BankCardDataReceived events, you can get some of the data directly from the args object. The following example demonstrates getting a few properties and assigning them to member variables:

private string _accountNumber;
private string _expirationDate;
private string _firstName;

private void Reader_BankCardDataReceived(
    ClaimedMagneticStripeReader sender, 
    MagneticStripeReaderBankCardDataReceivedEventArgs args)
{
    _accountNumber = args.AccountNumber;
    _expirationDate = args.ExpirationDate;
    _firstName = args.FirstName;
    // etc...
}

However, some data, including all data from the VendorSpecificDataReceived event, must be retrieved through the Report object, which is a property of the args parameter. This is of type MagneticStripeReaderReport.

You can use the CardType property to figure out what type of card has been swiped, and then use that to inform how you interpret the data from Track1, Track2, Track3, and Track4.

The data from each of the tracks are represented as MagneticStripeReaderTrackData objects. From this class, you can get the following types of data:

The following code snippet gets the report and the track data, and then checks the card type:

private void GetTrackData(MagneticStripeReaderBankCardDataReceivedEventArgs args)
{
    MagneticStripeReaderReport report = args.Report;
    IBuffer track1 = report.Track1.Data;
    IBuffer track2 = report.Track2.Data;
    IBuffer track3 = report.Track3.Data;
    IBuffer track4 = report.Track4.Data;

    if (report.CardType == MagneticStripeReaderCardTypes.Aamva)
    {
        // Card type is AAMVA
    }
    else if (report.CardType == MagneticStripeReaderCardTypes.Bank)
    {
        // Card type is bank card
    }
    else if (report.CardType == MagneticStripeReaderCardTypes.ExtendedBase)
    {
        // Card type is vendor-specific
    }
    else if (report.CardType == MagneticStripeReaderCardTypes.Unknown)
    {
        // Card type is unknown
    }
}

Support and feedback

Find answers to your questions

Have questions? Ask us on either our Docs Q&A forum with the UWP tag or on Stack Overflow with the pointofservice tag.

Help us locate your questions:

  • Add the pointofservice tag to your question on Stack Overflow.
  • Include the "UWP" term in your post on the Q&A forum

See also