Ndef CreateMyNdefMessage() { if (control == null) { return(null); } return(control.GetContent()); }
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); }