Click here to Skip to main content
15,888,600 members
Articles / Operating Systems / Windows 7

Using the GPS, Accelerometer & Vibration Controller

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Apr 2010CPOL3 min read 13.1K   5  
How to use the GPS, Accelerometer & Vibration Controller

Want to write an application for Windows Phone 7 Series devices? Need to know where you are? Are you moving? etc…

Well, I do!

Location, Location, Location

“The GeoCoordinateWatcher class supplies coordinate-based location data from the current location provider, which is the location provider that is currently prioritized the highest on the computer, based on a number of factors such as the age and accuracy of the data from all providers, the accuracy requested by location applications, and the power consumption and performance impact associated with the location provider. The current location provider may change over time, for instance, when a GPS device loses its fix indoors and a Wi-Fi triangulation provider becomes the most accurate provider on the computer.”

Here is a very simple example:

C#
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.MovementThreshold = 20;

watcher.StatusChanged += 
	new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += 
  new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

watcher.Start();

Finding the current location on a Windows Phone 7 Series device is very similar to how you would find it using the Windows 7 Sensor & Location Platform! The GeoCoordinateWatcher gives us an event based model to find the current most accurate location!

MovementThreshold specifies the distance that must be moved, in meters relative to the coordinate from the last PositionChanged event, before the location provider will raise another PositionChanged event.

PositionChanged event gets fired every time the current position changes.

Finally to start the watcher, just call Start().

If the position now changes, our watcher_PositionChanged will get fired with the new location.

[TIP] Remember to invoke back to the UI thread if you want to change something in the UI based on the position change!

Read more here (sample).

Am I Moving?

All Windows Phone 7 Series devices will have at least one accelerometer available.

What’s an Accelerometer?

An accelerometer is a device that measures proper acceleration, the acceleration experienced relative to freefall.

Single- and multi-axis models are available to detect magnitude and direction of the acceleration as a vector quantity, and can be used to sense position, vibration and shock. Micromachined accelerometers are increasingly present in portable electronic devices and video game controllers, to detect the position of the device or provide for game input.

Here is a simple example:

C#
AccelerometerSensor accelerometer = AccelerometerSensor.Default;

accelerometer.ReadingChanged += 
  new EventHandler<AccelerometerReadingAsyncEventArgs>(accelerometer_ReadingChanged);

accelerometer.Start();

The accelerometer is also event based (like the GeoCoordinateWatcher). Obtain a new instance of the AcelerometerSensor by using the Default property. Now just attach an event handler to the ReadingChanged and call Start().

Once a new reading is available, the accelerometer_ReadingChanged will be called.

Read more here (sample).

Good Vibrations…

All modern phones can vibrate… If you get a new message, on a phone call, etc…

Here is a simple example:

C#
VibrateController vibrate = VibrateController.Default;

vibrate.Start(TimeSpan.FromSeconds(1));

Read more here.

With the tighter control over the exact hardware requirements to run the OS, we are finally GUARANTEED that each phone will have a GPS, Accelerometer, 5MP+ Camera, etc.

I know these were very simple examples but just imagine the possibilities! Unfortunately, it’s still a catch-22! These are all real hardware sensors! Difficult to simulate… But if you do use them, they can make your phone applications better!!!

I CAN’T WAIT FOR WINDOWS PHONE 7 SERIES DEVICES!!!

Also read engadget - Windows Phone 7 Series: the complete guide.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --