The context is: Framework 4.5, Xamarin.Android v5.0
I want to use the NFC technology to implement shortcuts for my application users. I want the users to scan an NFC tag so they just have to put a value to a predefined scheme.
I have put some arguments in my NFC message and I do that when I write the message on 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 on my application, so I have put it in AndroidManifest.xml this:
<uses-feature android:name="android.hardware.nfc" android:required="true" />
And on my main activity I have the following intent filter:
[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered },
Categories = new[] { Intent.CategoryDefault },
DataScheme = "vnd.android.nfc", DataPathPrefix = "letypetype",
DataHost = "ext")]
public class Activity1 : Activity
{ ...
And I try to handle my parameter in this activity with the override method OnResume:
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 get it back. So I'm pretty sure I do something wrong but I don't know what.
If I well understand the question (if not correct me please), the problem is reading the data from the tag? Try the first simple reading through
EnableForegroundDispatchandOnNewIntentand then customize for your needs.In Activity OnCreate
In Activity OnResume
And overriede
do not forget for in OnPause
and OnDestroy