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 | Development Status | Tested on |
---|---|---|---|
Android | 4.4+ | Working | Google Nexus 5, Huawei Mate 10 Pro |
iOS | 11+ | Working | iPhone 7 (simple "text/plain" NDEF message) |
Windows | 10.0.16299+ | Pending | No Windows devices available |
- Add NFC Permission
android.permission.NFC
and NFC featureandroid.hardware.nfc
in 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.OnNewIntent(intent)
in yourOnNewIntent()
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
// Plugin NFC: Tag Discovery Interception
CrossNFC.OnNewIntent(intent);
}
Due to Apple restriction, only reading tag is supported.
An iPhone 7+ and iOS 11+ are required in order to use NFC with iOS devices.
- Add
Near Field Communication Tag Reading
capabilty in yourEntitlements.plist
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</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>
Only NDEF tags are supported on Windows.
- Add
Proximity
capability to thePackage.appxmanifest
<Capabilities>
<DeviceCapability Name="proximity" />
</Capabilities>
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.IsAvailable
to verify if NFC is available. - Check
CrossNFC.Current.IsEnabled
to 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;
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
{
...
}
In this case, we will receive tags supporting NDEF mapped to a specified MIME Type "application/com.companyname.yourapp
".
- Start listening with
CrossNFC.Current.StartListening()
. - When a NDEF message is received, the event
OnMessageReceived
is raised.
- To write a tag, call
CrossNFC.Current.StartPublishing()
- Then
CrossNFC.Current.PublishMessage(ITagInfo)
whenOnTagDiscovered
event 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)
whenOnTagDiscovered
event 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.
Feel free to contribute. PRs are accepted and welcomed.
This plugin is based on the great work of many developers in addition to mine. 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.