private async void btnAddCard_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Clear the messages
            MainPage.Current.NotifyUser(String.Empty, NotifyType.StatusMessage, true);

            // Register the AID/applet IDs that this application will handle for our particular card(s)
            var reg = await NfcUtils.RegisterAidGroup(txtDisplayName.Text, new[] { AID_PPSE.AsBuffer(), AID_SAMPLE_CARD.AsBuffer() }, SmartCardEmulationCategory.Payment, SmartCardEmulationType.Host, (chkAutomaticEnablement.IsChecked ?? false));

            var track2          = txtPAN.Text + "D" + txtExpiryYear.Text + txtExpiryMonth.Text + txtServiceCode.Text + txtTrack2Discretionary.Text;
            var record          = BuildReadRecordResponse(track2, txtCardholderName.Text, txtTrack1Discretionary.Text);
            var encryptedRecord = await ProtectDataAsync(record, "LOCAL=user");

            var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("ReadRecordResponse-" + reg.Id.ToString("B") + ".dat", Windows.Storage.CreationCollisionOption.ReplaceExisting);

            await Windows.Storage.FileIO.WriteBufferAsync(file, encryptedRecord);

            LogMessage("Card data saved", NotifyType.StatusMessage);
            this.Frame.GoBack();
        }
        private async void btnAddCard_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Clear the messages
            MainPage.Current.NotifyUser(String.Empty, NotifyType.StatusMessage, true);

            // Register the AID/applet IDs that this application will handle for our particular card(s)
            SmartCardAppletIdGroupRegistration reg;

            if (optUICC.IsChecked ?? false)
            {
                reg = await NfcUtils.RegisterAidGroup(
                    txtDisplayName.Text,
                    new[] { AID_PPSE.AsBuffer(), AID_V.AsBuffer(), AID_MC.AsBuffer() },
                    SmartCardEmulationCategory.Payment,
                    SmartCardEmulationType.Uicc,
                    (chkAutomaticEnablement.IsChecked ?? false));
            }
            else if (optNonPayment.IsChecked ?? false)
            {
                reg = await NfcUtils.RegisterAidGroup(
                    txtDisplayName.Text,
                    new[] { AID_NONPAYMENT.AsBuffer() },
                    SmartCardEmulationCategory.Other,
                    SmartCardEmulationType.Host,
                    (chkAutomaticEnablement.IsChecked ?? false));

                var rule1 = new SmartCardAutomaticResponseApdu(NfcUtils.HexStringToBytes("0012AABB").AsBuffer(), NfcUtils.HexStringToBytes("AABBCCDDEE9000").AsBuffer());
                rule1.AppletId = AID_NONPAYMENT.AsBuffer();
                await reg.SetAutomaticResponseApdusAsync(new List <SmartCardAutomaticResponseApdu>() { rule1 });
            }
            else
            {
                var aid = (optV.IsChecked ?? false) ? AID_V.AsBuffer() : AID_MC.AsBuffer();
                reg = await NfcUtils.RegisterAidGroup(
                    txtDisplayName.Text,
                    new[] { AID_PPSE.AsBuffer(), aid },
                    SmartCardEmulationCategory.Payment,
                    SmartCardEmulationType.Host,
                    (chkAutomaticEnablement.IsChecked ?? false));

                var rules = new List <SmartCardAutomaticResponseApdu>();

                // Construct SELECT PPSE response and set as auto responder
                rules.Add(new SmartCardAutomaticResponseApdu(
                              NfcUtils.HexStringToBytes("00A404000E325041592E5359532E444446303100").AsBuffer(),
                              new TlvEntry(0x6F, new TlvEntry[] {
                    new TlvEntry(0x84, "2PAY.SYS.DDF01"),
                    new TlvEntry(0xA5, new TlvEntry[] {
                        new TlvEntry(0xBF0C,
                                     new TlvEntry(0x61, new TlvEntry[] {
                            new TlvEntry(0x4F, aid),
                            new TlvEntry(0x87, new byte[] { 0x01 })
                        }))
                    })
                }).GetData(0x9000).AsBuffer()));

                if (optMC.IsChecked ?? false)
                {
                    rules.Add(new SmartCardAutomaticResponseApdu(
                                  NfcUtils.HexStringToBytes("00A4040007A000000004101000").AsBuffer(),
                                  new TlvEntry(0x6F, new TlvEntry[] {
                        new TlvEntry(0x84, aid),
                        new TlvEntry(0xA5, new TlvEntry[] {
                            new TlvEntry(0x50, "CREDIT CARD")
                        })
                    }).GetData(0x9000).AsBuffer()));

                    var ruleGpo = new SmartCardAutomaticResponseApdu(
                        NfcUtils.HexStringToBytes("80A80000").AsBuffer(),
                        new TlvEntry(0x77, new TlvEntry[] {
                        new TlvEntry(0x82, new byte[] { 0x00, 0x00 }),
                        new TlvEntry(0x94, new byte[] { 0x08, 0x01, 0x01, 0x00 }),
                        new TlvEntry(0xD7, new byte[] { 0x00, 0x00, 0x80 })
                    }).GetData(0x9000).AsBuffer());
                    ruleGpo.AppletId          = aid;
                    ruleGpo.ShouldMatchLength = false;
                    rules.Add(ruleGpo);
                }
                else if (optV.IsChecked ?? false)
                {
                    var ruleGpo = new SmartCardAutomaticResponseApdu(
                        NfcUtils.HexStringToBytes("80A80000").AsBuffer(),
                        new TlvEntry(0x80, new byte[] { 0x00, 0x80, 0x08, 0x01, 0x01, 0x00 }).GetData(0x9000).AsBuffer());
                    ruleGpo.AppletId          = aid;
                    ruleGpo.ShouldMatchLength = false;
                    rules.Add(ruleGpo);
                }

                byte[] record;
                if (optV.IsChecked ?? false)
                {
                    var track2 = txtPAN.Text + "D" + txtExpiryYear.Text + txtExpiryMonth.Text + txtServiceCode.Text + txtTrack2Discretionary.Text;
                    record = BuildReadRecordResponseV(track2, txtCardholderName.Text, txtTrack1Discretionary.Text);
                }
                else
                {
                    record = BuildReadRecordResponseMC(txtPAN.Text, txtExpiryYear.Text + txtExpiryMonth.Text, txtServiceCode.Text, txtCardholderName.Text, txtTrack1Discretionary.Text, txtTrack2Discretionary.Text);
                }
                var ruleReadRec = new SmartCardAutomaticResponseApdu(
                    NfcUtils.HexStringToBytes("00B2").AsBuffer(),
                    record.AsBuffer());
                ruleReadRec.AppletId          = aid;
                ruleReadRec.ShouldMatchLength = false;
                rules.Add(ruleReadRec);

                var encryptedRecord = await ProtectDataAsync(record, "LOCAL=user");

                var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("ReadRecordResponse-" + reg.Id.ToString("B") + ".dat", Windows.Storage.CreationCollisionOption.ReplaceExisting);

                await Windows.Storage.FileIO.WriteBufferAsync(file, encryptedRecord);

                await reg.SetAutomaticResponseApdusAsync(rules);
            }

            LogMessage("Card data saved", NotifyType.StatusMessage);
            this.Frame.GoBack();
        }