Пример #1
0
        async void Current_OnMessageReceived(ITagInfo tagInfo)
        {
            if (tagInfo == null)
            {
                await ShowAlert("No tag found");

                return;
            }

            // Customized serial number
            var identifier   = tagInfo.Identifier;
            var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":");
            var title        = !string.IsNullOrWhiteSpace(serialNumber) ? $"Tag [{serialNumber}]" : "Tag Info";

            if (!tagInfo.IsSupported)
            {
                await ShowAlert("Unsupported tag (app)", title);
            }
            else if (tagInfo.IsEmpty)
            {
                await ShowAlert("Empty tag", title);
            }
            else
            {
                var first = tagInfo.Records[0];
                await ShowAlert(GetMessage(first), title);
            }
        }
Пример #2
0
        private void CurrentOnOnMessageReceived(ITagInfo tagInfo)
        {
            AppendText("MessageReceived");
            AppendText($"SerialNumber = {tagInfo.SerialNumber}");
            var id = NFCUtils.ByteArrayToHexString(tagInfo.Identifier);

            AppendText($"Identifier = {id}");
            AppendText($"tagInfo.IsEmpty = {tagInfo.IsEmpty}");
            AppendText($"tagInfo.IsWritable = {tagInfo.IsWritable}");
            if (tagInfo.Records != null && tagInfo.Records.Any())
            {
                AppendText($"tagInfo.Records.Length = {tagInfo.Records.Length}");
                foreach (var record in tagInfo.Records)
                {
                    if (record.Payload != null && record.Payload.Any())
                    {
                        var payload = NFCUtils.ByteArrayToHexString(record.Payload);
                        AppendText($"record.Payload = {payload}");
                    }
                    if (record.Message != null)
                    {
                        AppendText($"record.Message = {record.Message}");
                    }
                }
            }
            Device.BeginInvokeOnMainThread(CrossNFC.Current.StartListening);
            Analytics.TrackEvent("NfcTag", new Dictionary <string, string> {
                { "Identifier", id }
            });
        }
Пример #3
0
 /// <summary>
 /// Custom contructor
 /// </summary>
 /// <param name="identifier">Tag Identifier as <see cref="byte[]"/></param>
 /// <param name="isNdef">Is Ndef tag</param>
 public TagInfo(byte[] identifier, bool isNdef = true)
 {
     Identifier   = identifier;
     SerialNumber = NFCUtils.ByteArrayToHexString(identifier);
     IsSupported  = isNdef;
 }
Пример #4
0
        protected override void OnNewIntent(Intent intent)
        {
            //base.OnNewIntent(intent);
            // CrossNFC.OnNewIntent(intent);

            string manuf        = "";
            string data         = "";
            string serialNumber = "";

            TagRead tagRead = new TagRead();


            if (intent.Action == NfcAdapter.ActionTagDiscovered)
            {
                List <string> tags        = new List <string>();
                NdefMessage   ndefMessage = null;

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

                if (id != null)
                {
                    serialNumber = NFCUtils.ByteArrayToHexString(id, ":");

                    /*for (int ii = 0; ii < id.Length; ii++)
                     * {
                     *  if (!string.IsNullOrEmpty(data))
                     *      serialNumber += "-";
                     *  serialNumber += id[ii].ToString("X2");
                     * }*/
                    tagRead.SerialNumber = serialNumber;
                    tags.Add(serialNumber);
                }
                else
                {
                    tags.Add(null);
                }

                var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                if (tag != null)
                {
                    manuf = NFCHelpers.GetManufacturer(tag);
                    tags.Add(manuf);
                    tagRead.Model = manuf;

                    if (NFCHelpers.IsNDEFTag(tag))
                    {
                        var ndef = Ndef.Get(tag);

                        if (ndef != null)
                        {
                            if (ndefMessage == null)
                            {
                                ndefMessage = ndef.CachedNdefMessage;
                            }

                            if (ndefMessage != null)
                            {
                                NdefRecord[] records     = ndefMessage.GetRecords();
                                var          ndefrecords = NFCHelpers.GetRecords(records);
                                for (var i = 0; i < ndefrecords.Count; i++)
                                {
                                    if (data != "")
                                    {
                                        data += "\n";
                                    }
                                    data += ndefrecords[i];
                                }
                                tags.Add(data);
                                tagRead.Data = data;
                            }
                        }
                    }

/*
 *                  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, TagRead>((App)Xamarin.Forms.Application.Current, "Tag", tagRead);
            }
            else if (intent.Action == NfcAdapter.ActionNdefDiscovered)
            {
                System.Diagnostics.Debug.WriteLine("ActionNdefDiscovered");
            }
        }