One of the greatest aspects of Lazarus, the free Delphi-like RAD environment of the FreePascal project, is its ability to cross-compile applications targetting Windows Mobile devices. I was taking a look at various CellID-based geolocation software lately and I thought I could probably write something of my own. However, it seems that most information pertaining to obtaining the CellID on WM devices is written for the .NET CF and there is - of course - nothing about Lazarus. So, I wrote my own simple unit to query the Cell ID, LAC and MNC codes of my Windows Mobile phone using nothing but Lazarus.
Windows Mobile has a rich set of Dynamic Link Libraries to interface all aspects of the device and the operating system itself. One of them is the Radio Interface Layer architecture, or RIL. This is an interface to the cellular radio module of your Windows Mobile device, if one is available. It can do all sort of things, from notifying you on incoming calls to getting useful information on the cellular network tower your device is connected to. We are going to use this last feature.
The information we need from the cell tower are:
- Cell ID. The unique identification of the cellular tower your device is connected to. "Unique" is a bit misleading, because this value is only unique to the specific cellular operator you are using.
- LAC. The Location Area Code specifies the area code of the current location.
- MNC. The Mobile Network Code is the identifier of the cellular network you are connected to.
The combination of these three values uniquely identifies the cell tower you are connected to. Once you have this information, you can do all sort of interesting stuff, like getting (approximate) geolocation information using, for example, OpenCellID or hacking your way through the Google Maps CellID geolocation API. One particularly interesting application is tracking your furniture during a move like the guy in the article did, without having to buy an iPhone and pay for the service.
That's all interesting theory, but how can we do this without having to buy Visual Studio just to develop a simple Windows Mobile application? Well, with Lazarus of course! The idea we'll pursue is load the RIL.DLL library, get a RIL handle, ask the device for cell tower info, encode useful information and return it as a string. This is easier said than done, because all you can get is a C header to the RIL.DLL library (aptly called ril.h ...). I don't especially digg C - I'm a mostly PHP and Pascal guy - so the biggest pain was understanding how to "translate" the header. After an hour or so, my ucellid.pas unit was ready:
In order to use it on your project, it's enough to include this unit in the implementation section of the form / unit you want to use the CellID in and call the published GetCellTowerInfo function. It returns a string in the format CellID-LAC-MNC, for example 1234-567-890. This is a ficticious example which reads as CellID = 1234, LAC = 567 and MNC = 890.
The code for ucellid.pas is licensed under the WTFPL Public License. It pretty much means that you can do whatever you want with it, without asking my permission. Enjoy!