Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(string.Format("GoToTags NFC Sample App v{0}", Assembly.GetCallingAssembly().GetName().Version.ToString()));
                Console.WriteLine();

                Licensing.Unlock("unlock code goes here");
                Console.WriteLine(string.Format("Licensed to: {0}", Licensing.GetLicenseData()));
                Console.WriteLine();

                Console.WriteLine("Place an nfc tag on the reader; press 'Enter' when ready.");
                Console.ReadLine();
                Console.WriteLine();

                try
                {
                    var isCertPropSvcRunning = CertPropSvc.IsRunning();

                    if (isCertPropSvcRunning)
                    {
                        Console.WriteLine("The CertPropSvc Service is running; it interferes with NFC and should be stopped.");

                        CertPropSvc.StopService();
                        Console.WriteLine("The CertPropSvc service was stopped.");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("The CertPropSvc service is not running.");
                        Console.WriteLine();
                    }
                }
                catch (Exception)
                {
                    // trap exceptions here, some systems dont have this service
                }

                nfcAdapter = NfcAdapter.DefaultAdapter;

                if (nfcAdapter == null)
                {
                    Console.WriteLine("No nfc readers found");
                    goto done;
                }

                // get all of the readers
                Console.WriteLine("Supported Nfc readers:");
                var readers = nfcAdapter.SupportedReaders();

                if (readers.Count() > 0)
                {
                    readers.ToList().ForEach(r => Console.WriteLine(r.ToString()));
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("No nfc readers found");
                    goto done;
                }

                // use the first reader
                var reader = readers.First();
                NfcAdapter.Adapter(reader);

                // get the tag
                var tag = nfcAdapter.GetTag();

                if (tag == null)
                {
                    Console.WriteLine("An nfc tag is not on the reader.");
                    Console.WriteLine();
                }
                else
                {
                    var tagTech = tag.TagType;
                    Console.WriteLine(string.Format("Tag Tech: {0}", tagTech));

                    // ndef operations
                    using (var ndef = NdefTechnology.Get(tag))
                    {
                        ndef.Connect();

                        var isFormatted = ndef.IsFormatted;
                        Console.WriteLine(string.Format("Is Formatted: {0}", isFormatted));

                        var isWritable = ndef.IsWritable;
                        Console.WriteLine(string.Format("Is Writable: {0}", isWritable));

                        var tagUID = ndef.TagUIDString();
                        Console.WriteLine(string.Format("Tag UID: {0}", tagUID));

                        Console.WriteLine();
                        Console.WriteLine("Ndef Records:");
                        var message = ndef.GetNdefMessage();

                        if (message != null)
                        {
                            foreach (var record in message.Records)
                            {
                                Console.WriteLine(string.Format("{0} {1}", record.GetType().Name, record.ToString()));
                            }

                            Console.WriteLine();
                        }

                        if (isWritable)
                        {
                            ndef.Erase();
                            Console.WriteLine("The tag's contents have been erased.");
                            Console.WriteLine();

                            // encode an ndef url to the nfc tag
                            var nfcUriRecord = new NfcUriRecord("http://www.buynfctags.com");
                            ndef.WriteNdefMessage(nfcUriRecord);
                            Console.WriteLine("The tag has been encoded with the url: {0}", nfcUriRecord.Uri);
                            Console.WriteLine();

                            // this will make the tag permanently read only
                            //ndef.MakeReadOnly();
                        }
                    }
                }

                Console.WriteLine("Place nfc tags on the reader, one after another...");

                // listen to the status changed event to listen for tags places on the reader
                nfcAdapter.StatusChanged += nfcAdapter_StatusChanged;

                // will read 3 tags, then quit
                while (tagCounter < 3)
                {
                    Thread.Sleep(100);
                }

                // give the last tag a bit of time to be read
                Thread.Sleep(100);
                Console.WriteLine("Done!");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
            }

            done:

            Console.WriteLine();
            Console.WriteLine("Press 'Enter' to quit");
            Console.ReadLine();
        }