×
Namespaces

Variants
Actions
Revision as of 21:52, 13 December 2012 by joaocardoso (Talk | contribs)

Write data to NFC tags using Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search

Note.pngNote: This is a community entry in the Windows Phone 8 Wiki Competition 2012Q4.

Article Metadata
Article
Created: rudyhuyn (04 Dec 2012)
Last edited: joaocardoso (13 Dec 2012)

Contents

Write a NFC tag using Windows Phone 8

It is possible with Windows phone 8 to write on NFC tags. NFC tags can take several forms such as stickers, key rings, cards or phone stand like Nokia Wireless Charging Stand (DT-910).

To do this, we will create an application that will:

  • detect the presence of a writable nfc tag
  • write data to the tag

Create Project

NFC communications are provided by ProximityDevice class of Windows.Networking.Proximity namespace. To use NFC, you need to add a specific Capabilities to WMAppManifest.xml file.

  • Open your WMAppManifest.xml and add the capability ID_CAP_PROXIMITY.

If you don't do this, an exception will be launched when you will try to access to ProximityDevice.

Test if NFC is available

NFC chipset aren't mandatory on windows phone, so before use NFC, it's necessary to test if NFC chipset is present.

 if (ProximityDevice.GetDefault() != null)
MessageBox.Show("NFC present");
else
MessageBox.Show("Your phone has no NFC or NFC is disabled");

Detect presence of writable nfc tags

We will first try to detect that a writable tag is here for instance to display a message.

ProximityDevice.GetDefault().SubscribeForMessage("WriteableTag", WriteableTagDetected);
private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("message");
});
}


Write content to nfc tag

The previous step is not necessary, but it can be interesting to give a feedback to the user.

To write to a writable tag, just publish a message of the following type:

Protocol Description
Windows:WriteTag Same as the Windows protocol, except that the content is intended to be written to a static tag. The message is not transmitted to any device except a writable static tag. This protocol is only valid for publications.
WindowsUri:WriteTag Same as the WindowsUri protocol, except that the content is intended to be written to a static tag. The message is not transmitted to any device except a writable static tag. This protocol is only valid for publications. No subtype.
LaunchApp:WriteTag Write a tag that can be used to launch a specific app with optional launch parameters. If you publish a LaunchApp:WriteTag message to a tag the result when that tag is tapped to a computer is the same as calling the PeerFinder.Start method and passing your launch parameters.

Write custom data or text

To write a custom data, you need to select the Windows:WriteTag protocol but be careful, a subtype is mandatory, we will use "NokiaTest" as subtype in the following example:

var dataWriter = new DataWriter() {UnicodeEncoding = UnicodeEncoding.Utf8};
dataWriter.WriteString("This is a test");
 
ProximityDevice.GetDefault().PublishBinaryMessage("Windows:WriteTag.NokiaTest", dataWriter.DetachBuffer(), MesssageTransmitted);
 
private void MesssageTransmitted(ProximityDevice sender, long messageId)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("The message is written");
});
}

Text data must be encoded in UTF-8.

To read this message, you need to subscribe to Windows.<subtype> messages (Windows.NokiaTest for the sample).

Write an url on the tag

It's pretty easy to write a link in an nfc tag, for example to share the address of your blog or your favorite website.

To do this, we will use the type WindowsUri:WriteTag, no subtype is necessary.

Uri must be encoded in UTF16-LE.

var dataWriter = new DataWriter() { UnicodeEncoding = UnicodeEncoding.Utf16LE };
dataWriter.WriteString("http://www.nokia.com");
ProximityDevice.GetDefault().PublishBinaryMessage("WindowsUri:WriteTag", dataWriter.DetachBuffer(),MesssageTransmitted);
 
private void MesssageTransmitted(ProximityDevice sender, long messageId)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("uri is written");
});
}

When a windows phone will read this tag, Internet Explorer will be automatically launched.

Launch an application using tag

It's possible to write a tag that can be used to launch a specific app with optional launch parameters. The message must be a UTF-16LE encoded string where the values are delimited by tab characters or null values in the following form:

<launch arguments>[tab]<app platform 1>[tab]<app name 1>...[tab]<app platform N>[tab]<app name N>

You must specify at least one app platform and app name.

For Windows Phone 8:

  • The app platform is "WindowsPhone"
  • The app name is the application id


To retrieve the app id of your application, you can find it on your Windows Phone Dev Center or on the www.windowsphone.com website. For example: http://www.windowsphone.com/en-us/store/app/wikipedia/fd40c569-2681-48df-9ff3-53ffd80aa9b5 the app id is {fd40c569-2681-48df-9ff3-53ffd80aa9b5}.

An example of this message is "param=test&toto=value\tWindowsPhone\{91000c5c-9943-43b8-aa65-7609d91057ef}". Of course, you can also support other app platforms.

var args = "param=test";
var appId="{91000c5c-9943-43b8-aa65-7609d91057ef}";
var launchAppMessage = args+ "\tWindowsPhone\t" + appId;
 
var dataWriter = new DataWriter() { UnicodeEncoding = UnicodeEncoding.Utf16LE };
dataWriter.WriteString(launchAppMessage);
ProximityDevice.GetDefault().PublishBinaryMessage("LaunchApp:WriteTag", dataWriter.DetachBuffer(),MesssageTransmitted);
 
private void MesssageTransmitted(ProximityDevice sender, long messageId)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("The message is written");
});
}

Retrieve parameters in your app

To retrieve the parameters of your app, you can write:

        protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String value;
NavigationContext.QueryString.TryGetValue("ms_nfp_launchargs", out value);
 
}

Save a contact on the tag

To describe a contact, you can use the vcard format. vCard is a file format standard for electronic business cards. They can contain name and address information, phone numbers, e-mail addresses, URLs, logos, photographs, and audio clips.

vCard specifications can be found here: http://tools.ietf.org/html/rfc6350

To write the vcard to the tag, we will use the The "WindowsMime:WriteTag" protocol with the subtype "text/vcard". Be careful the message must be a UTF-16LE.

var vcard = "BEGIN:VCARD\nVERSION:2.1\nFN:Jean Dupont\nN:Dupont;Jean\nTEL;CELL:+1234 56789\nEMAIL;INTERNET:jean.dupont@example.com\nEND:VCARD";
var dataWriter = new DataWriter() { UnicodeEncoding = UnicodeEncoding.Utf8 };
dataWriter.WriteString(vcard);
ProximityDevice.GetDefault().PublishBinaryMessage("WindowsMime:WriteTag.text/vcard", dataWriter.DetachBuffer(), MesssageTransmitted);
 
private void MesssageTransmitted(ProximityDevice sender, long messageId)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("The message is written");
});
}
5 page views in the last 30 days.