Sensors in Windows phone mainly include accelerometer sensors, compass sensors, gyroscope sensors, etc.
Accelerometer Sensors
The accelerometer class is an interface to the acceleration sensor, and the accelerometer class is located under the Windows.Devices.Sensors namespace. To use the function of the system accelerometer, you need to create an object of the accelerometer class, and then use this object to capture the current acceleration state of the phone. The accelerometer class provides the Readingchanged event for detecting the state of the accelerometer and returns the X, Y, and Z axis information.
Refer to the Microsoft.Devices.Sensors namespace before using the accelerometer class.
The main code is as follows:
1 usingMicrosoft.Devices.Sensors;2 3 namespaceAccelerometer4 {5 Public Partial classMainpage:phoneapplicationpage6 {7Accelerometer acc =NewAccelerometer ();8 9 PublicMainPage ()Ten { One InitializeComponent (); AAcc. Readingchanged + =acc_readingchanged; - acc. Start (); - } the - voidAcc_readingchanged (Objectsender, Accelerometerreadingeventargs e) - { -Deployment.Current.Dispatcher.BeginInvoke (() =threadstaticaccelerometerchanged (e)); + } - voidthreadstaticaccelerometerchanged (Accelerometerreadingeventargs e) + { AXtext.text =e.x.tostring (); atYtext.text =e.y.tostring (); -Ztext.text =e.z.tostring (); - } - } -}
View Code
Compass Sensor
Use the compass sensor to determine the angle of the cell phone's rotation relative to the Earth's Arctic magnetic field. The Compass class provides access to the device Compass sensor for the Windows Phone application. First Call Compass class Compass's getdefault () to get to the Compass object, and then set the current reporting compass reading interval through the property reportinterval. Gets the current property value through the Getcurrentreading method and the change in direction obtained through the Readingchanged event.
Refer to the Microsoft.Devices.Sensors namespace before using the Compass class.
The main code is as follows:
1 PublicMycompass ()2 {3 InitializeComponent ();4 if(compass.issupported)5 {6Compass =NewCompass ();7Compass. Currentvaluechanged + =NewEventhandler<sensorreadingeventargs<compassreading>>(compass_currentvaluechanged);8Compass. Timebetweenupdates = Timespan.frommilliseconds ( -);9 Compass. Start ();Ten } One Else AMessageBox.Show ("device does not support electronic compass"); - } - the voidCompass_currentvaluechanged (Objectsender, Sensorreadingeventargs<compassreading>e) - { - if(Compass. Isdatavalid) - { + -Deployment.Current.Dispatcher.BeginInvoke (() = + { A atHeadingaccuracy =e.sensorreading.headingaccuracy; - - if(!calibrating) - { -Trueheading =e.sensorreading.trueheading; - if(( the<= trueheading) && (trueheading <= the)) inreciprocalheading = trueheading- the; - Else toreciprocalheading = trueheading + the; +Compassface1.rendertransformorigin =NewPoint (0.5,0.5); -Transform. Angle = the-trueheading; theCompassface1.rendertransform =transform; * $ }Panax Notoginseng - }); the } + } A } the}
View Code
Gyro sensor
The gyroscope sensor measures the rotation speed of the phone along its three spindles the Gyrometer class represents a gyroscope sensor that provides access to the device gyroscope sensor for the Windows Phone application and returns angular velocity values for the X, Y, and Z axes.
Windows Phone Sensor