GPS Programming Tips for Windows Mobile - Part 1

- Introducing GPS Intermediate Driver


Scenario: jogging in a park with a friend. You wouldn't like to carry on a Pocket PC with you, even if with integrated GPS Antenna (ok, you may prefer it...). So I chose a Smartphone in a pocket and the Bluetooth GPS Antenna in the other. Some runners prefer to have empty pockets or none at all, but I considered it a good deal.


Windows Mobile 5.0 introduced the GPS Intermediate Driver: "The GPS Intermediate Driver provides a very simple-to-use API for providing shared access to GPS data". By P/Invoking the stub gpsapi.dll, you can interact with the GPS Intermediate Driver below (gpsid.dll). Few APIs are needed:

 #region PInvokes to gpsapi.dll
static extern IntPtr GPSOpenDevice(IntPtr hNewLocationData, IntPtr hDeviceStateChange, string szDeviceName, int dwFlags);

static extern int  GPSCloseDevice(IntPtr hGPSDevice);

static extern int  GPSGetPosition(IntPtr hGPSDevice, IntPtr pGPSPosition, int dwMaximumAge, int dwFlags);

static extern int  GPSGetDeviceState(IntPtr pGPSDevice);

There's even 2 handy code samples available in the SDK (native and managed), that have everything you need to start with, together with relevant and helpful MSDN Documentation. An important detail to know about GPS Intermediate Driver is that on Smartphones you won't find the control applet, the one that would allow different applications to access the driver at the same time without interfering with each other, as reported in Setting up GPS on Windows Mobile 5 by Jason Fuller, thus having made the GPS Intermediate Driver unusable on Smartphones, in the sense that only one application can access the underlying GPS device at a time.

However, Windows Mobile 6 SDKs come with a pretty solution to this limitation: "GpsSettings.exe enables Smartphone users to set up the GPS intermediate driver. It works the same as the built-in GPS settings applet on Pocket PC. " (C:\Program Files\Windows Mobile 6 SDK\Tools\GPS\Settings.exe). Windows Mobile 6 SDKs come also with a Fake GPS, so that developers can emulate receiving "data using the GPS APIs even if there is no GPS receiver on the device. The GPS data is read from NMEA .txt files that get deployed to \Program Files\FakeGPS\GPSFiles when FakeGPS.CAB is installed on the device" (C:\Program Files\Windows Mobile 6 SDK\Tools\GPS\FakeGPS.CAB).

A developer can work with GPS APIs if the GPS Antenna is integrated on the device and also if it's connected by other means, such as Bluetooth for example, as long as it can be considered a virtual COM port, which is set through the GPS settings applet.

However, if you want to play with NMEA sentences on your own, you can retrieve the streaming data from the antenna and interpret it, and raise events so that upper layers (typically the UI, for example). Before leveraging on the GPSID, I exactly wanted to play with NMEA sentences (once in my life!), so that I could later better appreciate the GPSID... more on the next post.