如何读取标签的内容

How to read the content of a tag

我是使用 xamarin 或 android nfc 应用程序进行编程的新手。这个想法是特定的标签会更改代码中的变量,以便我可以在我的逻辑中使用它们。 (有10个不同内容的标签)

namespace KaffeeListeAndroid
{
    [Activity(Label = "Kaffeeliste Fabrik ID", MainLauncher = true, Theme = "@style/AppTheme")]
    [IntentFilter(new[] { NfcAdapter.ActionTagDiscovered })]

    public class MainActivity : AppCompatActivity
    {

        TextView LabelNFCTag;
        NfcAdapter _nfcAdapter;
        PendingIntent nfcPI;
        IntentFilter nfcFilter;
        Tag nfcTag;

        string newLine = System.Environment.NewLine;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            //NFC initialisierung
            LabelNFCTag = FindViewById<TextView>(Resource.Id.textNFCTag);

            _nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
            if (_nfcAdapter == null)
            {
                LabelNFCTag.Text = "NFC Adapter steht nicht zur Verfügung.";
                return;
            }

            if (!_nfcAdapter.IsEnabled)
            {
                LabelNFCTag.Text = "NFC Adapter ist ausgeschaltet!";
                return;
            }

            var intent = new Intent(this, this.Class);
            intent.AddFlags(ActivityFlags.SingleTop);
            nfcPI = PendingIntent.GetActivity(this, 0, intent, 0);
            nfcFilter = new IntentFilter(NfcAdapter.ActionTagDiscovered);
            nfcFilter.AddCategory(Intent.CategoryDefault);

            //Methodenaufruf für das Erkennen eines Tags
            Scan();
        }

        private void Scan()
        {
            try
            {
                if (nfcTag == null)
                {
                    LabelNFCTag.Text = "NFC Tag ist nicht in der Nähe!";
                    return;
                }

                var ndef = Ndef.Get(nfcTag);
                ndef.Connect();
                var data = Encoding.ASCII.GetString(ndef.NdefMessage.ToByteArray());
                ndef.Close();

                LabelNFCTag.Text = $"Data: {newLine}{data}";
            }
            catch (Exception ex)
            {
                LabelNFCTag.Text += $"{newLine} Exeption: {newLine} {ex.Message} {newLine} {ex.StackTrace}";
            }
        }

        protected override void OnResume()
        {
            base.OnResume();

            _nfcAdapter.EnableForegroundDispatch(this, nfcPI, new IntentFilter[] { nfcFilter }, null);

            if (NfcAdapter.ActionTagDiscovered == Intent.Action)
            {
                ProcessIntent(Intent);
            }
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            Intent = intent;
            if (NfcAdapter.ActionTagDiscovered == intent.Action)
            {
                ProcessIntent(Intent);
            }
        }

        private void ProcessIntent(Intent intent)
        {
            var LabelNFCTag = FindViewById<TextView>(Resource.Id.textNFCTag);

            try
            {
                nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

                LabelNFCTag.Text = $"Inhalt des Tags: {nfcTag}";
            }
            catch (Exception ex)
            {
                LabelNFCTag.Text = $"{newLine} Exeption:{newLine}{ex.Message}{newLine}{ex.StackTrace}";
            }
        }

    }

}

如果我将 nfctag 靠近智能手机,它会显示:" Inhalt des Tags: TAG: Tech[android.nfc.tech.NfcA,android.nfc.Mifare Ultralight,android.nfc.tech.Ndef]"

所以变量nfcTag 得到了错误的内容。

但我希望它应该显示标签的内容。 也许有人可以帮我得到我需要的字符串。

您需要从 intent 中提取 ExtraNdefMessages,如下所示:

        private void ProcessIntent(Intent intent)
        {
            var LabelNFCTag = FindViewById<TextView>(Resource.Id.textNFCTag);

            try
            {
                nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

                if (nfcTag != null)
                {
                    // First get all the NdefMessage
                    var messages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
                    if (messages != null)
                    {
                        var msg = (NdefMessage)messages[0];

                        // Get NdefRecord which contains the actual data
                        var record = msg.GetRecords()[0];
                        if (record != null)
                        {
                            if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
                            {
                                // Get the transfered data
                                var data = Encoding.ASCII.GetString(record.GetPayload());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LabelNFCTag.Text = $"{newLine} Exeption:{newLine}{ex.Message}{newLine}{ex.StackTrace}";
            }
        }