Getting started
Compatibility
Car Trumps is compatible with Windows Phone 8 devices and has been tested on Nokia Lumia 820 and Nokia Lumia 920. Windows Phone 7 is not supported as the application is specifically demonstrating the use of the new Windows Phone 8 Proximity API.
Using the prebuilt installation package
Download the XAP file and install it on your device by using the Application Deployment tool that comes with the Windows Phone 8 SDK.
Building the application
Download the application solution source code and open the CarTrumps.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
Car Trumps is a Windows Phone 8 example game demonstrating the use of the ProximityDevice API to transfer data between two devices over the NFC. It is based on a well-known children's card game. Basically the aim of the game is to select such a card and statistic from your hand which you think can beat the opponent's selection. This is a two player game, and requires two phones with NFC support.
The game starts when the players touch the phones together. A random hand is given to both players from the deck. Both players select a card and pick one statistic from it, and touch the devices together to compare the selections. Both players must select the same statistic. The winner gets the opponent's losing card. The game ends when either player runs out of cards.
Implementation
Architecture
User interface
User interface of the application consists of MainPage
, GamePage
, WinnerPage
, and informational InfoPage
, all derived from PhoneApplicationPage
. MainPage
is the landing page for this example application, instructing the user to touch the opponents phone to start a new game. User makes his game selections on GamePage
displaying a car with its statistics. WinnerPage
is shown after determining the winner of each round.
Windows Phone Proximity API
Windows Phone Proximity APIs under the namespace Windows.Networking.Proximity
are used for communicating over NFC. To be able to use proximity services the application must have ID_CAP_PROXIMITY
capability specified in WMAppManifest.xml
file.
Transferring data over the NFC
The application uses ProximityDevice.PublishBinaryMessage
and ProximityDevice.SubscribeBinaryMessage
for data exchange between two devices. The exchanged messages are standard NDEF messages, with application specific XML content.
The initialisation of the ProximityDevice
is simple:
public NfcManager()
{
// Use and initialise the default proximity device.
_proximityDevice = ProximityDevice.GetDefault();
...
}
Peer-to-peer protocol requires synchronisation in message sending and receiving from both sides to eliminate mutual message flooding that would happen with non-controlled peer-to-peer. The synchronisation implementation in protocol causes considerable overhead. Client-server scheme is simpler, so it is chosen.
Protocol
There are three types of messages in the protocol:
- Initialisation message
- Deal cards message
- Card exchange message
Initialisation message
Before conversation starts between the devices, there is a negotiation phase with initialisation message exchange. The initialisation message is sent and received by both devices. The message conveys a random number generated by each counterpart. When the message is received, each recipient compares the received number to its own to decide its role in the conversation. After the message negotiation is complete, "Client" devices will subscribe to "server" messages until the end of the application session.
public class NfcInitiationMessage
{
...
[DataContract]
public class InitialMessage
{
[DataMember(Name = "devicetime")]
public double devicetime;
}
...
}
Note that the member variable name "devicetime" is a bit confusing. It does not have anything to do with actual device time; it is just a random number based on the board clock.
Deal cards message
Deal cards message is used to offer a card set to client when the game starts. The message data is shown below:
public class NfcMessage
{
[DataContract]
public class Message
{
...
/// <summary>
/// Used in EDealCards message. Random Card IDs from the deck (the hand).
/// </summary>
[DataMember]
public int[] CardIds
{
get;
set;
}
...
}
}
Card exchange message
Card exchange message is used during the game. The message data is shown below:
public class NfcMessage
{
[DataContract]
public class Message
{
...
/// <summary>
/// Used in EShowCard message. The id of the selected card.
/// </summary>
[DataMember]
public int CardId
{
get;
set;
}
/// <summary>
/// Used in EShowCard message. The selected card property.
/// </summary>
[DataMember]
public string SelectedCardProperty
{
get;
set;
}
...
Here is an example of the message body passed via NFC:
<Message xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CarTrumps.nfc">
<CardId>0</CardId>
<CardIds xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:int>4</d2p1:int><d2p1:int>1</d2p1:int><d2p1:int>7</d2p1:int><d2p1:int>2</d2p1:int><d2p1:int>6</d2p1:int><d2p1:int>0</d2p1:int>
</CardIds>
<MessageType>EDealCards</MessageType>
<SelectedCardProperty i:nil="true" />
<UniqueId>877800297</UniqueId>
</Message>
A ShowCard
message is exchanged when the players have selected a card and statistic, and touch the devices together. The message contains the selected card id and the selected statistic.
Here is an example of the message:
<Message xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CarTrumps.nfc">
<CardId>4</CardId>
<CardIds xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" />
<MessageType>EShowCard</MessageType>
<SelectedCardProperty>Engine</SelectedCardProperty>
<UniqueId>0</UniqueId>
</Message>
See NfcInitiationMessage.cs, NfcMessage.cs and NfcManager.cs for more information.
Interoperability
The ProximityDevice
uses standard NDEF messages pushed from one device to another. You can receive the above XML messages from a Windows Phone 8 device running the Car Trumps with an Android device by using, for example, NFC Reader. See [NFC Basics](http://developer.android.com/guide/topics/connectivity/nfc/nfc.html NFC Basics), Android Beam
in particular if you are interested in implementing a similar application.
Further reading
See Opening sockets with NFC for more in-depth information on the use of Proximity API and NFC functionality.
There's also another project dealing with Proximity API and NFC, NFC Talk, a simple chat application demonstrating NFC tap initiated connection establishment and further data transfer.
Licensing
All the car images were taken from en.wikipedia.org. Here is a complete list of the used images:
- http://en.wikipedia.org/wiki/File:2005-07_Volvo_V70.jpg
- http://en.wikipedia.org/wiki/File:Volvo_V70R.jpg
- http://en.wikipedia.org/wiki/File:Volvo-850-wagon-rear.jpg
- http://en.wikipedia.org/wiki/File:1988-1991_Volvo_240_GL_station_wagon_(2011-06-15)_01.jpg
- http://en.wikipedia.org/wiki/File:Volvo-XC60-DC.jpg
- http://en.wikipedia.org/wiki/File:3rd_Volvo_XC70_--_11-26-2011.jpg
- http://en.wikipedia.org/wiki/File:2nd_Volvo_XC70_--_03-16-2012.JPG
- http://en.wikipedia.org/wiki/File:2011-2012_Volvo_C30_--_01-07-2012.jpg
- http://en.wikipedia.org/wiki/File:2000-2002_Volvo_S40_2.0_sedan_(2011-11-17).jpg
- http://en.wikipedia.org/wiki/File:2005-07_Volvo_V70.jpg
The images are licensed under GNU Free Documentation License.
The example application and its source code is licensed under a permissive proprietary license.