Click or drag to resize

MagCardReader Interface

Provides access to the magnetic card reader, for devices equipped with one.

Namespace:  Zebra.Sdk.Device
Assembly:  SdkApi.Core (in SdkApi.Core.dll) Version: 2.15.2634
Syntax
public interface MagCardReader

The MagCardReader type exposes the following members.

Methods
  NameDescription
Public methodRead
Activates the device's magnetic card reader, if present, and waits for a card to be swiped.
Top
Examples
Desktop
using System;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;

public class MagCardReaderExample {

    public static void Main(string[] args) {
        Connection connection = new TcpConnection("192.168.1.100", TcpConnection.DEFAULT_CPCL_TCP_PORT);

        try {
            connection.Open();
            ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
            MagCardReader mcr = MagCardReaderFactory.Create(printer);
            if (mcr != null) {
                string[] trackData = mcr.Read(10 * 1000);
                Console.WriteLine("Track1: " + trackData[0]);
                Console.WriteLine("Track2: " + trackData[1]);
                Console.WriteLine("Track3: " + trackData[2]);
            }
        } catch (ConnectionException e) {
            Console.WriteLine(e.ToString());
        } catch (ZebraPrinterLanguageUnknownException e) {
            Console.WriteLine(e.ToString());
        } finally {
            connection.Close();
        }
    }
}
Examples
Android™
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Threading.Tasks;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;

public class MagCardReaderExample : Activity {

    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);

        LinearLayout layout = (LinearLayout)View.Inflate(this, Android.Resource.Layout.ActivityListItem, null);
        layout.Orientation = Orientation.Vertical;

        Button testButton = new Button(this) {
            Text = "Read Magnetic Card Example",
            LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent))
        };
        layout.AddView(testButton);

        SetContentView(layout);

        testButton.Click += async (sender, e) => {
            await Task.Run(() => {
                string ipAddress = "1.2.3.4";
                ReadMagCard(ipAddress);
            });
        };
    }

    public void ReadMagCard(string ipAddress) {
        Connection connection = new TcpConnection(ipAddress, TcpConnection.DEFAULT_CPCL_TCP_PORT);

        try {
            connection.Open();
            ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
            MagCardReader mcr = MagCardReaderFactory.Create(printer);
            if (mcr != null) {
                string[] trackData = mcr.Read(10 * 1000);

                RunOnUiThread(() => {
                    if (trackData != null) {
                        string magData = $"Track1: {trackData[0]}\nTrack2: {trackData[1]}\nTrack3: {trackData[2]}";
                        Toast.MakeText(ApplicationContext, magData, ToastLength.Long).Show();
                    } else {
                        Toast.MakeText(ApplicationContext, "No magnetic data read", ToastLength.Long).Show();
                    }
                });
            }
        } catch (ConnectionException e) {
            Console.WriteLine(e.ToString());
        } catch (ZebraPrinterLanguageUnknownException e) {
            Console.WriteLine(e.ToString());
        } finally {
            connection.Close();
        }
    }
}
Examples
iOS
using CoreGraphics;
using System;
using System.Threading.Tasks;
using UIKit;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;

public class MagCardReaderExample : UIViewController {

    public MagCardReaderExample(IntPtr handle) : base(handle) {
        UIButton testButton = new UIButton(UIButtonType.System) {
            Frame = new CGRect(25, 25, 300, 150)
        };

        testButton.SetTitle("Read Magnetic Card Example", UIControlState.Normal);

        testButton.TouchUpInside += async (sender, e) => {
            await Task.Run(() => {
                string ipAddress = "1.2.3.4";
                ReadMagCard(ipAddress);
            });
        };

        View.AddSubview(testButton);
    }

    public void ReadMagCard(string ipAddress) {
        Connection connection = new TcpConnection(ipAddress, TcpConnection.DEFAULT_CPCL_TCP_PORT);

        try {
            connection.Open();
            ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
            MagCardReader mcr = MagCardReaderFactory.Create(printer);
            if (mcr != null) {
                string[] trackData = mcr.Read(10 * 1000);


                if (trackData != null) {
                    string magData = $"Track1: {trackData[0]}\nTrack2: {trackData[1]}\nTrack3: {trackData[2]}";
                    ShowAlertMessage("ReadMagCard", magData);
                } else {
                    ShowAlertMessage("ReadMagCard", "No magnetic data read");
                }
            }
        } catch (ConnectionException e) {
            Console.WriteLine(e.ToString());
        } catch (ZebraPrinterLanguageUnknownException e) {
            Console.WriteLine(e.ToString());
        } finally {
            connection.Close();
        }
    }

    private void ShowAlertMessage(string title, string message) {
        InvokeOnMainThread(() => {
            UIAlertController alertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            PresentViewController(alertController, true, null);
        });
    }
}
See Also