private void card_read_proc() { NfcTag tag = null; string msg = null; try { if (NfcTagType2.RecognizeAtr(_cardchannel)) { if (NfcTagType2.Recognize(_cardchannel)) { tag = NfcTagType2.Create(_cardchannel); if (tag == null) { WarnNotify("Warning|An error has occured while reading the Tag's content"); } } else { msg = "From the ATR it may be a NFC type 2 Tag, but the content is invalid"; WarnNotify("Warning|" + msg); } } else { if (NfcTagType4.Recognize(_cardchannel)) { tag = NfcTagType4.Create(_cardchannel); if (tag == null) { msg = "An error has occured while reading the Tag's content"; WarnNotify("Warning|" + msg); } } else { msg = "Unrecognized or unsupported card"; WarnNotify("Warning|" + msg); } } if (tag != null) { this.BeginInvoke(new OnTagReadInvoker(OnTagRead), tag); } else { this.BeginInvoke(new OnErrorInvoker(OnError), msg, "This is not a valid NFC Tag"); } } catch (Exception ex) { ErrorHandler.TreatError(ex); } }
void BtnWriteClick(object sender, EventArgs e) { if (control == null) { return; } NfcTag nfcTag = null; SCardChannel channel = new SCardChannel(reader); if (!channel.Connect()) { MessageBox.Show("Failed to connect to the card."); return; } if (rbType2.Checked) { if (NfcTagType2.Recognize(channel)) { Trace.WriteLine("This card is a NFC type 2 Tag"); nfcTag = NfcTagType2.Create(channel); } else { Trace.WriteLine("This card is not a NFC type 2 Tag, sorry"); } } else if (rbType4.Checked) { if (NfcTagType4.Recognize(channel)) { Trace.WriteLine("This card is a NFC type 4 Tag"); nfcTag = NfcTagType4.Create(channel); } else { Trace.WriteLine("This card is not a NFC type 4 Tag, sorry"); } } if (nfcTag == null) { channel.Disconnect(); MessageBox.Show("This card is not supported. Are you sure the emulution mode is running?"); return; } if (!nfcTag.IsFormatted()) { channel.Disconnect(); MessageBox.Show("The card is not formatted as a NFC Tag."); return; } nfcTag.Content.Clear(); Ndef ndef = control.GetContent(); nfcTag.Content.Add(ndef); if (!nfcTag.Write(true)) { channel.Disconnect(); MessageBox.Show("Failed to write the NFC Tag."); return; } channel.Disconnect(); }