示例#1
0
        /*
         * 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);
        }
示例#2
0
        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();
        }