讀取 Mifare Classic ID
nuget : NFCFoems

nuget : Plugin.Permissions

在ProjectName.Android 設定 AndroidManifest.xml
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
在ProjectName.Android 設定 MainActivity.cs
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public NfcAdapter NFCdevice;
public NfcForms x;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Hello.Droid.Resource.Layout.Tabbar;
ToolbarResource = Hello.Droid.Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(Context.NfcService);
NFCdevice = NfcManager.DefaultAdapter;
Xamarin.Forms.DependencyService.Register<INfcForms, NfcForms>();
x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnResume()
{
base.OnResume();
if (NFCdevice != null)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
NFCdevice.EnableForegroundDispatch(this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {
new string[] {
NFCTechs.Ndef,
},
new string[] {
NFCTechs.MifareClassic,
},
});
}
}
protected override void OnPause()
{
base.OnPause();
NFCdevice.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
x.OnNewIntent(this, intent);
}
}
備註:
在sony測試時沒什麼問題,卻在移植到nexus時卻不能用,查文件才發現。需將
NfcAdapter.ActionTechDiscovered > NfcAdapter.ActionTagDiscovered
NfcAdapter.ActionTechDiscovered
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.
To receive this intent an activity must include an intent filter for this action and specify the desired tech types in a manifest meta-data entry. Here is an example manfiest entry:
NfcAdapter.ActionTagDiscovered
Intent to start an activity when a tag is discovered.
This intent will not be started when a tag is discovered if any activities respond to NfcAdapter.ActionNdefDiscovered or NfcAdapter.ActionTechDiscovered for the current tag.
在 ProjectName 建立 XAML
目的:開啟手機的NFC,當手機接觸卡片時將ID、卡的類型與寫入的資料
<StackLayout>
<Label x:Name="SerialNumber" ></Label>
<ListView x:Name="TechList"/>
<ListView x:Name="NDEFMessage"/>
</StackLayout>
在ProjectName XAML.cs
private readonly INfcForms device;
public NFCPage ()
{
InitializeComponent ();
device = DependencyService.Get<INfcForms>();
device.NewTag += HandleNewTag;
}
void HandleNewTag(object sender, NfcFormsTag e)
{
SerialNumber.Text = "Serial Number:" + BitConverter.ToString(e.Id);
TechList.ItemsSource = null;
NDEFMessage.ItemsSource = null;
if (TechList != null)
TechList.ItemsSource = e.TechList;
if (e.IsNdefSupported)
NDEFMessage.ItemsSource = readNDEFMEssage(e.NdefMessage);
}
private ObservableCollection<string> readNDEFMEssage(NdefMessage message)
{
ObservableCollection<string> collection = new ObservableCollection<string>();
foreach (var record in message)
{
// Go through each record, check if it's a Smart Poster
if (record.CheckSpecializedType(false) == typeof(NdefSpRecord))
{
// Convert and extract Smart Poster info
var spRecord = new NdefSpRecord(record);
collection.Add("URI: " + spRecord.Uri);
collection.Add("Titles: " + spRecord.TitleCount());
collection.Add("1. Title: " + spRecord.Titles[0].Text);
collection.Add("Action set: " + spRecord.ActionInUse());
}
if (record.CheckSpecializedType(false) == typeof(NdefUriRecord))
{
// Convert and extract Smart Poster info
var spRecord = new NdefUriRecord(record);
collection.Add("Text: " + spRecord.Uri);
}
if (record.CheckSpecializedType(false) == typeof(NdefTextRecord))
{
var spRecord = new NdefTextRecord(record);
collection.Add("Text: " + spRecord.Text);
}
}
return collection;
}