A Cross-Platform NFC (Near Field Communication) plugin to easily read and write NFC tags in your application.
This plugin uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems.
| Package | Build | NuGet | MyGet |
|---|---|---|---|
| Plugin.NFC |
CI Feed : https://www.myget.org/F/plugin-nfc/api/v3/index.json
| Platform | Version | Tested on |
|---|---|---|
| Android | 4.4+ | Google Nexus 5, Huawei Mate 10 Pro |
| iOS | 11+ | iPhone 7 |
Windows is currently not supported. Pull Requests are welcomed!
- Add NFC Permission
android.permission.NFCand NFC featureandroid.hardware.nfcin yourAndroidManifest.xml
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />- Add the line
CrossNFC.Init(this)in yourOnCreate()
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
// Plugin NFC: Initialization
CrossNFC.Init(this);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}- Add the line
CrossNFC.OnResume()in yourOnResume()
protected override void OnResume()
{
base.OnResume();
// Plugin NFC: Restart NFC listening on resume (needed for Android 10+)
CrossNFC.OnResume();
}- Add the line
CrossNFC.OnNewIntent(intent)in yourOnNewIntent()
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
// Plugin NFC: Tag Discovery Interception
CrossNFC.OnNewIntent(intent);
}iOS 13+ is required for writing tags.
An iPhone 7+ and iOS 11+ are required in order to use NFC with iOS devices.
- Add
Near Field Communication Tag Readingcapabilty in yourEntitlements.plist
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
<string>TAG</string>
</array>- Add a NFC feature description in your Info.plist
<key>NFCReaderUsageDescription</key>
<string>NFC tag to read NDEF messages into the application</string>- Add these lines in your Info.plist if you want to interact with ISO 7816 compatible tags
<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
<string>com.apple.developer.nfc.readersession.iso7816.select-identifiers</string>Before to use the plugin, please check if NFC feature is supported by the platform using CrossNFC.IsSupported.
To get the current platform implementation of the plugin, please call CrossNFC.Current:
- Check
CrossNFC.Current.IsAvailableto verify if NFC is available. - Check
CrossNFC.Current.IsEnabledto verify if NFC is enabled. - Register events:
// Event raised when a ndef message is received.
CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived;
// Event raised when a ndef message has been published.
CrossNFC.Current.OnMessagePublished += Current_OnMessagePublished;
// Event raised when a tag is discovered. Used for publishing.
CrossNFC.Current.OnTagDiscovered += Current_OnTagDiscovered;
// Event raised when NFC listener status changed
CrossNFC.Current.OnTagListeningStatusChanged += Current_OnTagListeningStatusChanged;
// Android Only:
// Event raised when NFC state has changed.
CrossNFC.Current.OnNfcStatusChanged += Current_OnNfcStatusChanged;
// iOS Only:
// Event raised when a user cancelled NFC session.
CrossNFC.Current.OniOSReadingSessionCancelled += Current_OniOSReadingSessionCancelled;In Android, you can use IntentFilter attribute on your MainActivity to initialize tag listening.
[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "application/com.companyname.yourapp")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
...
}To launch/open an app with a tag, TypeFormat of the record must be set to NFCNdefTypeFormat.Mime and MimeType should be setted to the same value of IntentFilter.DataMimeType (e.g. application/com.companyname.yourapp):
var record = new NFCNdefRecord {
TypeFormat = NFCNdefTypeFormat.Mime,
MimeType = "application/com.companyname.yourapp",
Payload = NFCUtils.EncodeToByteArray(_writePayload)
};- Start listening with
CrossNFC.Current.StartListening(). - When a NDEF message is received, the event
OnMessageReceivedis raised.
- To write a tag, call
CrossNFC.Current.StartPublishing() - Then
CrossNFC.Current.PublishMessage(ITagInfo)whenOnTagDiscoveredevent is raised. - Do not forget to call
CrossNFC.Current.StopPublishing()once the tag has been written.
- To clear a tag, call
CrossNFC.Current.StartPublishing(clearMessage: true) - Then
CrossNFC.Current.PublishMessage(ITagInfo)whenOnTagDiscoveredevent is raised. - Do not forget to call
CrossNFC.Current.StopPublishing()once the tag has been cleared.
For more examples, see sample application in the repository.
- Set a new
NfcConfigurationobject toCrossNFC.CurrentwithSetConfiguration(NfcConfiguration cfg)method like below
// Custom NFC configuration (ex. UI messages in French)
CrossNFC.Current.SetConfiguration(new NfcConfiguration
{
Messages = new UserDefinedMessages
{
NFCWritingNotSupported = "L'écriture des TAGs NFC n'est pas supporté sur cet appareil",
NFCDialogAlertMessage = "Approchez votre appareil du tag NFC",
NFCErrorRead = "Erreur de lecture. Veuillez rééssayer",
NFCErrorEmptyTag = "Ce tag est vide",
NFCErrorReadOnlyTag = "Ce tag n'est pas accessible en écriture",
NFCErrorCapacityTag = "La capacité de ce TAG est trop basse",
NFCErrorMissingTag = "Aucun tag trouvé",
NFCErrorMissingTagInfo = "Aucune information à écrire sur le tag",
NFCErrorNotSupportedTag = "Ce tag n'est pas supporté",
NFCErrorNotCompliantTag = "Ce tag n'est pas compatible NDEF",
NFCErrorWrite = "Aucune information à écrire sur le tag",
NFCSuccessRead = "Lecture réussie",
NFCSuccessWrite = "Ecriture réussie",
NFCSuccessClear = "Effaçage réussi"
}
});Thanks to Saamer Mansoor (@saamerm) who wrote this excellent article on Medium about Plugin.NFC and how to use it, check it out!
He also made this video:
Feel free to contribute. PRs are accepted and welcomed.
Inspired by the great work of many developers. Many thanks to:
- James Montemagno (@jamesmontemagno).
- Matthew Leibowitz (@mattleibow) for Xamarin.Essentials PR #131.
- Alessandro Pozone (@poz1) for NFCForms.
- Ultz (@ultz) for XNFC.
- Sven-Michael Stübe (@smstuebe) for xamarin-nfc.

