About 3 weeks ago, I decided to add what I called “NFC” to my WP8 Friend GPS App (currently called “Where U @?”, but with the next release will be called “Friend Pilot”). This started a journey for me where I had to wrap my head around the whole Proximity library. What I found was that the topic is somewhat confusing. It is not helped by the samples that are out there from Microsoft. I mean there are good examples, BUT those examples assume the type of interaction you are looking to create use BOTH MVVM and IAsync handlers (the former I can follow, the latter hurts my brain, and for the person just learning the mountain is pretty high just to learn how to use the Proximity library). [The one talk I found on the topic using the aforementioned complex/advanced code didn’t help with the reference to “magic happens” (which in his case meant that you tap and things just automatically happen for you.. this is untrue.. you have to call things to make “magic happen”, so therefore it’s not magical to the developer)].
In this post I want to give some background behind Proximity so you can understand what is going on. I will follow this up with a post on using my ProximityHelper class, and then will come back and give a few more specifics on Proximity. Essentially I feel the topic has been covered, but doesn’t really lend itself to the beginner or even intermediate developer. I believe that much of the discussions here will also be helpful for Windows Store (C#/VB) development as my perusal of the WinRT framework shows the same Proximity classes.
For me I wanted a simple interaction, while running my app on 2 phones (that have NFC), tap them together and exchange information. Additionally I wanted to have some way for a device without NFC to be able to connect and make an exchange of info as well (but I wasn’t sure this was possible originally). I suspect that this is what most people want to do.
First of all, Microsoft’s ProximityFinder class (the heart of the ProximityLibrary) actually combines a number of technologies together into a single API (NFC --if available, Bluetooth, and regular 802.11x Wi-Fi). You don’t need to be a guru with any of these technologies. It just works (when you get everything set up and you don’t need to worry about the underlying technology being used). BTW, you do use this class in different ways to get a connection based on what you are trying to do (like if you are making your phone available for a connection or if you are actively looking for a connection).
Second, you need to understand that NFC is really just there to set up a connection on one of the other technologies. NFC is the “tapping” part of the exchange, but after that everything happens over either Bluetooth or 802.11x Wi-Fi.
Thirdly, once you make a connection you are given a socket (specifically a “StreamSocket”). At this point you have to implement your own protocol of sorts (which if that sounds hard.. it can be.. As a result of this I created a SocketHelper class that really grabs some of the code from one of the Microsoft Samples).
Finally, one of the side benefits I found out was that once you get things set up that once you set one phone up to connect, NFC can be used to both launch your app on the other phone AND create the connection. (This is very cool!)
More on NFC
Some of you may be wondering about NFC at this point. You may have looked at a sample and it looks like you could communicate between two devices, etc. The reality is that you can communicate with NFC this way, but the devices have to remain very close. Ultimately, I think NFC is best used for short messages between 2 devices where 1 way communication is all that you need. I am admittedly still just figuring out NFC, and there are topics like writing to an NFC tag, that I have yet to really press down on. I am planning an NFC helper as well..
I wrote an app as a sample app to test all this out. It’s called Proximity Helper and (if it passes certification) should be in the Windows Phone Store today. It let’s you test out various scenarios with my library. It does talk about having a “client” and a “server” phone, but this isn’t entirely accurate. In my app’s case communication is done in a one way fashion. In reality Proximity allows for 2 way communication. If you want to look to see what all I am doing you can, of course, go look at the source for my sample at it’s repository.
This should lay a decent ground work for you to understand Proximity. I will be following this up with a couple more posts. One on how to use my ProximityHelper in your projects to start the communication between 2 apps; one on my SocketHelper and ways you can use it to create a simple protocol for communicating between 2 apps (hint: I’ll be using JSON strings); and finally a post on the technicals of Proximity.. how it works, etc.