In my previous post Setting up the Windows 7 SDK Beta Virtual Light Sensor, I walked through the setup of the Windows 7 SDK Beta, the Virtual Light Sensor, Windows 7 Sensor and Location Interop Sample Library, and the Sample MSDN Reader application.
The Windows 7 SDK Beta provides APIs for interacting with sensors using native code. If you are a .NET developer the Windows 7 Sensor and Location Interop Sample Library demonstrates how you can access Sensors using managed code.
In this post, I will walk through the code necessary to retrieve a list of sensors and get data from the Virtual light sensor. If you want to follow along I would recommend performing the setup outlined in Setting up the Windows 7 SDK Beta Virtual Light Sensor. This example borrows from the MSDN Reader Demo, the main difference is this example is highly simplified. It primarily focuses on retrieving sensors and getting data from a sensor.
The code in this post requires the following prerequisites:
-
.NET 3.5 SP1
-
Visual Studio 2008
-
- Download and un-zip the Windows 7 Sensor and Location Interop Sample Library.
The first step you need to perform is building the Interop Sample library. Open the project file located at UnzipLocation\Windows7IntegrationLibrary_Sensor_and_Location\ Windows7.SensorAndLocation\Windows7.SensorAndLocation.csproj. Where UnzipLocation is the folder where you unzipped the library.
Once the project is open, build it and the Windows7.SensorAndLocation.dll should be output to UnzipLocation\Windows7IntegrationLibrary_Sensor_and_Location\Windows7.SensorAndLocation\bin\.
Now you are ready to create a new .NET project. Create a new C# Windows Form Application called CSharpLightSensorExample. The next step is to add a reference to the interop library (Windows7.SensorAndLocation.dll).
Now add two labels, a combo box, and a button to the form so that your form looks like this:

Name the combo box "comboBoxSensors", the button "btnGetDataReport", and the bottom label "labelDataReport".
Open Form1.cs and add the following using statements to make the Sensor objects available:

The next step is to initialize the combo box with a list of available sensors. To do this double click the form to add FormLoad event and add the following code so the event looks like this:

Note how SensorManager.GetAllSensors() retrieves the list of available sensors. If you build and run the project now, you should see a list of installed sensors loaded into the combo box.
The next step is to retrieve a data report from a selected light sensor. To do that add a button click event to btnGetDataReport and modify the code so that it looks like this:

Line 25 retrieves the selected sensor from the combo box. In lines 27-30 we check to make sure that the selected sensor is an Ambient Light Sensor. Without this check the rest of the code might throw an exception for unexpected types of sensors. Once we have the sensor and are sure it is an ambient light sensor we use the case statement to take action based on the current sensor state. Lines 35-37 retrieves a data report and sets the label text to the current Lux value.
Now if you start the virtual light sensor application, and run this form application you can select a sensor and click the button to get the current LUX value reported by the sensor.
