相关文章推荐
逆袭的圣诞树  ·  Android ...·  3 天前    · 
气宇轩昂的西红柿  ·  FORTR77内部函数·  5 月前    · 
粗眉毛的柠檬  ·  ubuntu - curl: symbol ...·  1 年前    · 
爱看书的海豚  ·  Convert a Unicode ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm new to Xamarin cross-platform app development, and I'm trying to implement on Android version of my app an external NFC tag reading.

When a tag is scanned, I would like my application to open and read the text inside the tag and finally perform some specific actions based on what was read.

I've this implementation on MainActivity.cs, but it doesn't work because it seems that I can't get the intent:

using Android.Content;
using System;
using System.Text;
using System.Diagnostics;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Xamarin.Forms;
using Android.Content.Res;
using FFImageLoading.Forms.Platform;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Plugin.CurrentActivity;
using Android.Nfc;
using Android.Nfc.Tech;
using Poz1.NFCForms.Abstract;
using Poz1.NFCForms.Droid;
namespace Kibelis.Droid
    [Activity(Label = "Kibelis", Icon = "@drawable/icon", Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTop, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        private NfcAdapter _nfcAdapter;
        public object NFCUtil { get; private set; }
        protected override void OnCreate(Bundle savedInstanceState)
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            CachedImageRenderer.Init(true);
            base.OnCreate(savedInstanceState);
            CrossCurrentActivity.Current.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            _nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
            System.Diagnostics.Debug.WriteLine("CREATE");
            // is attached.
            LoadApplication(new App());
        protected override void OnResume()
            base.OnResume();
            if (_nfcAdapter == null)
                System.Diagnostics.Debug.WriteLine("NFC UNAVIABLE");
                var tagDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
                var filters = new[] { tagDetected };
                var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
                _nfcAdapter.EnableForegroundDispatch(this, pendingIntent, filters, null);
                System.Diagnostics.Debug.WriteLine("FOREGRAUND DISPATCH");
        protected override void OnPause()
            base.OnPause();
        protected override void OnNewIntent(Intent intent)
            base.OnNewIntent(intent);
            System.Diagnostics.Debug.WriteLine("NEW INTENT");
            if (intent.Extras.IsEmpty)
                System.Diagnostics.Debug.WriteLine("empty");
                System.Diagnostics.Debug.WriteLine("Not empty");
            //For start reading
            if (intent.Action == NfcAdapter.ActionTagDiscovered || intent.Action == NfcAdapter.ActionNdefDiscovered || intent.Action == NfcAdapter.ActionAdapterStateChanged
                || intent.Action == NfcAdapter.ActionTransactionDetected || intent.Action == NfcAdapter.ExtraNdefMessages || intent.Action == NfcAdapter.ExtraNdefMessages)
                System.Diagnostics.Debug.WriteLine("DISCOVERD");
                var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
                if (tag != null)
                    System.Diagnostics.Debug.WriteLine("TAG");
                    // First get all the NdefMessage
                    var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
                    if (rawMessages != null)
                        var msg = (NdefMessage)rawMessages[0];
                        System.Diagnostics.Debug.WriteLine("MESSAGE");
                        // Get NdefRecord which contains the actual data
                        var record = msg.GetRecords()[0];
                        if (record != null)
                            if (record.Tnf == NdefRecord.TnfWellKnown)
                                // Get the transfered data
                                var data = Encoding.ASCII.GetString(record.GetPayload());
                                System.Diagnostics.Debug.WriteLine("RECORD");
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);

Can you help me?

You are registering the foreground dispatch for the ActionNdefDiscovered intent. However, this intent filter also requires a specific data type (to be present on the tag and to be registered for the intent). If that's what you want, you need to add that data type (MIME type or URI) to the intent filter (variable tagDetected).

If, instead, you want to just listen for all tags, you want to use the ActionTagDiscovered intent instead. In fact, you can simply skip the intnent filter all together from the call to EnableForegroundDispatch:

_nfcAdapter.EnableForegroundDispatch(this, pendingIntent, null, null);
                @Micheal Roland Thank you! Now, by setting tagDetected to ActionNdefDiscovered, I got a new intent but I cannot read tag content yet... I think that something goes wrong in first if condition on OnNewIntent(Intent intent). Can you help me again?
– user8334240
                Nov 16, 2018 at 13:30
                Better, the code enter in if condition only when the app is also running and not when it was launched
– user8334240
                Nov 16, 2018 at 13:40
                @FabioPaccosi "Something goes wrong" is not really a helpful description of the problem. Was your initial problem solved by my answer? If yes, please ask a spearate new question. If no, please update your existing question with further details. Specifically, is it working now while your app is in the foreground (that's what the code you showed in your question is about...). Should it also work when your application is launched (that probably is a new question then)? Then you also need to show your manifest intent filters and what data you actually put on the tag.
– Michael Roland
                Nov 16, 2018 at 18:06
                @MichealRoland You're right, I'll open a new request if I could not solve my problem. Thanks for your suggestions.
– user8334240
                Nov 16, 2018 at 19:19
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.