如何读取标签内容

时间:2019-08-15 14:33:24

标签: c# xamarin.android nfc

我是使用xamarin或android nfc应用程序编程的新手。想法是,特定的Tag会更改代码中的变量,以便我可以在逻辑中使用它们。 (有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,则会显示:“吸入标签:标签:Tech [android.nfc.tech.NfcA,android.nfc.Mifare Ultralight,android.nfc.tech.Ndef]”

因此变量nfcTag的内容错误。

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

1 个答案:

答案 0 :(得分:0)

您需要按照以下步骤从val data2 = spark.read.option("header", "true").option("inferSchema", "true").option("treatEmptyValuesAsNulls","true").option("nullValue", null)***.option("emptyValue", null)***.csv("file:///tmp/test.csv") 中提取ExtraNdefMessages

intent