I'm coding in C# with Xamarin and trying to scan a MIFARE Classic 1K card by NFC.
The intent-filter of m1card_test is working fine. But I don't want to select which Activity I want to start. So I'm trying to use the foreground dispatch.
Here is part of my code (C#):
OnCreate
Intent Myintent = new Intent(this, GetType());
Myintent.AddFlags(ActivityFlags.SingleTop);
mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0);
ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
ndefDetected.AddDataType("*/*");
intentF = new IntentFilter[] { ndefDetected };
techLists = new string[][] {new string[] {
typeof(Android.Nfc.Tech.NfcA).FullName,
typeof(Android.Nfc.Tech.MifareClassic).FullName}
};
OnPause
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.DisableForegroundDispatch(this);
OnResume
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,intentF,techLists);
Unfortunately, the foreground dispatch doesn't work (i.e. it does not pick up the tag).
If I change the call to EnableForegroundDispatch()
to
manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,null,null);
the foreground dispatch work fine. But it picks up all tags and not just MIFARE Classic and I get an intent Action_Tag_Discovered instead of Action_Tech_Discovered.
How to use Action_Tech_Discovered with foreground dispatch system?
Did I miss something?
tech_list.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="m1card_test.m1card_test" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" />
<application android:label="m1card_test"></application>
<uses-permission android:name="android.permission.NFC" />
</manifest>
My C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Nfc;
namespace m1card_test
{
[Activity(Label = "m1_read", Icon = "@drawable/icon", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)]
[IntentFilter(
new[] {NfcAdapter.ActionTechDiscovered},
Categories = new[] {Intent.CategoryDefault,})]
[MetaData("android.nfc.action.TECH_DISCOVERED", Resource = "@xml/tech_list")]
public class m1_read : Activity
{
TextView mTV;
PendingIntent mPendingIntent;
IntentFilter ndefDetected;
IntentFilter[] intentF;
String[][] techLists;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.m1_read);
Intent Myintent = new Intent(this, GetType());
Myintent.AddFlags(ActivityFlags.SingleTop);
mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0);
ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
ndefDetected.AddDataType("*/*");
intentF = new IntentFilter[] { ndefDetected };
techLists = new string[][] {new string[] {
typeof(Android.Nfc.Tech.NfcA).FullName,
typeof(Android.Nfc.Tech.MifareClassic).FullName}
};
Button button = FindViewById<Button>(Resource.Id.Back_Button);
mTV = FindViewById<TextView>(Resource.Id.textview);
button.Click += delegate
{
Intent main_intent = new Intent(this, typeof(MainActivity));
this.StartActivity(main_intent);
Finish();
};
}
protected override void OnPause()
{
base.OnPause();
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.DisableForegroundDispatch(this);
}
protected override void OnResume()
{
base.OnResume();
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent,intentF,techLists);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
mTV.Text = "OnNewIntent";
}
}
}
The TECH_DISCOVERED
intent filter does not have a data-type (MIME type) associated with it. Thus you need to remove the line
ndefDetected.AddDataType("*/*");
Moreover, I'm not quite sure if typeof(Android.Nfc.Tech.MifareClassic).FullName
resolves to the correct name (full Java class name) of the tag technology. Thus, you should probably hard-code that string (just as you do in the tech-filter XML file):
techLists = new string[][] { new string[] {
"android.nfc.tech.NfcA",
"android.nfc.tech.MifareClassic"
}};
Finally, since the MifareClassic tag technology always implies NfcA, you can safely reduce the tech-filter to just
techLists = new string[][] { new string[] {
"android.nfc.tech.MifareClassic"
}};