Skip to main content
August 11, 2015
Mobile

Contextual Sensing in Windows 10



This blog was written by Rinku Sreedhar, Senior Program Manager

In Windows 10, we are very excited to introduce several APIs for contextual sensing that can help you build apps to improve your customers’ daily lives. These include apps that detect presence when customers approach their devices, apps that can detect whether your customer is walking or driving, apps that can help customers track their fitness routines, and more. Using these APIs you can anticipate customers’ needs and proactively deliver targeted, personalized, and relevant content or services to enhance and simplify their lives. It is very powerful both in Consumer and Enterprise scenarios.

sensorCloud

Detecting activities

We are very happy to introduce the new Activity Detection APIs, which will help detect the user’s motion context. These APIs attempt to detect activity states based on current motion of your customer, such as Walking, Running, In Vehicle, Biking, Stationary, and Idle. The Stationary state is returned when a user has the device with them, while the Idle state is returned when the user has the device laying idle on a table or still surface. You also have the ability to create background triggers and get history details for up to 30 days.

A few usage scenarios are:

  • Surface information based on the user’s motion context (such as an activity-based playlist)
  • Change the app behavior based on the user’s motion context (such as auto-adjusting the camera focus when you detect that the user is capturing an image while walking or running)
  • Health and fitness tracking
  • Navigation and maps
  • Power saving (for example, avoiding constantly polling location or wifi when the device is idle or stationary)

The API pattern is shown below:

[code lang=”csharp”]
// Getting the current activity
var reading = await activitySensor.GetCurrentReadingAsync();

// Subscribing to activity changes
activitySensor.ReadingChanged += new TypedEventHandler<ActivitySensor,
ActivitySensorReadingChangedEventArgs>(ReadingChanged);

// Getting history of up to 30 days
DateTimeOffset yesterday = …
var history = await ActivitySensor.GetSystemHistoryAsync(yesterday);
foreach (var entry in history) { … }

// Using a background task
var trigger = new
Windows.ApplicationModel.Background.ActivitySensorTrigger(reportIntervalMs);
trigger.SubscribedActivities.Add(ActivityType.InVehicle);

// .. register the trigger, etc..[/code]

More detailed API reference for activity detection can be found here and UWP SDK Samples can be found here.

Counting steps

Another new addition is the pedometer which counts the user’s steps both when walking and running. As with activity detection, you can access history details for up to 30 days.

A typical application would be health and fitness tracking (without requiring an additional gadget/ wearable device). The pedometer can also combine with the wearable and Windows device sensor data.

The API pattern is shown below:

[code lang=”csharp”]//Getting the pedometer reading changes
pedometer.ReadingChanged += new TypedEventHandler<Pedometer,
PedometerReadingChangedEventArgs>(ReadingChanged);

void ReadingChanged(Pedometer sender, PedometerReadingChangedEventArgs args)
{
PedometerReading reading = args.Reading;
if (reading.StepKind == PedometerStepKind.Walking)
walkingSteps = reading.CumulativeSteps;
}

//Getting pedometer step count history
var history = await Pedometer.GetSystemHistoryAsync(yesterday);
[/code]

More detailed API reference for pedometer can be found here and UWP SDK Samples can be found here.

Barometer and altitude sensing

We are very happy to introduce the Barometer API that reports barometric station pressure, and the Altimeter API that reports relative altitude i.e. changes in elevation.

Typical applications include:

  • Health and fitness tracking: knowing the relative altitude, you can determine if the user is going up or down and adjust calorie expenditure calculations accordingly
  • Detect what floor the user is on for indoor navigation
  • Weather forecasting

The API pattern is shown below:

[code lang=”csharp”]
Barometer barometer = Barometer.GetDefault();
BarometerReading reading = barometer.GetCurrentReading();

double pressure = reading.StationPressureInHectopascals;
barometer.ReadingChanged += …

Altimeter altimeter = Altimeter.GetDefault();
AltimeterReading altimeterReading = altimeter.GetCurrentReading();

double altitudeChange = altimeterReading.AltitudeChangeInMeters;
altimeter.ReadingChanged += …

//Selecting a report interval
mySensor.ReportInterval = 500;
[/code]

Detailed API reference for Barometer can be found here and UWP SDK Samples can be found here. Detailed API reference for Altimeter can be found here and UWP SDK Samples can be found here.

Detecting presence

We now support APIs for both short range and long range presence detection. Short range sensors can detect for up to 2-3cm, while some of the longer presence sensors can detect presence up to a 12 meter range.

A few usage scenarios are:

  • Enabling a device to wake on user approach or log off when the user has departed. The Surface Hub uses these APIs to wake on approach when a user walks into a conference room and log off when they depart.
  • Automatic turn off of the display during a phone call
  • Understand and ignore accidental clicks when it is detected that the device is in the user’s pocket
  • Detection of gestures

The API pattern using Proximity is as shown below:

[code lang=”csharp”]using Windows.Devices.Sensors;

//Getting the current reading
ProximitySensorReading reading = sensor.GetCurrentReading();
bool isDetected = reading.IsDetected;

//Subscribing to reading changes
sensor.ReadingChanged += ReadingChanged;
void ReadingChanged(ProximitySensor s, ProximitySensorReadingChangedEventArgs e)
{
ProximitySensorReading reading = e.Reading;
bool isDetected = reading.isDetected
}

//Selecting a report interval
mySensor.ReportInterval = 500;
[/code]

More detailed API reference for Proximity can be found here and UWP SDK Samples can be found here.

Stay tuned; we will be sharing more details of new Windows 10 features upcoming blog posts. You can also find more information in our Build 2015 session on building contextually-aware UWP apps using sensors.