Introduction
Again the great start of windowphone 8.1 is now "LightSensor" class available for developers.LightSensor returns the ambient-light reading as a LUX value.An ambient light sensor is one of several types of environmental sensors that allow apps to respond to changes in the user's environment.So You can use the ambient light sensor to detect changes in lighting with an app written in C#.
Why LightSensor?
Lets come with some real time examles
1)This sensor can be used for many things and it is common that the light sensor is used to illuminate the backlight on keyboards.
2)Controlling display backlight level for Windowsphone based on LUX values retun by LightSensor.
3)You know those fancy car GPS navigators that automatically switch color mode to dark when it gets dark to make it easy on your eyes? The display even turns dark when you drive into a tunnel and lights back up when you’re out
4)Another simple useful requirement is making device theme dark/white based on user environment status.
4)Another simple useful requirement is making device theme dark/white based on user environment status.
Source file at :LightSensorSample8.1
Building the Sample
- This sample is targeted on windowsphone 8.1(non-silverlight)
- Make sure you’ve downloaded and installed the Windows Phone SDK. For more information, see Get the SDK.
- I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
- This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows.
Description
In this post we are going to take a look at how to use the Light Sensor from within your C#/XAML Windowsphone 8.1 application.
Before we get started we should understand what the light sensor reading is providing you. This sensor will give you the ‘Lux Values’ (which is lumens per square meter) from the ambient light in your area. The Lux values can be described in the chart below and you can get more details by looking at the MSDN link here
Lighting condition | From (lux) | To (lux) | Mean value (lux) | Lighting step |
---|---|---|---|---|
Pitch Black | 0 | 10 | 5 | 1 |
Very Dark | 11 | 50 | 30 | 2 |
Dark Indoors | 51 | 200 | 125 | 3 |
Dim Indoors | 201 | 400 | 300 | 4 |
Normal Indoors | 401 | 1000 | 700 | 5 |
Bright Indoors | 1001 | 5000 | 3000 | 6 |
Dim Outdoors | 5001 | 10,000 | 7500 | 7 |
Cloudy Outdoors | 10,001 | 30,000 | 20,000 | 8 |
Direct Sunlight | 30,001 | 100,000 | 65,000 | 9 |
1)How to use LightSensor in windowsphone?
Unfortunately There is no API in Windows Phone 8 to access brightness sensor readings like LightSensor.However now luckely both windows phone 8.1 and windows phone silverlight 8.1 are support "LightSensor" class By the way when your app starts, you should first query the device to see if it has a light sensor. The sensor's GetDefault() method will return null if the sensor isn't present on the device or the system was unable to get a reference to it. This might happen if the device is in a connected standby mode.
C#
private Windows.Devices.Sensors.LightSensor _lightSensor;
private void SetupLightSensor()
{
_lightSensor = Sensor.LightSensor.GetDefault();
if (_lightSensor == null)
{
LuxLums = "Your current device does not support the light sensor.";
}
}
And there is two ways for reading ambient-light reading as a LUX value
- the app begins streaming light-sensor readings in real time and it is always get updated values with help ofReadingChanged event.
- the app will retrieve the sensor readings at a fixed interval with help of GetCurrentReading() Method.
1.1)Register to receive updates from the sensor as the values change:
Once you confirm access to the sensor, it's recommended that you set its reporting interval to the default value (0).
ReportInterval:
In cases where you need to set it explicitly, you can do this by changing the sensor's ReportInterval property. Make sure you don't go below the MinimumReportInterval property. Conversely, when an application is finished with the sensor, it should explicitly return the sensor to its default report interval by setting it to zero. This is important for power conservation, especially when using a language that may keep the sensor object active for an indefinite period prior to garbage collection.
C#
protected override void OnNavigatedTo(NavigationEventArgs e)
{
lightSensorobj = LightSensor.GetDefault();
uint minReportInterval = lightSensorobj.MinimumReportInterval;
lightSensorobj.ReportInterval = minReportInterval > 1000 ? minReportInterval : 1000;
lightSensorobj.ReadingChanged += LightSensorOnReadingChanged;
}
Note: Don't forgot to set it back to the default value (0) when you're done. Beacuse Conversely , when an application is finished with the sensor, it should explicitly return the sensor to its default report interval by setting it to zero. This is important for power conservation, especially when using a language that may keep the sensor object active for an indefinite period prior to garbage collection.
So lets start light-sensor readings with ReadingChanged event.
C#
void LightSensorOnReadingChanged(Windows.Devices.Sensors.LightSensor sender, Windows.Devices.Sensors.LightSensorReadingChangedEventArgs args)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
LuxTxt.Text = args.Reading.IlluminanceInLux.ToString();
EnvironmentStatusTxt.Text = args.Reading.IlluminanceInLux < 1000 ? "Dark" : "Light";
RequestedTheme = args.Reading.IlluminanceInLux < 1000 ? ElementTheme.Dark : ElementTheme.Light;//Changing the theme of app.
}
);
}
In above code light sensor allows the user to have a theme that responds to light, e.g. have a light theme during the day and dark theme during the night. This might be used for reading apps1.2)Getting the reading once or via Polling:
If you would like to simply get the Lux value one time you could just do the following
C#
var CurrentlightSensorReading = lightSensorobj.GetCurrentReading();
Absolutely.So saying you can build "a universal app" when actually building two separate apps, one for each platform just increases the confusion.mobile spy software
ReplyDelete