RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Location-Based Services Using CellID in Windows Mobile : Page 2

Though GPS functionality has become hugely popular, it doesn't work indoors and not all devices have built-in GPS receivers. How do you get around this? Learn how to find and use CellID to locate your WinMo device and use Google maps to display it.

Obtaining the CellID
First, launch Visual Studio 2008 and create a new Smart Device project (Windows Mobile 6 Professional platform using the .NET CF 3.5). Name the project LBS.

Populate the default Form1 with the controls shown in Figure 1.

Figure 1. Form1: Populating the default Form1.

To obtain the CellID of a Windows Mobile device, use the RIL (Radio Interface Layer) API in the Windows CE operating system. The Radio Interface Layer (RIL) provides an interface that handles the communication between the CellCore system software and the radio hardware. Instead of writing your own P/Invoke code to use the RIL API, a fellow blogger, Dale Lane, has written a C# wrapper that invokes the RIL API. You'll be using a slightly modified version of his code in this project.

Add a new Class item to the project and name it RIL.cs. Code the RIL.cs file as shown in Listing 1.

Basically, the RIL class is a wrapper for the RIL API and it exposes a static method, GetCellTowerInfo(), that returns the CellID of a base station in the following format:

CellID – Location Area Code – Mobile Country Code

A sample string returned by the GetCellTowerInfo() method might look like this:

Click here to see the complete list of mobile country codes (MCC).

In the code-behind of Form1, import the following namespaces:

using System;
using System.Windows.Forms;
using System.IO;
Define the following member variable:
namespace LBS
    public partial class Form1 : Form
        Timer t;
        //---log file for CellID data---
        const string FILE_NAME = @"\CellIDs.txt";
        StreamWriter sw;
Because you're invoking the GetCellTowerInfo() method only periodically, you'll use a Timer object to do so. Also, all the CellIDs you obtain will be logged in a text file for processing later.

In the Form1 constructor, add the following line of code:

        public Form1()
            sw = new StreamWriter(FILE_NAME, true, 
In the Form1_Load event handler, initialize the Timer object, and wire up the event handler for the Tick event:

private void Form1_Load(object sender, EventArgs e)
            t = new Timer() { 
                Interval = 2000,   //---fired every 2 seconds
                Enabled = true 
            t.Tick += new EventHandler(t_Tick);
In the Tick event's handler, you will obtain the CellID of the device by calling the the RIL class's GetCellTowerInfo() static method. This displays the CellID and the current time in the Label control, and simultaneously logs the information into the text file:

        void t_Tick(object sender, EventArgs e)
            string cellid = RIL.GetCellTowerInfo();
            string txt = cellid + "-" + DateTime.Now.ToString();

            //---display in textbox control---
            lblCellID.Text = txt;

            //---write to file---           
The Start menu's item control has a Click event handler that enables the Timer object to trigger the Tick event every two seconds:

Figure 2. Located! Displaying the CellID information obtained by the device.

        private void mnuStart_Click(object sender, EventArgs e)
            t.Enabled = true;
            sw = new StreamWriter(FILE_NAME, true, 
Likewise, the Stop menu's item control also has a Click event handler that disables the Timer object: private void mnuStop_Click(object sender, EventArgs e) { t.Enabled = false; sw.Close(); } That's it! Press F5 to deploy the application onto a Windows Mobile Professional device (remember to ensure that you have a valid SIM card). Figure 2 shows the CellID information you've obtained. Notice that, as you move from one location to another, the CellID information may change).

A snapshot of the CellIDs.txt file (saved in the root directory of the device) looks like Listing 2.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date