Using NFC (Near Field Communication) from Windows* 8 Applications

By Hallade, Xavier

Download Article

Download Using NFC (Near Field Communication) from Windows 8* Applications [PDF 617KB]

Near Field Communication, or NFC, is a short-range communications protocol for Contactless Transactions.

NFC is a high-frequency, wireless communication technology that enables the exchange of data between two NFC compatible devices within a short range distance (usually <2 cm). NFC devices can be active: Smartphone, Tablet, Ultrabook™ as well as passive: Tags, Smart cards, etc.

The NFC technology is intended to be used to transfer small amounts of data. The data transfer rate is low, but NFC is low-power, easy to use, and feels instantaneous (<1/10 s).

Using the NFC API from Windows Store and Desktop Applications

You can use NFC in Windows 8 from desktop applications as well as from Windows Store apps. Both of them use WinRT to access the NFC device.

Obviously, use of WinRT is seamless from Windows Store apps, but if you intend to use it from desktop applications, please first follows this guide: how to use WinRT from Windows 8* Desktop Applications.

To get access to the NFC device, you have to instantiate a ProximityDevice object with ProximityDevice class. You can do so by calling Windows.Networking.Proximity.ProximityDevice.SetDefault().

Then, using that object you can subscribe to DeviceArrived, DeviceDeparted events, and publish/subscribe to different types of messages.

ProximityDevice message types

The table lists the different types of messages that are provided by the API just for convenience, to avoid having to handle the full NDEF messages when setting the message content.

The Windows messages are fully compatible with standard NFC messages (NDEF), and you can also directly manipulate NDEF messages.

ProtocolPublicationSubscriptionMessage Contents




Binary data




UTF-16LE encoded string that is a URI.use PublishUriMessage




Message data of a specific mime type, for example, "WindowsMime.image/jpeg". You can subscribe to "WindowsMime" messages without specifying the mimetype. If so, the first 256 bytes received is the ASCII mime type string.



Windows message to be written to a static tag



WindowsUri message to be written to a static tag



WindowsMime message to be written to a static tag, for example, “WindowsMime:WriteTag.image/jpeg”



Write a tag that can be used to launch a specific app. More information on msdn.



If a writeable tag is brought in to proximity, a proximity message is received that contains an int32 indicating the maximum writeable size.




Properly formatted NDEF records



Application-defined NDEF records (TNF field value of 0x04)



Properly formatted NDEF mime message (TNF field value of 0x02), for example, "NDEF:MIME.image/jpeg".



Properly formatted NDEF message of a type defined by a URI namespace (TNF field value of 0x03), for example, "NDEF:URI.".



Properly formatted NDEF message of a type defined by the NFC forum (TNF field value of 0x01). An example of this type is "NDEF:wkt.U" for the well known type of URI.



Message data that should be written to an NFC forum standard static tag.



Untyped NDEF message (TNF field value of 0x05)

source: msdn

When your application subscribes to a message, it doesn't override the OS behavior, i.e., Windows might ask the user to open the NFC message using a default handler if it exists. Look at the second part of this article to directly integrate with the OS.

Publishing a URI

The WinRT API provides a method to easily publish a WindowsUri type:

	Windows.Networking.Proximity.ProximityDevice device = 
	   long messageId = 
	   device.PublishUriMessage(new Uri(uri)); //Reuse messageId to unpublish that message.

Registering a delegate to process WindowsUri incoming messages

	using Windows.Networking.Proximity; //access NFC device
	using Windows.Security.Cryptography; //help decoding binary messages
	        private void startListeningToURIs()
	          ProximityDevice proximityDevice = ProximityDevice.GetDefault();
	            if (proximityDevice != null){
	 //return value of SubscribeForMessage can be used later to unpublish it
	 new MessageReceivedHandler((ProximityDevice sender, ProximityMessage message) =>
	 String uri = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16LE, 
		message.Data).TrimEnd(''); // URI is retrieved and decoded from message

Writing a WindowsUri to a tag

	 using Windows.Networking.Proximity; //access NFC device
	 using Windows.Security.Cryptography; //help encoding binary messages
	 void startWritingURIToTag(String uri)
	 ProximityDevice proximityDevice = ProximityDevice.GetDefault();
	            if (proximityDevice != null){
	 tagWritingMessageId = proximityDevice.PublishBinaryMessage("WindowsUri:WriteTag",
	 CryptographicBuffer.ConvertStringToBinary(uri, BinaryStringEncoding.Utf16LE),
	 new MessageTransmittedHandler((ProximityDevice sender, long messageId) =>
	 //handle messageTransmitted event

You might want to check for incoming WriteableTag messages (it gives the amount of data accepted) before trying to write on them.

Using NFC seamlessly

The first part of this article showed you how to directly interact with an NFC device. In Windows 8, NFC is also integrated at the OS level, and you can use URIs and small-file-sharing functionalities seamlessly from your application, without having to deal directly with the NFC device.

Handling incoming URIs and files

Incoming URIs and MIME messages from NFC devices automatically trigger the default registered handler for the associated protocol or file type.

Desktop applications as well as Windows Store apps can be registered as default handlers for protocols or file types. Here is the MSDN documentation describing how to do that:

Publishing URIs and files using devices charms (for Windows Store apps only)

You can share content to NFC devices using the Devices charm.

First, subscribe to the Windows.ApplicationModel.DataTransfer.DataTransferManager.DataRequested event.

In the delegate, set a URI or StorageItem with the DataPackage. On an NFC-enabled device, you will be able to use the "Tap and send" functionality from the device charm bar.

You can use that sample to see how you can share a URI or a file from a Windows Store app using the Devices charm:

Select the second scenario (share link) or the fourth (share files), open the devices charm on an NFC-enabled device, and you'll see the "Tap and send" link:

Using the PeerFinder API (Windows Store apps only)

A direct socket can be easily established between two applications, using the PeerFinder WinRT API.

The NFC device can be used during the pairing step, then a communication channel over Wi-Fi* or Bluetooth* is used afterward. That API is restricted to Windows Store apps. You can find its documentation and code samples on msdn.

Для получения подробной информации о возможностях оптимизации компилятора обратитесь к нашему Уведомлению об оптимизации.
Иконка PDF using-nfc.pdf616 КБ
Возможность комментирования русскоязычного контента была отключена. Узнать подробнее.