Download Article

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.

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.

Protocol

Publication

Subscription

Message Contents

Windows

X

X

Binary data

WindowsUri

X

X

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

WindowsMime

X

X

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:WriteTag

X

Windows message to be written to a static tag

WindowsUri:WriteTag

X

WindowsUri message to be written to a static tag

WindowsMime:WriteTag

X

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

LaunchApp:WriteTag

X

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

WriteableTag

X

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

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.

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.

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.