IntentFilter on NFC - Xamarin - C #

Context: Framework 4.5, Xamarin.Android v5.0

I want to use NFC technology to implement shortcuts for application users. I want users to scan the NFC tag, so I just need to put the value in a predefined schema.

I put some arguments in my NFC message, and I do this when I write the message in my NFC tag:

    var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
    var ndef = Ndef.Get(tag);
            NdefRecord external = NdefRecord.CreateExternal(applicationPackageName(), "letypetype", Encoding.ASCII.GetBytes("param"));
            NdefRecord appRecord = NdefRecord.CreateApplicationRecord(applicationPackageName());
            NdefMessage ndefMessage = new NdefMessage(external, appRecord);
    if (ndef != null)
    {
        ndef.Connect();
        ndef.WriteNdefMessage(ndefMessage);
    }

Then I want to use it in my application, so I put it in AndroidManifest.xml:

<uses-feature android:name="android.hardware.nfc" android:required="true" />

And in my main activity I have the following filter of intentions:

     [IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered },
      Categories = new[] { Intent.CategoryDefault },
      DataScheme = "vnd.android.nfc", DataPathPrefix = "letypetype",
     DataHost = "ext")]
        public class Activity1 : Activity
       { ...

And I'm trying to process my parameter in this action using the override OnResume method:

    protected override void OnResume()
    {
        base.OnResume();
        if (NfcAdapter.ActionNdefDiscovered.Equals(this.Intent.Action))
        {
            IParcelable[] rawMsgs = this.Intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
            if (rawMsgs != null)
            {
                NdefMessage[] msgs = new NdefMessage[rawMsgs.Length];
                for (int i = 0; i < rawMsgs.Length; i++)
                {
                    msgs[i] = (NdefMessage)rawMsgs[i];
                }
            }
        }
    }

But there is no way to return it. Therefore, I am sure that something is wrong, but I do not know what.

+4
2

( , ), ? EnableForegroundDispatch OnNewIntent, .

private NfcAdapter mNfcAdapter;

OnCreate

  mNfcAdapter = NfcAdapter.GetDefaultAdapter(this);

OnResume

if (mNfcAdapter != null)
    {
      var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered);//or try other Action type
      var filters = new[] { tagDetected };
      var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
      var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
      mNfcAdapter.EnableForegroundDispatch(this, pendingIntent, filters, null);
    }

overriede

    protected override void OnNewIntent(Intent intent)
{
  object obj = intent.GetParcelableExtra(NfcAdapter.ExtraTag);
  if (obj != null && obj is Tag)
  {
    Tag t = (Tag)obj;
    byte[] id = t.GetId();
    string[] techList = t.GetTechList();
    int con = t.DescribeContents();
    string objName = t.ToString();
  }
}

OnPause

if (mNfcAdapter != null) mNfcAdapter.DisableForegroundDispatch(this);

OnDestroy

if (mNfcAdapter != null)
    {
      mNfcAdapter.Dispose();
      mNfcAdapter = null;
    }
0

, , . , ,

[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered },
      Categories = new[] { Intent.CategoryDefault },
      DataScheme = "vnd.android.nfc",
      DataPathPrefix = "letypetype",
      DataHost = "ext")]

,

NdefRecord external = NdefRecord.CreateExternal(
            applicationPackageName(),
            "letypetype",
            Encoding.ASCII.GetBytes("param"));

- Android (AAR). theres NFC-, Android , NFC , , ( NDEF) .

NDEF / , , . "com.example", :

[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered },
      Categories = new[] { Intent.CategoryDefault },
      DataScheme = "vnd.android.nfc",
      DataPathPrefix = "/com.example:letypetype",
      DataHost = "ext")]

, DataPathPrefix.

, ( ) Java/Android NFC Forum . (, example.com).

, : NFC :

<uses-permission android:name="android.permission.NFC" />
0

Source: https://habr.com/ru/post/1613642/


All Articles