Click here to Skip to main content
11,409,516 members (63,255 online)
Click here to Skip to main content

A GPS Keep-alive Utility and Tester for Windows Mobile

, 18 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Keeps the GPS active in Windows Mobile, allowing for instant, accurate location determination. Also a tutorial on how to access GPS data with almost no code.


Most of the new Windows Mobile devices include a GPS receiver as part of the standard configuration. However, one problem is that of the repeated "cold start." Presumably to save battery life, the GPS receiver is turned off when it is not being used. Unlike standard GPS devices, mobile GPS chipsets do not save data when they are powered off, requiring a "cold start" each time they are used. This means up to 10 minutes of keeping the phone motionless until it has locked on to the satellites. Windows Mobile 5 and 6 standard / smartphone editions do not provide user-accessible configuration options to change this.

However, if the GPS remains turned on, even after losing its fix (i.e. by going inside) it will be able to re-acquire its location within seconds of being placed in an area that has a signal. Also, once locked on to a signal, the receiver is able to hang onto it even when going into areas where it would not be able to lock on to the signal from a cold start.

I originally dealt with this annoyance by leaving Google Maps running all the time in the background. This solution was imperfect, since it used a lot of memory and CPU, as well as downloading data from the Internet to update the map, which is quite expensive on many mobile devices. I instead designed this utility to run in the background, keep the GPS open, and poll its status at a user-defined interval.

This program is also useful if you want to quickly test your GPS to make sure it is configured correctly and/or has a signal.


How to Use the Microsoft Intermediate GPS Driver

The library used in this app is an open source sample provided for free with the Windows Mobile 6 standard SDK. It encapsulates the API hooks, allowing for quick and easy access to the phone's GPS using C# managed code.

I have included the necessary source for the library; if you have the Windows Mobile 6 Standard SDK, those files can also be found at "\program files\Windows Mobile 6 SDK\Samples\Smartphone\CS\GPS".

Add the whole folder to your project (minus the demo app), and add...

using Microsoft.WindowsMobile.Samples.Location;

... (or the VB equivalent) to your classes that require GPS access.

IMPORTANT: Known Issues with the Microsoft .NET Compact GPS Library

One of the reasons that unnecessarily complex solutions have been posted here and elsewhere is, THE WINDOWS MOBILE 6 SDK LIBRARY DOES NOT WORK PROPERLY IN THE WINDOWS MOBILE 6 EMULATOR. HOWEVER, IT WORKS PERFECTLY ON AN ACTUAL PHONE. A bunch of NMEA files are included with the SDK to simulate navigation on the emulator... when used with the "FakeGPS" driver (for testing GPS apps in the emulator) the latitude and longitude are alternately invalid or ridiculous (near the South Pole.) There is an MSDN blog somewhere apologizing for this goof and giving a possible fix (an equation to convert decimal degrees to standard latitude-longitude, which does absolutely nothing to solve the problem.)

That said, use the library. It makes getting GPS data easier than opening a text file, as you will see in the code below. The problem is with the emulator. My advice would be, in the emulator, test with simulated latitude and longitude fed in however you see fit (i.e. an array of values, text file, etc.), then wire your position-getter methods up to the library and deploy to an actual GPS-enabled device. It will function as expected.

The project that accompanies this article, while small and simple, demonstrates the use of the core methods that you will actually use when building GPS-enabled applications:

  • Start the GPS:

    Gps g = new Gps(); Gps.Open(); 
  • Determine if the GPS is ready and knows its location:

    if (g.GetPosition().LatitudeValid)
    { //Has position: do something with the data } 
  • Get latitude / longitude:

    double latitude = g.GetPosition().Latitude;
    double longitude = g.GetPosition().Longitude; 
  • Stop the GPS:


Knowing all of this, one can easily build a decent application, ready for beta testing by normal users, in under 4 hours.

Using the Code

The code for this app is contained in "form1.cs" and its associated files. The other source files bundled with the project are the GPS objects provided by Microsoft.

It is so simple as to be trivial - the entire application runs in the code behind form1, the main window.

When the program starts, the user is given an option to turn on the GPS and start polling it at regular intervals. Here is how the device is stopped and started:

public bool isTurnedOn = false; //tracks state
public int pollInterval = 5; //keep-alive interval
public Gps gps; //the phone's internal GPS

public Form1()

private void Form1_Load(object sender, EventArgs e)
    gps = new Gps(); //Create the handle, but don't turn it on yet.

private void mnuTurnOn_Click(object sender, EventArgs e)
    if (!isTurnedOn) //Turn on GPS
            isTurnedOn = true;
            mnuTurnOn.Text = "Turn Off";
            timer1.Interval = pollInterval * 60 * 1000;
            timer1.Enabled = true;                    
        catch (Exception ex)
            MessageBox.Show("Error: could not find GPS device");
    else //Turn off GPS
        isTurnedOn = false;
        mnuTurnOn.Text = "Turn On";
        timer1.Enabled = false;                

Accessing the GPS location data is almost too easy. Here is the UpdateStatus() method that is the heart of the program that checks if the GPS is locked to a sufficient number of satellites, and then gets its latitude and longitude.

private void UpdateStatus()
    if (!isTurnedOn)
        lblState.Text = "GPS Turned Off";
        label1.Visible = label2.Visible = lblStatus.Visible = 
        lblLastFix.Visible = lblLastUpdate.Visible = false;
        lblState.Text = "GPS is turned on.";
        label1.Visible = label2.Visible = lblStatus.Visible = 
        lblLastFix.Visible = lblLastUpdate.Visible = true;
        lblLastUpdate.Text = DateTime.Now.ToString();
        if (gps.GetPosition().LatitudeValid)
            lblLastFix.Text = "Locked on to satellites: 
                " - "+gps.GetPosition().Longitude.ToString();
            lblLastFix.Text = "No signal";

How to Test in the Emulator

Seeing as the "FakeGPS" emulation does not work, on-device testing should be started early for any application built using these libraries. Building the application here that I have posted for download results in an EXE and a DLL. Place both in the same folder on your mobile device, and you should be good to go.

Unfortunately most devices are shipped without the newer versions of the .NET Compact Framework. If you have the Windows Mobile SDK, there will be a CAB for each different processor. Don't worry about breaking your phone by choosing the wrong one - it will simply refuse to install. You can also download the framework by itself from Microsoft.

Deploying to the Device

The project is currently set to use a Windows Mobile 6 standard build target (smartphones such as the Motorola Q9H or Samsung Jack II. It will also build for Windows Mobile 6 professional (touchscreen PDAs / PocketPCs.)

It has been tested in the real world on a Motorola Q9H with standard configuration but should work on any of the above mentioned device categories provided that a GPS chipset is present and configured properly.

Windows Mobile 5 devices should also accept this, and other applications using the intermediate GPS driver. Switch the build target in Visual Studio, and look for warnings on compile: if you are using components (mainly UI ones) not allowed on 5 (i.e. an embedded web browser control that can be manipulated by the host application) you will be notified at build time, and can make the appropriate (minor) changes.

Obviously, to make a production GPS application using .NET CF, you will want to create a Setup and Deployment project and explicitly specify that the .NET Compact Framework 2.0 or 3.5 be included with it, rather than making the user download it separately. The current build configuration is for 3.5, but it builds perfectly to a 2.0 target as well without modifications.


  • 18th July, 2008: Initial post


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


About the Author

Sam Rahimi
Team Leader World Golf Tour
Canada Canada
Sam Rahimi currently leads the web team at World Golf Tour ( and has an extensive background in ASP.NET, specializing in social networking and casual gaming.

Previously, he has worked for Roblox Corporation on their unique children's MMO as well as spending two years as team lead at, helping bring their social networking site into the modern era and doubling traffic along the way.

Sam started as a classic ASP engineer as a summer job 6 years ago to make some money to pay for tuition - to finish a degree in political science - and needless to say, never looked back. His experience has lead him to gain additional experience in the mobile space - J2ME, Android app developmentm, and SMS protocols.

And sure, the other engineers in SF may have an IPhone - Rahimi sticks with the EVO all the way!

Comments and Discussions

Questionwhere do I find gpsapi.dll and coredl.dll? Pinmembersubrahmanya19-Feb-14 12:23 
QuestionGPS based system example Pinmemberkasun61810-Oct-13 23:26 
GeneralWM standard forces GPS off if keyboard locked PinmemberOxcarz30-Jan-10 3:00 
QuestionHello Sam Pinmembergeramen20038-May-09 13:10 
QuestionVB? Pinmemberunusualfrank29-Dec-08 9:39 
QuestionGPS data logger PinmemberJeffTz11-Dec-08 5:52 
AnswerRe: GPS data logger PinmemberSam Rahimi12-Dec-08 8:06 
GeneralRe: GPS data logger PinmemberJeffTz15-Dec-08 22:54 
GeneralNo GPS data, (Winmobile5 Treo 700) Pinmembermhoemann4-Nov-08 16:06 
Generaltime to get cold boot PinmemberAlon Ronen25-Sep-08 12:58 
GeneralCan't open project PinmemberBarney H19-Sep-08 16:25 
GeneralRe: Can't open project PinmemberSam Rahimi20-Sep-08 8:13 
Questionhow to put on pda PinmemberMember 400385321-Jul-08 13:37 
AnswerRe: how to put on pda PinmemberSam Rahimi22-Jul-08 5:20 
QuestionWhat about battery life PinmemberJoel Ivory Johnson20-Jul-08 10:09 
AnswerRe: What about battery life PinmemberSam Rahimi20-Jul-08 18:53 
GeneralRe: What about battery life PinmemberJoel Ivory Johnson21-Jul-08 3:29 
GeneralRe: What about battery life PinmemberSam Rahimi21-Jul-08 5:03 
Hey Joel,

I know exactly what you mean - It reminds me of that Heisenberg Uncertainty Principle I learned way back in my first year of college - that in certain systems, the act of taking a measurement will invariably influence the value returned.

I agree - the battery life indicator is not linear... but it most likely is consistent. What I mean by that is, if it says "20% remaining" one time, it won't actually be 20%, but it will be an unknown constant value - so the next time it says 20%, it should be the same.

So I would propose the following test case:

- Add a HTTP web service call to each loop of the timer... have it send in a time stamp every minute (or whatever interval you choose)... when the requests stop coming, the battery has died (start this with only 10% battery remaining so it doesn't take days!)

- Perform the same procedure with the GPS closed and the line that attempts to poll it commented out.

Assuming you properly control for other variables (i.e. running processes, incoming calls, etc.) this should work pretty well.


GeneralRe: What about battery life PinmemberMatware21-Jul-08 14:31 
GeneralRe: What about battery life PinmemberSam Rahimi22-Jul-08 4:08 
GeneralRe: What about battery life Pinmemberkrokus66619-Nov-10 0:09 
GeneralRe: What about battery life PinmemberBrian Lowe23-Jul-08 9:31 
GeneralRe: What about battery life PinmemberSam Rahimi23-Jul-08 9:34 
GeneralRe: What about battery life PinmemberBrian Lowe23-Jul-08 10:45 
AnswerRe: What about battery life PinmemberJeffGuroo7-Aug-08 8:24 
GeneralRe: What about battery life PinmemberSam Rahimi7-Aug-08 9:11 
GeneralRe: What about battery life [modified] PinmemberJeffGuroo7-Aug-08 11:50 
GeneralUpdate: real-world testing performed today at lunch PinmemberSam Rahimi18-Jul-08 9:10 
GeneralRe: Update: real-world testing performed today at lunch PinmemberSiliconValleyDreamer18-Jul-08 18:08 
GeneralRe: Update: real-world testing performed today at lunch PinmemberJeffGuroo7-Aug-08 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 18 Jul 2008
Article Copyright 2008 by Sam Rahimi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid