示例#1
0
        private SmartPoster Parse(NdefRecord[] ndefRecord)
        {
            var records = NdefMessageParser.GetInstance().GetRecords(ndefRecord);
            var uri     = records.Where(o => o is UriRecord);
            var title   = records.FirstOrDefault(o => o is TextRecord);

            return(new SmartPoster(uri, title));
        }
示例#2
0
        protected override void OnNewIntent(Intent intent)
        {
            if (intent.Action == NfcAdapter.ActionTagDiscovered)
            {
                List <string> tags = new List <string>();

                var id = intent.GetByteArrayExtra(NfcAdapter.ExtraId);

                if (id != null)
                {
                    string data = "";
                    for (int ii = 0; ii < id.Length; ii++)
                    {
                        if (!string.IsNullOrEmpty(data))
                        {
                            data += "-";
                        }
                        data += id[ii].ToString("X2");
                    }

                    tags.Add(data);
                }
                else
                {
                    tags.Add(null);
                }

                var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                if (tag != null)
                {
                    var rawTagMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraTag);

                    // First get all the NdefMessage
                    var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
                    if (rawMessages != null)
                    {
                        // https://medium.com/@ssaurel/create-a-nfc-reader-application-for-android-74cf24f38a6f

                        foreach (var message in rawMessages)
                        {
                            foreach (var r in NdefMessageParser.GetInstance().Parse((NdefMessage)message))
                            {
                                System.Diagnostics.Debug.WriteLine("TAG: " + r.Str());
                                tags.Add(r.Str());
                            }
                        }
                    }
                }

                MessagingCenter.Send <App, List <string> >((App)Xamarin.Forms.Application.Current, "Tag", tags);
            }
            else if (intent.Action == NfcAdapter.ActionNdefDiscovered)
            {
                System.Diagnostics.Debug.WriteLine("ActionNdefDiscovered");
            }
        }