Sunday, July 22, 2007

Configuring the GPS Intermediate Driver

I recently purchased the QSTARZ Bluetooth GPS Receiver Model BT-Q818 to play around with some location-aware research ideas I've had. First, the GPS receiver worked out-of-the-box after following the configuration instructions in the directions (or see this post on the Windows Mobile team blog). I tested it both on my Cingular 2125 SmartPhone and Cingular 8125 Pocket PC Phone. Second, it works amazingly well with the new Windows Live Search--I can get smooth map scrolling, hybrid maps, and real time traffic overlays on my phone automatically centered on my current location. To configure Windows Live on WM to use GPS, open Live Search->Menu->Settings and then set the GPS COM port (in my case, COM6). Then, open Map and make sure that Menu->Center on GPS is checked.

I was anxious to start my own hacking. I knew that Windows Mobile 5 shipped with the GPS Intermediate Driver framework, unfortunately I did not realize that this is an unmanaged API. However, both the Windows Mobile 5 SDK and the Windows MObile 6 SDK ship with sample code that provides a managed wrapper around that native GPS API. On my computer, these two samples can be found in:
  1. C:\Program Files\Windows Mobile 6 SDK\Samples\Smartphone\CS\GPS
  2. C:\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Samples\Cs\Gps
Unfortunately, neither of these samples worked for me immediately. I don't have a WM6 device, so I'll focus on the WM5 sample. The sample includes the Microsoft.WindowsMobile.Samples.Location GPS wrapper API and a small test app called GpsSample. To get the sample to work, you must configure the GPS Intermediate Driver, which requires either editing the registry or, alternatively, running the "GPS Intermediate Driver Control Panel." See Configuring the GPS Intermediate Driver on msdn. Well, where can I find this control panel? As the msdn link points out, many OEMs hide the application icon. On my Cingular 8125 Pocket PC Phone, this was certainly the case. Here's what I did to unhide it:
  1. On the desktop, open Start->All Programs->Microsoft Visual Studio 2005->Visual Studio Remote Tools->Remote Registry Editor
  2. Connect to your Windows Mobile device in the Remote Registry Editor
  3. Find the path: HKEY_LOCAL_MACHINE\ControlPanel\GPS Settings
  4. Delete any "Redirect" or "Hide" keys under HKEY_LOCAL_MACHINE\ControlPanel\GPS Settings.
Then, on my Pocket PC device, I opened Settings->System and there was a GPS icon. Click on that icon and choose an unused COM port. This is the COM port that your GPS applications are going to communicate through--the GPS intermediate driver talks to the hardware directly and serves as a virtual device sending data out this COM port. I selected COM0. Then select the Hardware tab and choose the GPS hardware port. On my device, I setup GPS to communicate via COM6 (at the highest baud rate possible). Then on the third and final tab ("Access"), make sure that Manage GPS Automatically is selected. After doing this, I reran the WM5 managed GPS sample and it worked great :)

Successfully configured GPS Intermediate Driver on Pocket PC Phone

Well, what if you have a SmartPhone? According to Jason Fuller on the Windows Mobile Team blog, the SmartPhone does not have the GPS Intermediate Driver Control Panel (see his post here). You can, however, manually configure the GPS Driver by modifying the registry. Again, using the Remote Registry Editor found in the Visual Studio Remote Tools directory:
  1. [HKEY_LOCAL_MACHINE\System\CurrentControlSet\GPS Intermediate Driver] "IsEnabled"=dword:00000001
  2. [HKEY_LOCAL_MACHINE\System\CurrentControlSet\GPS Intermediate Driver\Drivers]
    "CurrentDriver"="BT GPS"
  3. [HKEY_LOCAL_MACHINE\System\CurrentControlSet\GPS Intermediate Driver\Drivers\BT GPS]
    "CommPort"="COMX:" where X = the hardware COM port (for me COM6)
    "FriendlyName"="BT GPS"
    "InterfaceType"="COMM"
See this post on xda-developers for more information about modifying the registry to configure the GPS Intermediate Driver. Alternatively, someone has created a small tool to modify the registry settings--run this from your SmartPhone. Here's the GpsSample running on my Windows Mobile 5 SmartPhone (Cingular 2125).

Successfully configured GPS Intermediate Driver on SmartPhone

Update 07/24/2007 11:58PM: Some have asked me to post further details on the registry settings I am using on the Pocket PC and SmartPhone. Here's a PDF displaying screenshots of my key/value pairs related to the GPS Intermediate Driver.

Friday, July 20, 2007

Wallpaper, Screenshots, and Application Switching

To get around some of the Today Screen API limitations (see this post for a Today Screen rant), I've played around quite a bit with programatically switching the wallpaper on a WM device. However, there a number of problems with this approach, most significantly: changing the device wallpaper is slow and once the device has been signaled that the wallpaper has been updated, it does not actually redraw the wallpaper until the desktop is visible (e.g., the desktop is the top level window). For more about this issue, see my .NET CF newsgroup post. Nonetheless, changing the device wallpaper is one of the easiest ways to display ambient information.

A project at Intel Research required that a screenshot be taken of a specified application, cropped, and displayed as the wallpaper on the device. However, as mentioned above, setting the wallpaper on a user's device can be an intrusive operation--not only can it take between 10-30 seconds but the wallpaper update does not actually happen until all applications are minimized making the desktop visible.

I created a little test application which fulfills our requirements and attempts to avoid disrupting the user by waiting to execute until the device is idle. Here's how it works:
  1. Waits for device idle
  2. Finds handle to specified application (based on its window title)
  3. Switches from current app to specified application (saving current foreground window handle)
  4. Takes screenshot
  5. Crops screenshot (if necessary)
  6. Sets background wallpaper to screenshot
  7. Minimizes all open applications so phone background refreshes
  8. After background refresh, restores state of open applications
  9. Returns to saved foreground window handle
  10. Puts device back to sleep
Yes, it's hacky but I could think of no better way to do this. Note that in this case it was not possible to create a Today Screen plug-in.

Download the source code and the binary. You should be able to compile and deploy as long as you have the Smart Device Framework 2.0 from the OpenNETCF team installed. This sample will probably work with the SDF 2.1 as well but I haven't tested it. Alternatively, you can download the binary zip file and add those .dll references to the source code project. I've also added this sample code to the samples directory in Roam (see SourceForge). Roam is a Windows Mobile open source library which provides functionality like a SQL connection pool, power management functions, and a whole slew of UI code.

Note: this code has been tested on the Cingular 2125 and the T-Mobile SDA device. In addition, I also ran it on the Cingular 8125 Pocket PC Phone and everything worked except for the wallpaper changing code--this is because Roam's wallpaper changer methods only work on the SmartPhone.

Programming the Today Screen, A Rant

I find the Today Screen APIs (and lack thereof) to be one of the great failures in the design of the Windows Mobile framework. Many times in my mobile computing research, I have looked enviously at the Today Screen as prime real estate for ambient display of information and novel interactions. From an HCI perspective, it should be one of the most configurable components on the WM platform--it is, after all, a part of the display that the user glances at nearly every time they open/use their phone be it to check the time or simply to make a phone call. That is why, of course, Microsoft themselves include Today Screen plug-ins for calender information, text messages, emails. Microsoft has also recently recognized the value of ambient information on the desktop--Vista now ships with the Gadget Bar, which displays information about stocks, weather, email, traffic, etc. in an ambient manner.

This same functionality should be available on WM. Unfortunately, not only are these plug-ins difficult to develop for Windows Mobile (e.g., they hook into a rather archaic Windows messaging scheme, cannot be C#/VB, and cannot easily be animated) they also require that the user modify their Today Screen settings by selecting a new configuration XML file. This latter point sounds harmless; however, it requires some effort to allow the user to maintain their current Today Screen look and feel with the addition of the new plug-in (even if this plug-in is a one line text output). They must copy over the XML into each of their existing Today Screen profiles--otherwise the plug-in will not be visible. I've seen some developers simply ship their plug-in with a fresh Today Screen XML file that is meant to overwrite the user's existing profile; however, unless the XML file has the exact same plug-ins specified, the user may miss out on a plugin they've previously installed (or a layout they've established via Settings->Home Screen->Home screen layout).

Note: I speak only out of my own personal experience and watching others in my lab attempt to play around with Today Screen functionality. Though I haven't used WM6, it does not appear that this functionality has been improved with that release--perhaps WM7?