Sensors! Sensors Everywhere!!!

Sensors play very important role in mobile and tablet devices. These days, we can’t imagine a device without some basic sensors. For example, ‘Proximity Sensor’ helps to disable touch-screen (and its input) when the device is in pocket. This avoids unnecessary touch inputs. ‘Light Sensor’ can automatically adjust the brightness of the screen.

Every Windows device, be it Phone or Tablet has these basic sensors and Windows Runtime makes it easy to access these sensors by providing consistent API model. You just need to master one sensor and you can easily program other ones. These APIs, being Windows Runtime APIs are available in Universal Applications i.e. in both Windows Phone or Windows applications and all these APIs are available under Windows.Devices.Sensors namespace. Lets see how we can program these sensors step-by-step

Step 1: Create Sensor Objects

You can create class level sensor objects in your page. For example, below code creates some of the basic sensors.

Step 2: Initialize Sensors

You don’t really create object of these sensors by newing up the instances. Rather you ask the system to give access to those sensors by calling ‘GetDefault()’ method. Usually this step is performed right after ‘InitializeComponent()’

Step 3: Attaching Callbacks aka Delegates

Now is the time to attach delegates to the ‘ReadingChanged’ event to get data from these sensors and process it in your application. This is also a good time to set sensor specific properties e.g. minimum reporting intervals, etc.. Having said that, It is always a good idea to perform ‘null checks’ before getting into these delegates.

Step 4: Reading Values

Now, whenever there will be any change in orientation, light, etc… These changes will be available as ‘changed event arguments’ in the event that we’ve assigned using delegate in above step.

Now, as a developer; you can decide about how to use this data returned from the ReadingChanged event. Someone may create a nice book reading application which can automatically rotate the screen or adjust the brightness of screen. Or someone can build another TempleRun which can allow to collect coins by tilting the screen. These sensors and their APIs can help you to achieve these tasks.

Hope you enjoyed this post.

Mayur Tendulkar |


1 Comment

Leave a Reply