Sensors In Windows 10 UWP App

Windows 10 SDK comes with lots of new sensors which helps the users to keep health fit to know what the user needs. The new list of sensors are the following,

  • Altimeter
  • Activity Sensor
  • Barometer
  • Pedometer
  • Proximity Sensor

Let’s see the sensor in detail.

To access the sensors API add the following namespace first,

  1. using Windows.Devices.Sensors;  
Altimeter

Altimeter helps to measure the relative altitude i.e. changes in elevation. The Altimeter has an event called ReadingChanged and triggered whenever a new value is sensed by the Altimeter sensor. ReportInterval property is used to set the time interval at which the sensor has to report.

Sample code 
  1. public void GetAltimeter()  
  2. {  
  3.     Altimeter getAltiude = Altimeter.GetDefault();  
  4.     AltimeterReading reading = getAltiude.GetCurrentReading();  
  5.     getAltiude.ReportInterval = 100;  
  6.     getAltiude.ReadingChanged += GetAltiude_ReadingChanged;  
  7. }  
  8. private void GetAltiude_ReadingChanged(Altimeter sender, AltimeterReadingChangedEventArgs args)  
  9. {  
  10.     AltimeterReading readingvalues = args.Reading;  
  11. }  
Activity Sensor

ActivitySensor will help to detect the user’s activity based on the user motion context. These sensor help to detect the motion of user either walking, running, idle, etc. This sensor set to idle state when the device is without any movement and also you can get the details up to 30 days.

Sample code to get the activity reading.
  1. public async void GetActivity()  
  2. {  
  3.     ActivitySensor activity = await ActivitySensor.GetDefaultAsync();  
  4.     var reading = await activity.GetCurrentReadingAsync();  
  5.     activity.ReadingChanged += new TypedEventHandler < ActivitySensor, ActivitySensorReadingChangedEventArgs > (ReadingChanged);  
  6. }  
  7. private void ReadingChanged(ActivitySensor sender, ActivitySensorReadingChangedEventArgs args)  
  8. {  
  9.     ActivitySensorReading readingActivity = args.Reading;  
  10. }  
To get the last 30 days details,
  1. DateTimeOffset last30days = DateTime.Today.AddDays(-30);  
  2. var details = await ActivitySensor.GetSystemHistoryAsync(last30days);  
  3. foreach(var values in details)  
  4. {  
  5.     string newvalue = values.Activity.ToString();  
  6. }  
  7. var trigger = new  
  8. Windows.ApplicationModel.Background.ActivitySensorTrigger(100);  
  9. trigger.SubscribedActivities.Add(ActivityType.InVehicle);  
Barometer

The Barometer help to measure the atmospheric pressure. The Barometer API has an event handler ReadingChanged which gets a new value is sensed by the sensor. The ReportInterval property is used to set the interval at which the sensor has to report. GetCurrentReading method is used to get the current reading for the barometer.

Sample Code
  1. public async void getBarometer()  
  2. {  
  3.     Barometer barometerValues = Barometer.GetDefault();  
  4.     BarometerReading reading = barometerValues.GetCurrentReading();  
  5.     var getPressure = reading.StationPressureInHectopascals;  
  6.     barometerValues.ReportInterval = 150;  
  7.     barometerValues.ReadingChanged += Barometer_ReadingChanged;  
  8. }  
  9. private void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args)  
  10. {  
  11.     string values = args.Reading.ToString();  
  12. }  
Pedometer

Pedometer help you to count the user’s steps in walking and running and you can access history details for up to 30 days. Pedometer sensor comes with Windows Phone 10 and also in Microsoft Band.

Sample Code
  1. public async void pedometer()  
  2. {  
  3.     Pedometer readings = await Pedometer.GetDefaultAsync();  
  4.     readings.ReportInterval = 100;  
  5.     readings.ReadingChanged += Readings_ReadingChanged;  
  6. }  
  7. private void Readings_ReadingChanged(Pedometer sender, PedometerReadingChangedEventArgs args)  
  8. {  
  9.     PedometerReading readvalues = args.Reading;  
  10.     if (readvalues.StepKind == PedometerStepKind.Walking)  
  11.     {  
  12.         var walkingsteps = readvalues.CumulativeSteps;  
  13.     }  
  14.     else if (readvalues.StepKind == PedometerStepKind.Running)  
  15.     {  
  16.         var runningSteps = readvalues.CumulativeSteps;  
  17.     }  
  18. }  
  19. public async void gettingHistory()  
  20. {  
  21.     var history = await Pedometer.GetSystemHistoryAsync(DateTime.Now.AddDays(-30));  
  22. }  
To get the last 30 days or any day you can get it from GetSystemHistoryAsync method by passing the date. This sensor mainly used to detect weather the user has intentionally touched the display or not.

Proximity Sensor

Proximity sensor helps you to detect the presence of the object it supports for short and long range. You can notify this to turn off the display during phone call.

Sample Code 
  1. ProximitySensor sensor;  
  2. public void GetProximityStatus()  
  3. {  
  4.     DeviceWatcher watch;  
  5.     watch = DeviceInformation.CreateWatcher(ProximitySensor.GetDeviceSelector());  
  6.     watch.Added += Watch_Added;  
  7.     watch.Start();  
  8.     ProximitySensorReading reading = sensor.GetCurrentReading();  
  9.     sensor.ReadingChanged += Sensor_ReadingChanged;  
  10. }  
  11. private void Sensor_ReadingChanged(ProximitySensor sender, ProximitySensorReadingChangedEventArgs args)  
  12. {  
  13.     ProximitySensorReading readStatus = args.Reading;  
  14. }  
  15. private void Watch_Added(DeviceWatcher sender, DeviceInformation args)  
  16. {  
  17.     ProximitySensor identified = ProximitySensor.FromId(args.Id);  
  18.     sensor = identified;  
  19. }