/* * card_write_proc * --------------- * This is the core function to try read a card, and maybe recognize it as an NFC tag * The function is executed in a background thread, so the application's window keeps * responding during the dialog */ private void card_write_proc(object _tag) { if (_tag == null) { return; /* Oups */ } if (!(_tag is NfcTag)) { return; /* Oups */ } NfcTag tag = _tag as NfcTag; if (!tag.IsFormatted() && !tag.Format()) { this.BeginInvoke(new OnErrorInvoker(OnError), "This application has been unable to format the Tag", "Failed to encode the NFC Tag"); return; } if (!tag.Write()) { this.BeginInvoke(new OnErrorInvoker(OnError), "This application has been unable to write onto the Tag", "Failed to encode the NFC Tag"); return; } if (cbLock.Visible && cbLock.Checked) { /* Try to lock the Tag */ if (tag.IsLockable()) { if (!tag.Lock()) { this.BeginInvoke(new OnErrorInvoker(OnError), "This application has been unable to lock the Tag in read-only state", "Failed to encode the NFC Tag"); return; } } else { /* This Tag is not locackable */ this.BeginInvoke(new OnErrorInvoker(OnError), "The Tag has been successfully written, but there's no method to put it in read-only state", "This NFC Tag can't be locked"); return; } } /* Done */ this.BeginInvoke(new OnTagWriteInvoker(OnTagWrite), tag); }
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(); }
void BtnWriteClick(object sender, EventArgs e) { if (tag == null) { return; /* Oups */ } if (control == null) { return; /* Oups */ } if (!control.ValidateUserContent()) { /* The user did not provide a valid content */ return; } Ndef ndef = control.GetContent(); if (ndef == null) { MessageBox.Show("Failed to create the NDEF message to be written onto the Tag"); return; } tag.Content.Clear(); tag.Content.Add(ndef); long content_size = tag.ContentSize(); long tag_capacity = tag.Capacity(); if (content_size > tag_capacity) { MessageBox.Show("The capacity of the Tag is " + tag_capacity + "B, but the content you're trying to write makes " + content_size + "B", "This Tag is too small"); return; } if (!tag.IsEmpty()) { /* Ask for confirmation before overwriting */ if (MessageBox.Show("The Tag already contains data. Do you really want to overwrite its content?", "Confirm overwrite", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } if (!tag.IsFormatted()) { /* Ask for confirmation before formatting */ if (MessageBox.Show("The Tag is not yet formatted. Do you really want to format it?", "Confirm formatting", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } if (cbLock.Visible && cbLock.Checked) { if (!tag.IsLockable()) { /* The Tag can't be locked */ } else { /* Ask for confirmation before locking */ if (MessageBox.Show("Locking the Tag in read-only state is permanent and can't be cancelled. Are you really sure you want to do this?", "Confirm locking", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } } cardthread = new Thread(new ParameterizedThreadStart(card_write_proc)); cardthread.Start(tag); }