Home

Awesome

NDEF Library for Proximity APIs / NFC

Easily parse and create NFC tags that contain standard-based NDEF messages.

Available in C# and JavaScript (for HTML5-based apps).

The library download comes with complete example apps that demonstrate reading and writing tags using the NDEF Library. Available for: .net Standard 1.4, Windows 10 / UWP. The Windows 8.1 / WinRT and Windows Phone 8 / Silverlight versions are deprecated and are no longer available in the latest packages.

NuGet Library Download | Platform Specific Extension Library | Windows 10 Example App Download

Background - NFC and NDEF

NFC tags as well as the content sent in device-to-device communication when tapping two phones is based on certain standards by the NFC Forum (called NDEF – NFC Data Exchange format). Luckily, these standards were well received and nearly all manufacturers are part of the standardization body. This ensures that public NFC tags can actually be read by all mobile phones today.

When it comes to storing data on NFC tags that can have as little writable storage as around 40 bytes, very efficient and complex data storage schemes are necessary. The downside is that most operating systems do integrate the NFC data transmission at the base level, but offer developers very little support for the NDEF standards on top. Obviously, reading those technical documents isn’t generally too much fun – to create an own implementation of a message that stores a simple URL on a tag, a developer would need to read and understand 59 pages of specifications.

As this is a lot work, there is the risk of people creating own solutions, leading to a fragmented NFC ecosystem

The NFC Library

The open source NFC / NDEF Library contains a large set of classes that take care of formatting your data according to NDEF standards, so that these can be directly written to NFC tags or sent to other devices.

In your app, you choose the corresponding record type (e.g., for URLs, emails or geo tags) and provide the necessary data. The library creates an NDEF message out of the data, which you can directly send to the NFC stack in your operating system as a byte array, which takes care of writing it to a tag or publishing it to another device (using the SNEP protocol).

Additionally, the library can parse NDEF byte arrays that you read from tags or receive from other devices and create a list (NDEF Message) of data classes (NDEF records) that you can easily analyze and use in your app.

For Windows, the NFC stack is represented through the Proximity APIs - they encapsulate NFC hardware communication and basic NDEF formatting for a very limited subset of the NDEF standards. This missing part is added by this NDEF library.

Availability

The NFC / NDEF library is available in C# and JavaScript and can therefore be used on any operating system.

To keep up to date, either follow this project or follow me on Twitter.

C# Version

The library is available as a ready-made portable class library, which can be used on any platform compatible to .net Standard 1.4+, like Windows 10 (UWP).

Additional platform-specific functionality is added through the the separate extension library. It integrates with the platform APIs for UWP and allows real-life tasks like creating a business card record based on a Contact from the Windows address book.

An example app is available for Windows 10. In addition to the library download on this page, you can use the NuGet package manager of Visual Studio to easily integrate the library with your project.

JavaScript / HTML5 Version

The new JavaScript port of the library provides the most important NDEF types also to HTML5 / JavaScript apps.

Feature Overview

Reusable NDEF classes

Supported NDEF records:

New and custom functionality (C#)

Convenience classes extending the basic URI class for common use case scenarios

Platform-specific extension library to enable real-life use cases (C#)

Example Apps

For C#, the library download comes with NdefDemoWin10 (for Windows 10 / UWP). This example app demonstrates some of the features of the NDEF Library. The demo is available under GPL v3 license. You can download the Windows 10 example app from the Windows Store.

Another GPL-licensed example app for Windows Phone 8 is NfcShare, which is available together with accompanying webinar slides and a recording at the NFC developer's section at NfcInteractor.com.

Examples of apps currently using the NDEF Library and available in the public store:

Usage example (C#)

Reading & parsing a Smart Poster

private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
{
    // Parse raw byte array to NDEF message
    var rawMsg = message.Data.ToArray();
	var ndefMessage = NdefMessage.FromByteArray(rawMsg);

	// Loop over all records contained in the NDEF message
	foreach (NdefRecord record in ndefMessage) 
	{
		Debug.WriteLine("Record type: " + Encoding.UTF8.GetString(record.Type, 0, record.Type.Length));
		// 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);
			Debug.WriteLine("URI: " + spRecord.Uri);
			Debug.WriteLine("Titles: " + spRecord.TitleCount());
			Debug.WriteLine("1. Title: " + spRecord.Titles[0].Text);
			Debug.WriteLine("Action set: " + spRecord.ActionInUse());
		}
	}
}

Writing a Smart Poster

// Initialize Smart Poster record with URI, Action + 1 Title
var spRecord = new NdefSpRecord {
                  Uri = "http://www.nfcinteractor.com", 
                  NfcAction = NdefSpActRecord.NfcActionType.DoAction };
spRecord.AddTitle(new NdefTextRecord { 
                  Text = "Nfc Interactor", LanguageCode = "en" });

// Add record to NDEF message
var msg = new NdefMessage { spRecord };

// Publish NDEF message to a tag
// AsBuffer(): add -> using System.Runtime.InteropServices.WindowsRuntime;
_device.PublishBinaryMessage("NDEF:WriteTag", msg.ToByteArray().AsBuffer());

// Alternative: send NDEF message to another NFC device
_device.PublishBinaryMessage("NDEF", msg.ToByteArray().AsBuffer());

Usage Example (JavaScript)

Create a URI Record:

// Create NDEF Message
var ndefMessage = new NdefLibrary.NdefMessage();
// Create NDEF Uri Record
var ndefUriRecord = new NdefLibrary.NdefUriRecord();
// Set Uri in record
ndefUriRecord.setUri("https://www.mobilefactory.at");
// Add record to message
ndefMessage.push(ndefUriRecord);
// Get byte array for NFC tag
var byteArray = ndefMessage.toByteArray();

Create a raw NDEF Message by defined input:

var recordType = new Array(1,3,1,3,5,6,7);
var recordPayload = new Array(1,2,1);
var id = new Array(3,3);
var ndefRecord2 = new NdefLibrary.NdefRecord(NdefLibrary.NdefRecord.TypeNameFormatType.NfcRtd, recordType);
ndefRecord2.setPayload(recordPayload);
ndefRecord2.setId(id);

var ndefMessage = new NdefLibrary.NdefMessage();
ndefMessage.push(ndefRecord2);
var byteArray = ndefMessage.toByteArray();

Create a raw NDEF Message by byte array from NFC tag:

var ndefMessage = NdefLibrary.NdefMessage.fromByteArray(byteArray);

Installation (C#)

To try the library, you can clone the complete repository from GitHub and test the included NdefDemo example app.

If you want to use the NFC / NDEF Library from your own app, the easiest option is to use the NuGet package manager in Visual Studio to automatically download & integrate the portable library:

  1. Tools -> Library Package Manager -> Manage NuGet Packages for Solution...
  2. Search "Online" for "NDEF"
  3. Install: "NFC / NDEF Library for Proximity APIs"
  4. To use the platform extension library, also install: "NFC / NDEF Library Platform Extensions for Proximity APIs"

Core NFC / NDEF Library

NFC / NDEF Library Platform-Specific Extension Library

You can also download the complete portable library project from the source control server of this project, and build the library yourself, or directly integrate the relevant class files.

Installation (JavaScript)

The JavaScript library is available in two versions, both are available in the "dist" folder of the JavaScript project:

Version History (C#)

4.1.0 - August 2017

3.1.1 - January 2016

Changes

3.0.3 - March 2015

3.0.2 - July 2014

3.0.1-alpha - July 2014

2.0.0.2 - April 2014

1.1.0.0 - July 2013

Version History (JavaScript)

Known issues and limitations:

1.0.0 - Work in progress

0.0.1 - March 2014

Status & Roadmap

The NDEF library is classified as stable release and is in use in several projects, most importantly NFC interactor for Windows Phone (https://www.nfcinteractor.com/).

Any open issues as well as planned features are tracked online: https://github.com/andijakl/ndef-nfc/issues

Related Information

Released under the LGPL 2.1 license - see the LICENSE.LGPL file for details.

Developed by Andreas Jakl https://twitter.com/andijakl https://www.andreasjakl.com/

Ported to Javascript by Sebastian Höbarth https://at.linkedin.com/in/sebastianhoebarth

Parts of this library are based on the respective code of the Connectivity Module of Qt Mobility (NdefMessage, NdefRecord, NdefUriRecord and NdefTextRecord classes. Original source code: https://doc-snapshots.qt.io/qt-mobility/).

Library homepage on GitHub: https://andijakl.github.io/ndef-nfc/ https://github.com/andijakl/ndef-nfc/

You might also be interested in the Bluetooth Beacon library: https://github.com/andijakl/universal-beacon