Click here to Skip to main content
Click here to Skip to main content

A GPS Keep-alive Utility and Tester for Windows Mobile

By , 18 Jul 2008
 
GPSController

Introduction

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.

Background

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:

    gps.Close(); 

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()
{
    InitializeComponent();
}

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
    {
        try
        {
            isTurnedOn = true;
            mnuTurnOn.Text = "Turn Off";
            gps.Open();
            timer1.Interval = pollInterval * 60 * 1000;
            UpdateStatus();
            timer1.Enabled = true;                    
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: could not find GPS device");
        }
    }
    else //Turn off GPS
    {
        isTurnedOn = false;
        gps.Close();
        UpdateStatus();
        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;
    }
    else
    {
        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().Latitude.ToString()+ 
                " - "+gps.GetPosition().Longitude.ToString();
        else
            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.

History

  • 18th July, 2008: Initial post

License

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 (www.wgt.com) 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 Supernova.com, 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!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWM standard forces GPS off if keyboard lockedmemberOxcarz30-Jan-10 2:00 
QuestionHello Sammembergeramen20038-May-09 12:10 
QuestionVB?memberunusualfrank29-Dec-08 8:39 
QuestionGPS data loggermemberJeffTz11-Dec-08 4:52 
AnswerRe: GPS data loggermemberSam Rahimi12-Dec-08 7:06 
GeneralRe: GPS data loggermemberJeffTz15-Dec-08 21:54 
GeneralNo GPS data, (Winmobile5 Treo 700)membermhoemann4-Nov-08 15:06 
I can NOT seem to be able to get any GPS data. The code works fine, the GPS is enabled, but the numbers, when I show the Lat & Long numbers, they're always Zero. My GPS IS enabled in my phone, and I'm performing these tests outside, there's really nothing around to interfere. I simply cannot seem to actually retrieve any data? If anybody has any suggestions, I would be ETERNALLY grateful!
Generaltime to get cold bootmemberAlon Ronen25-Sep-08 11:58 
GeneralCan't open projectmemberBarney H19-Sep-08 15:25 
GeneralRe: Can't open projectmemberSam Rahimi20-Sep-08 7:13 
GeneralDeadlock [modified]memberJeffGuroo13-Aug-08 11:05 
GeneralRe: DeadlockmemberCarlG15-Dec-08 23:23 
Questionhow to put on pdamemberMember 400385321-Jul-08 12:37 
AnswerRe: how to put on pdamemberSam Rahimi22-Jul-08 4:20 
QuestionWhat about battery lifememberJoel Ivory Johnson20-Jul-08 9:09 
AnswerRe: What about battery lifememberSam Rahimi20-Jul-08 17:53 
GeneralRe: What about battery lifememberJoel Ivory Johnson21-Jul-08 2:29 
GeneralRe: What about battery lifememberSam Rahimi21-Jul-08 4:03 
GeneralRe: What about battery lifememberMatware21-Jul-08 13:31 
GeneralRe: What about battery lifememberSam Rahimi22-Jul-08 3:08 
GeneralRe: What about battery lifememberkrokus66618-Nov-10 23:09 
GeneralRe: What about battery lifememberBrian Lowe23-Jul-08 8:31 
GeneralRe: What about battery lifememberSam Rahimi23-Jul-08 8:34 
GeneralRe: What about battery lifememberBrian Lowe23-Jul-08 9:45 
AnswerRe: What about battery lifememberJeffGuroo7-Aug-08 7:24 
GeneralRe: What about battery lifememberSam Rahimi7-Aug-08 8:11 
GeneralRe: What about battery life [modified]memberJeffGuroo7-Aug-08 10:50 
GeneralUpdate: real-world testing performed today at lunchmemberSam Rahimi18-Jul-08 8:10 
GeneralRe: Update: real-world testing performed today at lunchmemberSiliconValleyDreamer18-Jul-08 17:08 
GeneralRe: Update: real-world testing performed today at lunchmemberJeffGuroo7-Aug-08 7:34 

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

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