Github — sorinmiroiu97/xamarin-nfc-app: android app written in c# using the xamarin framework
This is a ready to use app written in C#, using the Xamarin framework. It is compatible with the Android devices startging from 2.3 (gingerbread). This is a sample showing how to use the NFC library within the Android framework and you can in this app write anything you want into an NFC chip (by typing into the EditText) and read anything you want from an NFC chip.
Read android nfc tag with unknown tag type xamarin — c#
I want to use the NFC technology for my Android application.
I want the users to be able to scan an NFC card so they can log in to the application. The card contains a number.
So the idea is just to retrieve the code and convert it to string
but I’m getting an Nullobjectreference
Attempt to invoke virtual method ‘boolean
android.nfc.Tag.hasTech(int)’ on a null object reference
UPDATE
Source code:
using System;
using System.Text;
using Android.App;
using Android.Nfc;
using Android.OS;
using Android.Widget;
using Android.Content;
using Android.Nfc.Tech;
namespace NFC_NDEF
{
[Activity(Label = "NFC_NDEF_TST", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })]
public class MainActivity : Activity
{
NfcAdapter nfcAdapter;
PendingIntent nfcPi;
IntentFilter nfcFilter;
Tag nfcTag;
string newLine = System.Environment.NewLine;
protected override void OnCreate(Bundle bundle)
{ base.OnCreate(bundle); SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button btnScan = FindViewById<Button>(Resource.Id.MyButton); btnScan.Click = Scan; //var writerButton = FindViewById<Button>(Resource.Id.WriteButton); //writerButton.Click = Write; var label = FindViewById<TextView>(Resource.Id.textView_rslt); nfcAdapter = NfcAdapter.GetDefaultAdapter(ApplicationContext); if (nfcAdapter == null) { label.Text = "NFC is not available."; return; } if (!nfcAdapter.IsEnabled) { label.Text = "NFC is disabled."; return; } var intent = new Intent(this, this.Class); intent.AddFlags(ActivityFlags.SingleTop); nfcPi = PendingIntent.GetActivity(this, 0, intent, 0); nfcFilter = new IntentFilter(NfcAdapter.ActionTechDiscovered); nfcFilter.AddCategory(Intent.CategoryDefault);
}
private void Scan(object sender, EventArgs e)
{ var label = FindViewById<TextView>(Resource.Id.textView_rslt); try { if (nfcTag == null) { label.Text = "nfc tag is null"; return; } var ndef = Ndef.Get(nfcTag); ndef.Connect(); var data = Encoding.UTF8.GetString(ndef.NdefMessage.ToByteArray()); ndef.Close(); label.Text = $"Data:{newLine}{data}"; } catch (Exception ex) { label.Text = $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}"; }
}
protected override void OnResume()
{ base.OnResume(); nfcAdapter.EnableForegroundDispatch(this, nfcPi, new IntentFilter[] { nfcFilter }, null); if (NfcAdapter.ActionTechDiscovered == Intent.Action) { ProcessIntent(Intent); }
}
protected override void OnNewIntent(Intent intent)
{ base.OnNewIntent(intent); Intent = intent; if (NfcAdapter.ActionTechDiscovered == intent.Action) { ProcessIntent(Intent); }
}
private void ProcessIntent(Intent intent)
{ var label = FindViewById<TextView>(Resource.Id.textView_rslt); try { nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; label.Text = $"Id: 0x{Smartisan.Nfc.Utils.ByteToHex(nfcTag.GetId())}"; label.Text = $"{newLine}Techs: {newLine}"; label.Text = string.Join(newLine, nfcTag.GetTechList()); } catch (Exception ex) { label.Text = $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}"; }
}
}
}AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NFC_NDEF.NFC_NDEF" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> <uses-sdk android:minSdkVersion="16" /> <uses-permission android:name="android.permission.NFC" /> <uses-sdk android:targetSdkVersion="15" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
<application android:label="NFC_NDEF"></application>
</manifest>Trying to implement nfc in xamarin forms
@ross, I use Alessandro Pozone plugin and for me it works well.
In github repository you find an «Example» directory. In «NFCFormSample» directory there is a NFCPage.cs file that «read» the NFC and display its values.
If you run this example, put a breakpoint in HandleNewTag method. In «e» parameter you should find all you need about your NFC
Have a nice day
Alessandro





