×

Quick Start

Prerequisites

In order to develop applications that use SensorCore SDK you will need Visual Studio 2013 with Windows Phone 8.1 SDK installed.

The SDK supports emulator target with the simulator classes but with greatly reduced functionality, so in practice you will also need a developer unlocked device that supports SensorCore SDK.

SensorCore SDK is supported by the latest Lumia devices running Windows Phone 8.1 which have Motion data enabled.

Getting started

As our first project we will be creating a simple Windows XAML application that displays the current Step Counter values.

Create a new project

Start by opening Visual Studio and creating a new Visual C# Windows Phone 8.1 project.
  1. Select File > New > Project...
  2. Select Installed > Templates > Visual C# > Store Apps > Windows Phone Apps > Blank App )Windows Phone) and give the name of the project as " HelloSensorCore "

Add SensorCore SDK to the project

Next step is to include the SensorCore SDK to the project. The packages are in NuGet format.

Now we can add the actual NuGet package to the project.
  1. Select Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
  2. Locate Lumia SensorCore SDK and click Install .
  3. When prompted to select projects, make sure our project is checked and click Ok .
  4. Read the license terms and click Ok .
  5. You can now close the package manager window.
  6. If prompted to reload project files, click Reload All .
  7. For testing purposes, we need to add the Lumia SensorCore SDK Testing Tools as well. Steps are the same as above, just the name differs.

Figure 1. About to install SensorCore SDK to out project

Installing the SDK adds Lumia.Sense, Lumia.Sense.Testing, and Microsoft Visual C++ 2013 Runtime Package for Windows Phone references to the project.

In order for an application to access the APIs it needs to declare SensorCore HID device and Location capability in its manifest. Installing SDK adds capabilities automatically to Package.appxmanifest :
<DeviceCapability Name="location" />
<m2:DeviceCapability Name="humaninterfacedevice">
  <m2:Device Id="vidpid:0421 0716">
    <m2:Function Type="usage:ffaa 0001" />
    <m2:Function Type="usage:ffee 0001" />
    <m2:Function Type="usage:ffee 0002" />
    <m2:Function Type="usage:ffee 0003" />
    <m2:Function Type="usage:ffee 0004" />
  </m2:Device>
</m2:DeviceCapability>

For the SDK to work correctly, you need to remove the Any CPU configuration from the Configuration Manager. Click the Solution Platforms combo box (with the text Any CPU ), and select the Configuration Manager from the list. Select from Active solution platform -combo box <Edit..> -option. From the following screen, make sure that the Any CPU is selected and press Remove .

Figure 2. Active solution platform -combo box

Using the SensorCore SDK in code

First we will define an UI element to receive all our information to the screen. Open MainPage.xaml and inside the <Grid> element add a ListBox:
<Grid>
    <ListBox x:Name="SensorcoreList"/>
</Grid>
Now we are ready for some coding! Add the following " using" statement to MainPage.xaml.cs to enable the SensorCore SDK:
using Windows.UI.Popups;
using System.Threading.Tasks;
using Lumia.Sense;
using Lumia.Sense.Testing;
If you have a Motion enabled phone, add the following member variables to the MainPage class, but if you don't have such device, add Simulator -text to end of each class (for example PlaceMonitorSimulator):
private PlaceMonitor _placeMonitor;
private TrackPointMonitor _pointTracker;
private ActivityMonitor _activityMonitor;
private StepCounter _stepCounter;
Next add the call to each SensorCore SDK part after the page has loaded to the constructor of MainPage and handle the activation/deactivation of SensorCore SDK according to the visibility of the app.
this.Loaded += async (oo, ee) =>
{
    await ShowStepCounter();
    await ShowActivityMonitor();
    await ShowTrackPointMonitor();
    await ShowPlacesMonitor();
};

Window.Current.VisibilityChanged += async (oo, ee) =>
{
    if (!ee.Visible) 
    {
        if (_placeMonitor != null) await CallSensorcoreApiAsync(async () => await _placeMonitor.DeactivateAsync());
        if (_pointTracker != null) await CallSensorcoreApiAsync(async () => await _pointTracker.DeactivateAsync()); 
        if (_activityMonitor != null) await CallSensorcoreApiAsync(async () => await _activityMonitor.DeactivateAsync());
        if (_stepCounter != null) await CallSensorcoreApiAsync(async () => await _stepCounter.DeactivateAsync());
    }
    else
    {
        if (_placeMonitor != null) await CallSensorcoreApiAsync(async () => await _placeMonitor.ActivateAsync());
        if (_pointTracker != null) await CallSensorcoreApiAsync(async () => await _pointTracker.ActivateAsync());
        if (_activityMonitor != null) await CallSensorcoreApiAsync(async () => await _activityMonitor.ActivateAsync());
        if (_stepCounter != null) await CallSensorcoreApiAsync(async () => await _stepCounter.ActivateAsync());
    }
};

Note: Many people will have either Location services or Motion data collection disabled by default, so we are providing an easy access for the users to enable them without having to find it from the settings.

Now we are ready to add the code for each SensorCore SDK part. First we define two helper methods which will allow us to call SensorCore SDK functionality safely, ensuring that the phone has SensorCore SDK support and necessary services enabled.

/// <summary> 
/// Performs asynchronous SensorCore SDK operation and handles any exceptions 
/// </summary> 
/// <param name="action"></param> 
/// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns> 
private async Task<bool> CallSensorcoreApiAsync(Func<Task> action) 
{
    Exception failure = null;
    try
    {
        await action();
    }
    catch (Exception e)
    {
        failure = e;
    }

    if (failure != null)
    {
        MessageDialog dialog;
        switch (SenseHelper.GetSenseError(failure.HResult))
        {
            case SenseError.LocationDisabled:
                dialog = new MessageDialog("Location has been disabled. Do you want to open Location settings now?", "Information");
                dialog.Commands.Add(new UICommand("Yes", async cmd => await SenseHelper.LaunchLocationSettingsAsync()));
                dialog.Commands.Add(new UICommand("No"));
                await dialog.ShowAsync();
                new System.Threading.ManualResetEvent(false).WaitOne(500);
                return false;

            case SenseError.SenseDisabled:
                dialog = new MessageDialog("Motion data has been disabled. Do you want to open Motion data settings now?", "Information");
                dialog.Commands.Add(new UICommand("Yes", async cmd => await SenseHelper.LaunchSenseSettingsAsync()));
                dialog.Commands.Add(new UICommand("No"));
                await dialog.ShowAsync();
                new System.Threading.ManualResetEvent(false).WaitOne(500);
                return false;

            default:
                dialog = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult), "");
                await dialog.ShowAsync();
                return false;
        }
    }

    return true;
}

private static async Task CreateMessageDialog(string message, string title, string label, UICommandInvokedHandler command, bool no)
{
    var dialog = new MessageDialog(message, title);
    dialog.Commands.Add(new UICommand(label,command));
    if (no) dialog.Commands.Add(new UICommand("No"));

    await dialog.ShowAsync();
}
After that we are ready to start with the StepCounter. The CallSensorcoreApiAsync wraps our call to the SensorCore SDK so that is safe. After we instantiate the Step Counter, we call to StepCounter.IsSupportedAsync() - this is needed to ensure that the Step Counter is supported by the phone. Note that if you are using the StepCounterSimulator -class, this property is not supported, and you must leave it out.  After that we get the current readings from SensorCore SDK, and show them in the ListBox defined in the first step.
private async Task ShowStepCounter()
{
    await CallSensorcoreApiAsync(async () =>
    {
        if (_stepCounter == null)
        {
            _stepCounter = await StepCounter.GetDefaultAsync();
        }

        if (await StepCounter.IsSupportedAsync())
        {
            var reading = await _stepCounter.GetCurrentReadingAsync();
            SensorcoreList.Items.Add("Current step counter reading");
            if (reading != null)
            {
                SensorcoreList.Items.Add(reading.Timestamp.ToString());
                SensorcoreList.Items.Add("Walk steps = " + reading.WalkingStepCount);
                SensorcoreList.Items.Add("Walk time = " + reading.WalkTime.ToString());
                SensorcoreList.Items.Add("Run steps = " + reading.RunningStepCount);
                SensorcoreList.Items.Add("Run time = " + reading.RunTime.ToString());
            }
            else
            {
                SensorcoreList.Items.Add("data not available");
            }
        }
    });
}
Next we add the ActivityMonitor and show the current activity. The main flow follows in all SensorCore SDK APIs the same structure, with initialising the  ActivityMonitor, checking if the hardware supports it, and then querying for the current readings. Please note that the IsSupportedAsync is not in ActivityMonitorSimulator class, and you have to leave it out.
private async Task ShowActivityMonitor()
{
    await CallSensorcoreApiAsync(async () =>
    {
        if (_activityMonitor == null)
        {
            _activityMonitor = await ActivityMonitor.GetDefaultAsync();
        }

        if (await ActivityMonitor.IsSupportedAsync())
        {
            var reading = await _activityMonitor.GetCurrentReadingAsync();
            SensorcoreList.Items.Add("Current activity");

            if (reading != null)
            {
                SensorcoreList.Items.Add(reading.Timestamp.ToString() +  
                                         ", activity = " + reading.Mode);
            }
            else
            {
                SensorcoreList.Items.Add("not available");
            }
        }
    });
}  
Our third part of the SensorCore SDK is TrackPointMonitor , which in this example shows route history from last two days. We'll follow the same routine to initialise and query the availability of the support from the hardware, and then add the route information to the ListBox . Please note that the IsSupportedAsync -property is not supported by the TrackPointMonitorSimulator class.
private async Task ShowTrackPointMonitor()
{
    await CallSensorcoreApiAsync(async () =>
    {
        if (_pointTracker == null)
        {
            _pointTracker = await TrackPointMonitor.GetDefaultAsync();
        }

        if (await TrackPointMonitor.IsSupportedAsync())
        {
            IList<TrackPoint> reading = await _pointTracker.GetTrackPointsAsync(
                DateTime.Now.AddDays(-2), TimeSpan.FromDays(2));
            SensorcoreList.Items.Add("Track Point history");

            if (reading != null)
            {
                SensorcoreList.Items.Add("count = " + reading.Count);

                foreach (TrackPoint tPoint in reading)
                {
                    SensorcoreList.Items.Add( 
                        tPoint.Position.Latitude + ", " +
                        tPoint.Position.Longitude);
                }
            }
            else
            {
                SensorcoreList.Items.Add("not available");
            }
        }
    });
}  
Now we can add the final part of the SensorCore SDK, the PlaceMonitor. In this sample we'll show the home place, if it has been recognised already. The model follows the same order as previous snippets with initialising, checking hardware support, and then querying for the current place. Please note that the IsSupportedAsync -property is not supported by the PlaceMonitorSimulator class.
private async Task ShowPlacesMonitor()
{
    await CallSensorcoreApiAsync(async () =>
    {
        if (_placeMonitor == null)
        {
            _placeMonitor = await PlaceMonitor.GetDefaultAsync();
        }

        if (await PlaceMonitor.IsSupportedAsync())
        {
            Place reading = await _placeMonitor.GetHomeAsync();
            SensorcoreList.Items.Add("Home place");

            if (reading != null)
            {
                SensorcoreList.Items.Add("Lat:" + reading.Position.Latitude.ToString());
                SensorcoreList.Items.Add("Lon:" + reading.Position.Longitude.ToString());
            }
            else
            {
                SensorcoreList.Items.Add("not available");
            }
        }
    });
}  

Deploy your app

Now make sure that your phone is connected to PC and Device build target and ARM architecture are selected from Visual Studio toolbar. Before launching the application, go to device settings and make sure that:
  1. Location services are enabled in Location settings page.
  2. Motion data collection is enabled in Motion data settings page.

Deploy your application by selecting Build > Deploy HelloSensorCore , HelloSensorCore being your application name.

Figure 3. Our application running on device

Congratulations! You have now successfully built your first SensorCore SDK application.

Downloads

Quickstart project v1.0 HelloSensorCore.zip

Last updated 7 July 2014

Back to top

×