3 Home
tompaana редактировал(а) эту страницу 2014-11-28 05:25:57 -08:00

Getting started

NFC NDEF Tag Reader is compatible with Windows Phone 8 devices. Windows Phone 7 is not supported because the application is specifically demonstrating the use of the Proximity API that is not available in Windows Phone 7.

To test the application on a phone, you need to have a developer unlocked Windows Phone 8 device. Download the XAP file from the latest release and install it on your device by using the Application Deployment tool that comes with the Windows Phone 8 SDK.

To build the application, download the application solution source code and open the NfcNdefTagReader.sln file in the Microsoft Visual Studio Express for Windows Phone 8. Start building and running the application by hitting F5 or selecting Start Debugging from the Debug menu.

Design

NFC NDEF Tag Reader UI is very minimal and heavily focuses on the main purpose of the app - reading tags and displaying information regarding them.

Screenshot 1 Screenshot 2

Implementation

Architecture overview

Architecture

Subscribing to receive NFC events

To publish/subscribe messages to/from proximate devices, we need to use ProximityDevice under Windows.Networking.Proximity with ID_CAP_PROXIMITY capability defined for the application. To create an instance of a ProximityDevice class and activate the default proximity provider, we need to use the static GetDefault() method of ProximityDevice:

public static ProximityDevice GetDefault()

In order to read a NFC NDEF tag, the application needs to subscribe for NDEF messages using SubscribeForMessage() of ProximityDevice.

public long SubscribeForMessage(string messageType, MessageReceivedHandler messageReceivedHandler)

The messageType parameter defines the type of message to deliver to subscribers. To subscribe to a generic NDEF tag, the messageType needs to be "NDEF". The messageReceivedHandler parameter defines the handler that the proximity provider will call when it delivers a message. The messageReceivedHandler is a MessageReceivedHandler type that follows the syntax described below:

public delegate void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)

After subscription, when a tag with NDEF message is touched with the device, the messageReceivedHandler gets called with the message parameter that holds the NDEF tag message to be handled.

Parsing the data

In order to parse a ProximityMessage that holds NDEF message, we need to understand the structure of NDEF message. The specification of NDEF can be found from [NFC Forum: Specifications](http://www.nfc-forum.org/specs/ NFC Forum: Specifications). Each NDEF message could have multiple NDEF records. Records can be in different Record Type Definitions (RTD): URI RTD, text RTD, and smart poster RTD. We provide the RTD classes such as NdefUriRtd and NdefTextRtd (see NdefRecordTypeDefinitions.cs) to represent the different RTDs, a record class NdefRecord to represent the record layout, and a utility class NdefRecordUtility to help in reading data from the message, record, and RTDs.

If we want to parse the ProximityMessage into a list of records, we need to use the ReadNdefRecord() method of NdefRecordUtility:

static public void ReadNdefRecord(DataReader buf, List<NdefRecord> list) 

It can be used in the MessageReceivedHandler simply as shown below:

// message is ProximityMessage instance from MessageReceivedHandler
// recordList is List<ReadNdefRecord>
using (var buf = DataReader.FromBuffer(message.Data)) 
{
    NdefRecordUtility.ReadNdefRecord(buf,recordList);
}

If we want to read a URI RTD from a record, then we use the ReadUriRtd() method of NdefRecordUtility:

static public void ReadUriRtd(NdefRecord record, NdefUriRtd uri)

If we want to read a text RTD from a record, then we use the ReadTextRtd() method of NdefRecordUtility:

static public void ReadTextRtd(NdefRecord record, NdefTextRtd text)

Further reading