Skip to content
mopius edited this page Oct 4, 2014 · 1 revision

Getting Started

This short guide will you through creating your first app that uses the NDEF library.

All classes in the library are fully commented following the XML / Doxygen standards. A complete HTML version of the documentation can be found in the /docs/ directory of the downloaded source code.

Project setup

Ensure you have Nuget version >= 2.1.

  • Update through: Tools -> Extensions and Updates... -> Updates (left sidebar) -> Visual Studio Gallery
    • If Nuget is not the most recent version, you will get an error message like this during the library installation: Install failed. Rolling back... Could not install package 'NdefLibrary'. You are trying to install this package into a project that targets 'WindowsPhone,Version=v8.0', but the package does not contain any assembly references that are compatible with that framework. For more information, contact the package author."
  1. Create a new Windows 8.1 / Windows Phone 8.1 project with Visual Studio 2013
  2. Open the application manifest file and add the Proximity capability
  3. Add the NDEF library to your project using the NuGet package manager of Visual Studio
    • Tools -> Library Package Manager -> Manage NuGet Packages for Solution...
    • Search "Online" for "NDEF"
    • Install the "NDEF Library for Proximity APIs (NFC)"
  4. Create your own NFC manager class, which takes care of initializing and handling the ProximityDevice - more information

More information about the NuGet package and the Debug symbols

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.

Reading Tags

In your NFC manager class, subscribe for messages of type NDEF. You should save the subscription ID to a member variable, so that you can remove the subscription again when it is no longer needed.

_subscribedMessageId = _device.SubscribeForMessage("NDEF", MessageReceivedHandler);

In the handler method that you supplied in the subscription call, convert the byte array to an NdefMessage, and then loop over the contained records. For each record, you can check its type and then create the appropriate specialized class for direct access to the record's properties:

private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage 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));
        // Check the type of each record - handling a Smart Poster, URI and Text record in this example
        var specializedType = record.CheckSpecializedType(false);
        if (specializedType == 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());
            // You can also check the action (if in use by the record), 
            //mime type and size of the linked content.
        }
        else if (specializedType == typeof(NdefUriRecord))
        {
            // Convert and extract URI info
            var uriRecord = new NdefUriRecord(record);
            Debug.WriteLine("URI: " + uriRecord.Uri);
        }
        else if (specializedType == typeof(NdefTextRecord))
        {
            // Convert and extract Text record info
            var textRecord = new NdefTextRecord(record);
            Debug.WriteLine("Text: " + textRecord.Text);
            Debug.WriteLine("Language code: " + textRecord.LanguageCode);
            var textEncoding = (textRecord.TextEncoding == NdefTextRecord.TextEncodingType.Utf8 ? "UTF-8" : "UTF-16");
            Debug.WriteLine("Encoding: " + textEncoding);
        }
    }
}

Writing tags

Create a method to write tags in your Nfc Manager class, and use the following code to create a new Smart Poster record and write it to a tag. You can also set additional properties of the Smart Poster or add more titles in different languages. Creating other types (URI, Text) works in a similar way.

Add the following using statement to your class file:

// For AsBuffer() method to convert a byte array (byte[]) to an IBuffer
using System.Runtime.InteropServices.WindowsRuntime;

Code to write a tag / publish to another device:

// 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
_device.PublishBinaryMessage("NDEF:WriteTag", msg.ToByteArray().AsBuffer());

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