/* * 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 (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); }