Click here to Skip to main content
13,356,318 members (58,634 online)
Click here to Skip to main content
Add your own
alternative version


67 bookmarked
Posted 18 Jul 2008

A GPS Keep-alive Utility and Tester for Windows Mobile

, 18 Jul 2008
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!

You may also be interested in...


Comments and Discussions

Questionwhere do I find gpsapi.dll and coredl.dll? Pin
subrahmanya19-Feb-14 12:23
membersubrahmanya19-Feb-14 12:23 
QuestionGPS based system example Pin
kasun61810-Oct-13 23:26
memberkasun61810-Oct-13 23:26 
GeneralWM standard forces GPS off if keyboard locked Pin
Oxcarz30-Jan-10 3:00
memberOxcarz30-Jan-10 3:00 
QuestionHello Sam Pin
geramen20038-May-09 13:10
membergeramen20038-May-09 13:10 
QuestionVB? Pin
unusualfrank29-Dec-08 9:39
memberunusualfrank29-Dec-08 9:39 
QuestionGPS data logger Pin
JeffTz11-Dec-08 5:52
memberJeffTz11-Dec-08 5:52 
AnswerRe: GPS data logger Pin
Sam Rahimi12-Dec-08 8:06
memberSam Rahimi12-Dec-08 8:06 
GeneralRe: GPS data logger Pin
JeffTz15-Dec-08 22:54
memberJeffTz15-Dec-08 22:54 
GeneralNo GPS data, (Winmobile5 Treo 700) Pin
mhoemann4-Nov-08 16:06
membermhoemann4-Nov-08 16:06 
Generaltime to get cold boot Pin
Alon Ronen25-Sep-08 12:58
memberAlon Ronen25-Sep-08 12:58 
GeneralCan't open project Pin
Barney H19-Sep-08 16:25
memberBarney H19-Sep-08 16:25 
GeneralRe: Can't open project Pin
Sam Rahimi20-Sep-08 8:13
memberSam Rahimi20-Sep-08 8:13 
Hmm... Probably a versioning issue with the SDK. Plus I am still using the VS 2008 CTP - also may be an issue.

I would recommend creating a new project targeting your desired platform and then just import the files from my project using the existing folder structure. Since there are no external references it should work fine that way.

Send me an email if you have any problems.


GeneralDeadlock [modified] Pin
JeffGuroo13-Aug-08 12:05
memberJeffGuroo13-Aug-08 12:05 
GeneralRe: Deadlock Pin
CarlG16-Dec-08 0:23
memberCarlG16-Dec-08 0:23 
Questionhow to put on pda Pin
Member 400385321-Jul-08 13:37
memberMember 400385321-Jul-08 13:37 
AnswerRe: how to put on pda Pin
Sam Rahimi22-Jul-08 5:20
memberSam Rahimi22-Jul-08 5:20 
QuestionWhat about battery life Pin
Joel Ivory Johnson20-Jul-08 10:09
memberJoel Ivory Johnson20-Jul-08 10:09 
AnswerRe: What about battery life Pin
Sam Rahimi20-Jul-08 18:53
memberSam Rahimi20-Jul-08 18:53 
GeneralRe: What about battery life Pin
Joel Ivory Johnson21-Jul-08 3:29
memberJoel Ivory Johnson21-Jul-08 3:29 
GeneralRe: What about battery life Pin
Sam Rahimi21-Jul-08 5:03
memberSam Rahimi21-Jul-08 5:03 
GeneralRe: What about battery life Pin
Matware21-Jul-08 14:31
memberMatware21-Jul-08 14:31 
GeneralRe: What about battery life Pin
Sam Rahimi22-Jul-08 4:08
memberSam Rahimi22-Jul-08 4:08 
GeneralRe: What about battery life Pin
krokus66619-Nov-10 0:09
memberkrokus66619-Nov-10 0:09 
GeneralRe: What about battery life Pin
Brian Lowe23-Jul-08 9:31
memberBrian Lowe23-Jul-08 9:31 
GeneralRe: What about battery life Pin
Sam Rahimi23-Jul-08 9:34
memberSam Rahimi23-Jul-08 9:34 

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

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

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