Skip to content

andijakl/ndef-nfc

Repository files navigation

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 JavaScript port of the library provides comprehensive NDEF support for modern web applications. It's built as ES modules and is compatible with the Web NFC API for Chrome on Android. The library includes support for common NDEF record types including social media records for modern platforms.

Feature Overview

Reusable NDEF classes

  • Parse NDEF message & records from raw byte arrays
  • Extract all information from the bits & bytes contained in the record
  • Create standard compliant records just by providing your data
  • Identify the exact record type when reading an NDEF message
  • Records check their contents for validity according to standards
  • Can throw NdefException in case of content validity issues, with translatable messages defined in a resource file
  • Fully documented source code, following Doxygen standards

Supported NDEF records:

  • URI: the most common type: any kind of URI, for example an Internet address, email link or any custom URI scheme.
  • Smart Poster: combines a URL with textual descriptions in various languages (C# only)
  • Text records: contains text in a specific language
  • Social records: support for modern social media platforms including X (formerly Twitter), LinkedIn, Instagram, Threads, TikTok, and Facebook (JavaScript version includes enhanced social media support)
  • Microsoft LaunchApp: launch a Windows (Phone) app just by tapping a tag (C# only)
  • Android Application Record (AAR): launch an Android app
  • Geo records: longitude & latitude of a place using standard Geo URI schemes
  • Tel records: telephone number links
  • Bluetooth Secure Simple Pairing: connect to a Bluetooth device, contains information about the target device like the service class (C# only)
  • Handover Select: part of the Connection Handover specification, provides a list of alternative carriers to connect to. Used for example for NFC loudspeakers. Includes support for child records - Handover Alternative Carrier and Handover Error records (C# only)

New and custom functionality (C#)

  • Smart URI class: automatically represents itself as the smallest possible NDEF type (URI or Smart Poster), depending on supplied data

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

  • Geo: longitude & latitude of a place, using different Geo URI schemes (more details)
  • Social: linking to social networks like Twitter, Facebook, Foursquare or Skype
  • SMS: defining number and body of the message (C# only)
  • Mailto: sending email messages with recipient address and optional subject and body (C# only)
  • Telephone call: defining the number to call
  • Windows Settings: launch a settings page on Windows 10 (PC and mobile)

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

  • Business card (vCard): convert a Contact from the user's address book directly to a vCard record
  • iCalendar: store appointments and events on tags, integrates with WinRT calendar classes (alpha release)
  • Image: images in various format on NFC tags or embedded in a Smart Poster. Includes de/encoding of JPEG, PNG, GIF and other file formats

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.

For JavaScript, the NdefDemoJS directory contains a modern web application built with Vite that demonstrates the Web NFC API integration and various NDEF record types. The demo showcases social media records, URI records, and real-time NFC tag reading/writing. It requires Chrome for Android and works with physical NFC tags.

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:

  • NFC interactor for Windows Phone: powerful NFC tag reader / writer app
  • NFCsms: enables Windows Phone to send SMS messages from NFC tags

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)

Installation

Install the library using npm:

npm install ndef-library

ES Module Import

The library is distributed as ES modules for modern JavaScript applications:

import { NdefMessage, NdefRecord, NdefUriRecord, NdefTextRecord, NdefSocialRecord, NfcSocialType } from 'ndef-library';

Create a URI Record:

// Create NDEF Message
const ndefMessage = new NdefMessage();
// Create NDEF Uri Record
const ndefUriRecord = new NdefUriRecord("https://www.nfcinteractor.com");
// Add record to message
ndefMessage.push(ndefUriRecord);
// Get byte array for NFC tag
const byteArray = ndefMessage.toByteArray();

Create Social Media Records:

// Create social media records for modern platforms
const xRecord = new NdefSocialRecord("username", NfcSocialType.X);          // X (formerly Twitter)
const linkedinRecord = new NdefSocialRecord("andreasjakl", NfcSocialType.LinkedIn);
const instagramRecord = new NdefSocialRecord("username", NfcSocialType.Instagram);
const threadsRecord = new NdefSocialRecord("username", NfcSocialType.Threads);

// Add to message
const socialMessage = new NdefMessage([xRecord, linkedinRecord]);

Web NFC API Integration:

The library works seamlessly with the Web NFC API (Chrome for Android):

// Writing to NFC tag
const ndef = new NDEFReader();
await ndef.write(message);

// Reading from NFC tag
await ndef.scan();
ndef.onreading = event => {
    const message = new NdefMessage(event.message.records.map(r => 
        new NdefRecord(r.recordType, r.mediaType, r.data, r.id)));
    console.log(message.records);
};

Create a raw NDEF Message by defined input:

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

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

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

const ndefMessage = 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 designed for modern web applications and supports ES modules.

NPM Installation

Install the library using npm:

npm install ndef-library

Browser Compatibility

Important: The JavaScript library uses the Web NFC API, which is currently only supported in Chrome for Android (version 89+). Desktop browsers and other mobile browsers do not yet support the Web NFC API.

Demo Application

The NdefDemoJS directory contains a modern demo application built with Vite that demonstrates:

  • Reading and writing NDEF messages using the Web NFC API
  • Creating various record types including social media records
  • Modern ES module architecture
  • Responsive design for mobile devices

To run the demo:

  1. Navigate to the NdefDemoJS directory
  2. Install dependencies: npm install
  3. Start the development server: npm run dev
  4. Access from your Android device using Chrome browser at http://[YOUR_IP]:5173/src

For production deployment, build the demo with npm run build and deploy the dist folder to any web server with HTTPS support.

Version History (C#)

4.1.0 - August 2017

  • Main NFC / NDEF library is ported to .NET Standard 1.4
  • Use latest .net Core beta version of iCal.net library instead of custom UWP port
  • Removed deprecated code and versions from the repository, including Silverlight and WinRT versions + example apps.

3.1.1 - January 2016

  • New extension library targeting Windows 10 / UWP, including Mime/Image and vCard-records
  • New NdefWindowsSettingsRecord to launch all settings pages for Windows 10 (both desktop and mobile)
  • Deprecated NdefNokiaAccessoriesRecord and NdefWpSettingsRecord from Windows Phone 8
  • Updated Geo tag record for Windows 10, also allowing a title for the DriveTo and WalkTo types
  • New Demo app for Windows 10 (UWP, Universal Windows Platform)

Changes

  • Rebased library from Codeplex to Github

3.0.3 - March 2015

  • Changed contact information

3.0.2 - July 2014

  • New Windows Phone 8.1 settings schemes
  • More flexibility when setting properties of the Bluetooth Secure Simple Pairing record from enum values

3.0.1-alpha - July 2014

  • Compatible to Windows Phone 8.1 WinRT-based Apps and Universal Apps
  • New Bluetooth and Connection Handover classes. Enables full support for writing Bluetooth handover NFC tags as found in accessories like loudspeakers and headsets.
    • Bluetooth Secure Simple Pairing record, including extensive ready made type definitions according to Bluetooth Core specification 4.1 from December 2013
    • Connection Handover standard by the NFC Forum, version 1.3 (January 2014)
      • Handover Select record
      • Handover Alternative Carrier record
      • Handover Error record

2.0.0.2 - April 2014

  • New: NDEF record classes
    • Business card records (vCard): Windows 8 (depends on integrated vCard library) / Windows Phone 8 (no 3rd party dependencies)
    • iCalendar records: Windows 8 (alpha version, depends on 3rd party DDay.iCal library)
    • MIME / Image records: Windows 8
  • New: Windows 8.1 Demo app
  • New: TagGenerator utility app (console-based) to create files containing NDEF messages
  • Changed structure: core NDEF library + platform-specific extension library to enable real-life use cases (e.g., converting a WinRT Contact to a vCard record)
  • Added: HERE Maps navigation schemes for Geo records
  • Added: Power and screen rotation for WP Settings records

1.1.0.0 - July 2013

  • SMS handling improved: allows wrong sms:// scheme, parses URLs without body text and/or number
  • Social record adds Google+ and the FourSquare protocol
  • Adds dictionary of available Nokia Accessories including their product names
  • NearSpeak record now understands cloud-based tags
  • Improved comments for NDEF message, removed debug output

Version History (JavaScript)

2.0.0 - September 2025

  • Complete modernization of the JavaScript library
  • Rebuilt as ES modules for modern web applications
  • Added comprehensive social media record support (X/Twitter, LinkedIn, Instagram, Threads, TikTok, Facebook)
  • Integration with Web NFC API for Chrome on Android
  • New Vite-based demo application with modern development workflow
  • Improved TypeScript support and better development experience
  • Enhanced documentation and examples

Known Browser Compatibility:

  • Web NFC API support is currently limited to Chrome for Android (version 89+)
  • Desktop browsers and other mobile browsers do not yet support the Web NFC API
  • HTTPS is required for Web NFC API in production environments

1.0.0 - Work in progress

  • Fixes for the compiled JavaScript library (include const to compiled version)
  • Use var instead of const for better JavaScript compatibility
  • New JavaScript demo app using Apache Cordova for Android, Windows Phone and Windows
  • Rebased library from Codeplex to Github

0.0.1 - March 2014

  • Initial Version

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