Sample Windows* Store App for NFC

Download Article

Download Sample Windows* Store App for NFC [PDF 299KB]

Introduction

This is a sample application that shows how to use Windows 8 Proximity API for NFC (Near Field Communication). The Application is written in JavaScript. The application first advertises the device via NFC and then finds the peer device to connect. After the device connects to the peer device, then it can send message to the peer device. I will give a brief explanation of each function, and then describe the basic life cycle of the sample apps.

WinJS.log(message, tag, type):

This helper function can make sending different types of messages to different div boxes easy.

initializeProximityDevice()

This function checks to see if there is already a proximity device assigned in the code. If there is no proximity device assigned, it will get the system default NFC device. If there is no device available, an error will be reported.

triggeredConnectionStateChangedEventHandler(e)

This function lets you handle what happens when the connection state changes.

connectionRequestedEventHandler()

The connectionRequestedEventHandler function lets you designate what happens when another NFC-enabled device tries to request a connection from you.

peerFinder_AcceptRequest()

This function lets the connection complete if the user clicks the Accept Connection button. The function also reports the status to the user.

socketError(errMessage)

This helper function lets you set the output location for error messages.

startReader(dataReader)

This function reads the entire message coming through the dataReader and then writes it out to the user receiving the message.

peerFinder_StartFindingPeers()

Sets the peerFinderStarted flag and then starts the PeerFinder function in the ProxNS library. The function will find all peers asynchronously and populate the drop-down <select> box when complete.

startSendReceive(proximitySocket)

This function starts the process for sending and receiving messages by creating the dataWriter that is to be used in the startReader(dataReader) function. StartSendReceive() also sets both the input and output stream of the socket.

peerFinderConnect()

This function lets you define what happens once a connection is made between peers.

peerFinder_Send()

This function grabs the string in the <Text area> box and then uses the dataWriter to write the message to the socket.

Basic Lifecycle

An application starts by doing a check of the available devices and what types of connections are allowed by that device. If the device / connection types are available, the event handlers are set accordingly. The application flow is controlled by enabling / disabling buttons. If no connection can be made, the send message button will not appear. NFC-capable devices must first advertise themselves by attempting to signal others. When the “Find Peers” button is clicked, the device looks for other device signals and then populates the drop-down box with all the device names it finds. The user on the device that clicked “Find Peers” can now select which device to connect to and click connect button connect. The other user must accept the connection by clicking the “Accept Connection” button. After the connection is made, both devices are free to send messages to each other.

About the Author

Cristiano is a Software Applications Engineer with Intel's Software and Solutions Group. Over the past year, Cristiano has worked nearly exclusively with Microsoft* Windows 8 and Intel's new tablet platforms, and has a written a variety of Windows Store applications.


Intel and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Pour de plus amples informations sur les optimisations de compilation, consultez notre Avertissement concernant les optimisations.