📜 ⬆️ ⬇️

Context Sensors in Windows 10

About a year ago, we wrote about how to work with the Lumia SensorCore SDK ( review , application creation ) - a special API for working with sensors on Lumia-devices. In Windows 10, on the basis of these and other developments in the universal Windows platform (UWP), we expanded the API for working with devices, adding new features for interacting with the user's context. Below we offer your attention to the report from the Build conference on this topic and the translation of the review article describing the new features.




In Windows 10, we are excited about the opportunity to present a number of new APIs for interacting with the context, which can help you create applications that improve the lives of users every day. This includes applications that can detect presence when a user approaches devices, applications that understand whether a user is walking or driving, applications that help users track their fitness performance, and many other scenarios. Using these APIs, you can anticipate user needs and proactively offer relevant personalized and relevant data or services to improve and facilitate their lives. This is quite a powerful thing for both consumer and corporate scenarios.



Determination of the type of activity


One of the new added APIs is the means for determining the type of activity (Activity Detection), which helps to clarify the context of the (re) movement of the user. These APIs are trying to understand what a person is doing on the basis of the current nature of the movement: walking, running, riding in a car or on a bicycle, in a stationary or “resting” state. The fixed state is returned when the device is with the user, and “rest” when the user has placed the device on a table or a fixed surface. You can also create background triggers and request a detailed history up to 30 days ago.
')
Several scenarios to use:


The scheme of work with API is given below.
//    var reading = await activitySensor.GetCurrentReadingAsync(); //      activitySensor.ReadingChanged += new TypedEventHandler<ActivitySensor, ActivitySensorReadingChangedEventArgs>(ReadingChanged); //   (  30 ) DateTimeOffset yesterday = ... var history = await ActivitySensor.GetSystemHistoryAsync(yesterday); foreach (var entry in history) { ... } //    var trigger = new Windows.ApplicationModel.Background.ActivitySensorTrigger(reportIntervalMs); trigger.SubscribedActivities.Add(ActivityType.InVehicle); // ..    .. 


More details on the API for determining the type of activity are described in MSDN , and sample code can be found in the UWP SDK collection of examples .

Counting steps


Another useful addition is a pedometer that counts the number of user steps while walking or running. As in the case of determining the type of activity, information in the history is stored up to 30 days.

A typical application in which this functionality can be used is to track health and fitness data (without the need for additional (wearable) devices). A pedometer can also combine wearable device data and data from your device’s sensors with Windows.

An example of working with the API is shown below:
 //   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; } //   var history = await Pedometer.GetSystemHistoryAsync(yesterday); 


More information about the API work with a pedometer is given in MSDN , and ready-made sample code is available in the collection of examples .

Barometer and altitude sensor


To work with information about pressure (data from the barometer) and relative height (for example, change during lifting), we added, respectively, the API of the barometer and height.

Typical scenarios:


An example of working with the API:
 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 += ... //    mySensor.ReportInterval = 500; 


Additional information:


Presence detection


We now also support an API for detecting presence with close and long range radii. Sensors of close action work at a distance of 2-3cm, in the far - they can detect presence at a distance of up to 12 meters.

Practical scenarios:


An example of working with the proximity API is shown below:
 using Windows.Devices.Sensors; //   ProximitySensorReading reading = sensor.GetCurrentReading(); bool isDetected = reading.IsDetected; //   sensor.ReadingChanged += ReadingChanged; void ReadingChanged(ProximitySensor s, ProximitySensorReadingChangedEventArgs e) { ProximitySensorReading reading = e.Reading; bool isDetected = reading.isDetected } //   mySensor.ReportInterval = 500; 


More information on the proximity API documentation can be found in MSDN , and do not forget about the finished example .




In addition to documentation, additional information (for example, on working with our own sensors ) can also be found in our report on creating context-aware UWP applications using sensors, which we told at the Build 2015 conference (also shown at the beginning of the translation).

Source: https://habr.com/ru/post/265963/


All Articles