This project is read-only.

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: http://ndef.codeplex.com/SourceControl/list/changesets

Project setup

Ensure you have Nuget version >= 2.1 (see: https://nuget.org/).
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 / Windows Phone 8 project with Visual Studio 2012
  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
    1. Tools -> Library Package Manager -> Manage NuGet Packages for Solution...
    2. Search "Online" for "NDEF"
    3. Install the "NDEF Library for Proximity APIs (NFC)"
  4. Create your own NFC manager class, which takes care of initializing and handling the ProximityDevice - see bit.ly/ProximityAPI

More information about the NuGet package: https://nuget.org/packages/NdefLibrary
Debug symbols: http://www.symbolsource.org/Public/Metadata/NuGet/Project/NdefLibrary

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());

Last edited Dec 28, 2012 at 10:01 PM by mopius, version 12

Comments

mopius Jul 30, 2013 at 2:06 PM 
Missed answering the comment, for completeness - the instructions now already state where to find the AsBuffer() method:
using System.Runtime.InteropServices.WindowsRuntime;

I wasn't aware at the beginning that this could be an issue, as the ReSharper plug-in takes care of finding and adding this using statement.

nicodekker Feb 12, 2013 at 7:28 AM 
Hi,
I'm very happy with this library and working on a small solution. Although I'm facing one little problem I can't seem to resolve.
I want to write a tag containing two tekst record. Basically I'm doing the following:

var spRecord1 = new NdefTextRecord
{
Id = System.Text.Encoding.UTF8.GetBytes(111),
Text = "Record 1",
LanguageCode = "nl"
};

var spRecord2 = new NdefTextRecord
{
Id = System.Text.Encoding.UTF8.GetBytes(222),
Text = "Record 2",
LanguageCode ="nl"
};

var message = new NdefMessage();
message.Add(spRecord1);
message.Add(spRecord2);

Now comes the point that I want to write the message to a tag.

var msgArray = message.ToByteArray();
device.PublishMessage((writeToTag ? "NDEF:WriteTag" : "NDEF"), message.AsBuffer() , MessageWrittenHandler);

Here I'm stuck at the message.AsBuffer() part. It seems not be be recognised. Any advise is more than welcome.