Quick Start

Prerequisites

In order to develop applications that use the Lumia 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 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 Lumia SensorCore SDK to the project. The packages are in NuGet format.

  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 .

Figure 1. About to install SensorCore SDK to our project

Installing the SDK adds Lumia.Sense 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 the 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>

Using the Lumia 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 Lumia SensorCore SDK:
using Windows.UI.Popups;
using Lumia.Sense;
Add the following member variable to the MainPage class:
private StepCounter _stepCounter;

Note: Please note that 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 the correct settings. First we define a helper method that will allow us to call SensorCore SDK functionality safely, ensuring that the device supports the API and the necessary services are enabled.

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;

            case SenseError.SensorNotAvailable:
                dialog = new MessageDialog("The sensor is not supported on this device", "Information");
                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;
}

Next add code to MainPage constructor to initialize step counter and handle activation/deactivation of the sensor according to the visibility of the app.

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

After that we are ready to add the code to access step counter. We  get the current reading from the sensor and show it in the list box defined in the first step.

private async Task ShowCurrentReading()
{
    await CallSensorcoreApiAsync(async () =>
    {
        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");
        }
    });
}

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 2. Our application running on device

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

Downloads

Quickstart project v1.0 hello-sensorcore-1.0.zip

Last updated 15 October 2014

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×