Click here to Skip to main content
6,822,123 members and growing! (17,027 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Compact Framework     Beginner License: The Code Project Open License (CPOL)

A GPS Keep-alive Utility and Tester for Windows Mobile

By Sam Rahimi

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.
C#2.0, C#3.0, .NETCF, Mobile, WinMobile5, WinMobile6VS2005, VS2008, Architect, Dev
Revision:3 (See All)
Posted:18 Jul 2008
Views:37,900
Bookmarked:53 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 5.35 Rating: 4.80 out of 5

1

2

3
2 votes, 15.4%
4
11 votes, 84.6%
5
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


Member
Sam Rahimi is the lead developer for the interactive division of Supernova Entertainment group, and directs development for their social networking site, Supernova.com

He has years of experience with Microsoft development technologies, starting with classic ASP, and now develops in ASP.NET 3.5. A longtime VB programmer, Rahimi now swears by C# . In his code, he focuses on clarity, simplicity, and reusability, and loves to teach these techniques to other developers.

He also has extensive experience in the mobile industry; specifically, with the development of interactive SMS applications, optimization of websites for mobile use, and the development of J2ME applications for the phone.
Occupation: Team Leader
Company: Supernova Interactive
Location: Canada Canada

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Windows Mobile, iPhone, Android - Marketplace Comparison
    Detailed comparison between Windows Mobile Marketplace, Apple's iPhone AppStore and Android Market from developer point of view.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
GeneralWM standard forces GPS off if keyboard locked PinmemberOxcarz3:00 30 Jan '10  
QuestionHello Sam Pinmembergeramen200313:10 8 May '09  
GeneralVB? Pinmemberunusualfrank9:39 29 Dec '08  
QuestionGPS data logger PinmemberJeffTz5:52 11 Dec '08  
AnswerRe: GPS data logger PinmemberSam Rahimi8:06 12 Dec '08  
GeneralRe: GPS data logger PinmemberJeffTz22:54 15 Dec '08  
GeneralNo GPS data, (Winmobile5 Treo 700) Pinmembermhoemann16:06 4 Nov '08  
Generaltime to get cold boot PinmemberAlon Ronen12:58 25 Sep '08  
GeneralCan't open project PinmemberBarney H16:25 19 Sep '08  
GeneralRe: Can't open project PinmemberSam Rahimi8:13 20 Sep '08  
GeneralDeadlock [modified] PinmemberJeffGuroo12:05 13 Aug '08  
GeneralRe: Deadlock PinmemberCarlG0:23 16 Dec '08  
Generalhow to put on pda PinmemberMember 400385313:37 21 Jul '08  
GeneralRe: how to put on pda PinmemberSam Rahimi5:20 22 Jul '08  
GeneralWhat about battery life PinmemberJoel Ivory Johnson10:09 20 Jul '08  
GeneralRe: What about battery life PinmemberSam Rahimi18:53 20 Jul '08  
GeneralRe: What about battery life PinmemberJoel Ivory Johnson3:29 21 Jul '08  
GeneralRe: What about battery life PinmemberSam Rahimi5:03 21 Jul '08  
GeneralRe: What about battery life PinmemberMatware14:31 21 Jul '08  
GeneralRe: What about battery life PinmemberSam Rahimi4:08 22 Jul '08  
GeneralRe: What about battery life PinmemberBrian Lowe9:31 23 Jul '08  
GeneralRe: What about battery life PinmemberSam Rahimi9:34 23 Jul '08  
GeneralRe: What about battery life PinmemberBrian Lowe10:45 23 Jul '08  
GeneralRe: What about battery life PinmemberJeffGuroo8:24 7 Aug '08  
GeneralRe: What about battery life PinmemberSam Rahimi9:11 7 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jul 2008
Editor: Deeksha Shenoy
Copyright 2008 by Sam Rahimi
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project