Tech Blog

Jay's Technical blog

Proximity and WP8 - Introduction

16 October 2013
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

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.

My Goal

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.

Background

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..

Sample App

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.

Final Thoughts

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.


WP8 App/Page Base Navigation Classes

15 October 2013
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

On my utilities page, I need to document a few of the items. The next one up is my App/Page Base Classes which you can get here at it’s repository.

Background

I created this set of classes in response to a DVLUP challenge criteria (Fast App Switching). The main reason I created these was because I wanted to be able to easily handle the various types of navigation to my pages with minimal thinking on my part. With Fast App Switching, you detect that via the Application_Activated event in the application (it gets passed to you via an event argument object). For me the best solution was to indicate this via a property on the application, and then I needed to remember to clear that property once I had responded to it. This last part was what spawned the new mechanism. Essentially, I split the page’s OnNavigatedTo method into 3 separate methods (that need to be overridden in child pages). The 3 methods are:

  • OnNormalNavigatedTo – a normal page-to-page navigation (or app launch) where you need to do an initial load of data
  • OnResumedFromTombstone – indicates that the page is being launched after a tombstoning situation (where you need to reload the page’s state
  • OnFastSwitchedTo – indicates that the page is being launched after returning from the OS, but nothing needs to be reloaded

All of these methods are passed a single argument of type: NavigationEventArgs which is the normal argument passed to the OnNavigatedTo Event. You can of course override the OnNavigatedTo event (and remember to call base.OnNavigatedTo(e) so that the new overrides get called.

Installation

There is a bit of work to wire all this up. First of all you need to grab the 2 new class files and drop them into your project. Next go ahead and compile the project (BTW, these classes MAY also work in WP7 projects). Now you need to wire up the application class. You need to do 2 things.

Set the App.xaml.cs class to inherit from DTNet.StateAwareApplication instead of Application. That will look something like this:

namespace MyProject
{
publicpartialclass App : DTNet.StateAwareApplication
    {


Additionally, you need to change the Application_Activated override method a little. It should look like this

protectedoverridevoid Application_Activated(object sender, ActivatedEventArgs e)
        {
base.Application_Activated(sender, e);
// Add any additional code you need in this event here
        }


Next you need to modify the app.xaml file by changing the base class. You probably have something that looks like this:

<Application
x:Class="ProximityToy.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
<local:LocalizedStringsxmlns:local="clr-namespace:ProximityToy"x:Key="LocalizedStrings"/>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching"Closing="Application_Closing"
Activated="Application_Activated"Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>

 

You will need to add the DTNet namespace and change the “<Application” (or “</Application”)  to “<dtnet:StateAwareApplication “ (or “</dtnet:StateAwareApplication”). To convert the above xaml it would look like this:

<dtnet:StateAwareApplication
x:Class="ProximityToy.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:dtnet="clr-namespace:DTNet">
<!--Application Resources-->
<dtnet:StateAwareApplication.Resources>
<local:LocalizedStringsxmlns:local="clr-namespace:ProximityToy"x:Key="LocalizedStrings"/>
</dtnet:StateAwareApplication.Resources>
<dtnet:StateAwareApplication.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching"Closing="Application_Closing"
Activated="Application_Activated"Deactivated="Application_Deactivated"/>
</dtnet:StateAwareApplication.ApplicationLifetimeObjects>
</dtnet:StateAwareApplication>

See how event the resources change. This is all that needs to be done to the application file.

You need to make similar changes for every page. Here’s a sample Xaml page that (with nothing in it):

<phone:PhoneApplicationPage
x:Class="ProximityToy.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Gridx:Name="LayoutRoot"Background="Transparent">
</Grid>
</phone:PhoneApplicationPage>

We need to make this page reference the base class of the DTNet.StateAwareNavigationPage. We’ll do this just like we did for the App.xaml by adding the namespace reference and changing every occurrence of “<phone:PhoneApplicationPage” or “</phone:PhoneApplicationPage” to “<dtnet:StateAwareNavigationPage” or “</dtnet:StateAwareNavigationPage.” Here’s the xaml from above fixed:

<dtnet:StateAwareNavigationPage
x:Class="ProximityToy.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:dtnet="clr-namespace:DTNet">

										
<!--LayoutRoot is the root grid where all page content is placed-->
<Gridx:Name="LayoutRoot"Background="Transparent">
</Grid>
</dtnet:StateAwareNavigationPage>

(the above example only had 2 text changes plus the namespace reference, but if you may have more references to “phone:PhoneApplicationPage” you will have to change them, so be aware).

Next you make the codebehind’s (aka PAGE.xaml.cs file) class derive from DTNet.StateAwareNavigationPage. This is pretty easy. You’ll also want to add the 3 new method overrides. It will look something like this:

// Using above this line
namespace MyProject
{
publicpartialclass MainPage:DTNet.StateAwareNavigationPage    
    {
protectedoverridevoid OnFastSwitchedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
        }
protectedoverridevoid OnResumedFromTombstone(System.Windows.Navigation.NavigationEventArgs e)
        {
        }
protectedoverridevoid OnNormalNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
        }

										
        // more code follows

 

Finally you will want to take the OnNavigatedTo Event (in your code) and remove the code placing it into the appropriate methods (you will probably create a method with the common code to reduce duplication of code). Finally delete the OnNavigatedTo Event (or at the least add a line that routine to call the base.OnNavigatedTo routine).

Just in case you missed this point, you do the page changes to every page you want to enable this functionality in.

Admittedly it is a lot of work for an existing project, but for a new one it’s not that bad. I have been meaning to create templates for this, but have never really had the time. Anyway, that’s it. Let me know what you think…

Just a reminder the 2 classes can be found on project’s repository.


SpeechHelper class

14 October 2013
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

I wrote a mini library which features a main helper class for Speech. I did this because I wanted to bring both the talking and the listening (recognition) aspects into a single class. I expose all the regular classes as well through the same object making it easy for you to simply use the helper class without having to know all the ins and outs of the speech classes. You can get the code from it’s online source code repository.

Here’s some c# code to show you how easy this is to use:

// First of all this class brings together all the speech


// related classes and offers some shortcuts. The overall
// goal is to make it so we only need to know 1 class
await SpeechHelper.Instance.Say("Hello. What is your name?");

var name = await SpeechHelper.Instance.UI.RecognizeWithUIAsync();

// Next we make some things a little easier to do like handling
// speech recognition with a word list.
var listOfWords = (
new [] {
"Blue",
"Red",
"Green",
"Purple",
"Orange",
"Yellow" }).ToList();

// BTW, the prompt here ("What is your favorite color?" is an
// optional parameter
var favColor= await SpeechHelper.Instance.RecognizeWordInListAsync(
listOfWords,
"What is your favorite color?", true);
// Last parameter indicates that we want to use the SpeechRecognizer
// with the default UI

var age await SpeechHelper.Instance.RecognizeNumberAsync(
"How old are you?", true);

// One caveat to this is that if the recognition fails you will get an
// empty string (which we aren't bother to test for here).
var prompt = String.Format("Hello, {0}! I understand that" +
" you are {1} years old and like " +
"the color {2}. Is this correct?",
name,favColor,age);

// Finally detect a boolean response (understands a lot of english variations)
var correct = await SpeechHelper.Instance.AskYesNoQuestionAsync(prompt, true);

You can find the links to the repository for this over on the Libraries site (there are some additional things in this dealing with SRGS which is not complete).

------------------

Again, you can get it here.


New Dev Utils/Libraries page

14 October 2013
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

 

I recently was asked to create a single page with my utility classes/libraries that I have built.

You can find the beginnings of that right here: http://bit.ly/18f9096

I am in much need of documenting some of these (hence, they hadn’t been talked about anywhere other than with people who meet me personally).

I will try to blog about these as I have time. I will get the first one out of the way today (following this post)..


Adventures in Win8, WP8, and Game Maker Studio

30 May 2013
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

My son and I have been playing with Game Maker Studio and have been building apps for Win8 Store and Windows Phone Store. This is a place holder post where I may link everything together in one handy place.