In this two part article (see part II here), we will introduce mobile positioning based on CellID, and we will take a look at OpenCellID, the open source database of CellIDs. We will demonstrate how to use OpenCellID through some simple examples. The second part of the article will show how to use OpenCellID with OpenStreetMap and the 8Motion library to create a full interactive mobile mapping experience in JavaME, with each of these examples being less than 100 lines of code.
What is CellID?
We all know that GPS will be deployed in the vast majority of phones in the future. For now though, only a few high-end handsets are GPS enabled. An alternative, which has been available for some time, but which has gained momentum only recently is location based on CellID. So, what is CellID? A CellID is a number which is associated with a specific cell (the radio tower to which your handset is connected). In most cases, this is the closest tower to your location. So by knowing the location of this tower, then you can know approximately where the handset is. This concept is illustrated below. However, a tower can cover a huge area, from a few hundred meters, in high density areas, to several kilometers in lower density areas. This is why location CellID accuracy is lower than GPS accuracy. Nevertheless location via CellID still presents a very useful alternative.
CellID has recently become much more popular, thanks mainly to its seamless integration into GoogleMaps for Mobile.
OpenCellID
So this sounds great, let’s just get the device’s CellID, and then we will know its location. So what’s the catch? The issue is that the location of cells is not public information. Operators keep this private, for many reasons including:
- They don’t want to give tower location information to their competitors
- They use this as an extra revenue income, as they provide paying services to retrieve cell location
This is the reason we have set up a service called “OpenCellID”. OpenCellID.org is an open source database of CellIDs, serving two purposes:
- Anyone can create or use an application to gather information about cell locations, and send this information to the OpenCellID server, thereby improving the coverage
- The database can be used by any application to retrieve the location of a cell, according to the CellID
How to use OpenCellID
The OpenCellID API is a simple REST API. There are only two relevant calls: measure/add and get/cell.
Measure/add is used to add a measure of cell. This is used by any application that wants to add information to the database. Such applications produce content for the OpenCellID database. To make use of the API, a key is required for communication with OpenCellID.org. The key is used to track application providers, and to ensure that no fake data are inserted into the system.
Examples:
http://www.opencellid.org/measure/add?key=myapikey&mnc=1&mcc=2&lac=200&cellid=234&lat=3.42&lon=3.12
Parameters:
myapikey
: Substitute here the key that you received while registering at OpenCellID.mcc
: the mobile country codemnc
: the mobile network codelat
: lattitudelon
: longitudecellid
: CellIDlac
: location area code
The URL request above will tell the server that a client found a cell of id
234, for mcc=1
, mcc=2
and the lat,lon
position of 3.42,3.12. A word of caution: all data should be submitted in decimal, while some handsets (SonyEricsson for instance) provide lac
and cellid
in hexadecimal, so you will need to convert them before calling the API.
Cell/get is used to get the position associated with a cell. For example:
http://www.opencellid.org/cell/get?mcc=250&mnc=99&cellid=29513&lac=0
You need to provide at least mcc
, mnc
(mobile country code, and mobile network code of the operator), and cellid
. Lac
(location area code) is an optional parameter, and will help to determine an alternate position if cell is not found. The API will return a simple XML document, like the one below:
1 2 3 4 |
<rsp stat="ok"> <cell lat="57.8240013122559" lac="0" lon="28.00119972229" mcc="250" nbsamples="38" range="0" cellid="29513" mnc="99"> </cell> </rsp> |
This can be interpreted simply as: lat
and lon
are the coordinates of the cell, based on 38 samples. If needed, you can use the optional parameter fmt
to specify an output format e.g.:
http://www.opencellid.org/cell/get?mcc=250&mnc=99&cellid=29513&lac=0&fmt=txt
will return something like this:
57.8240013122559,28.00119972229,250,99,0,29513,100,38
This carries the same information as before, but in a much more compact form (see the API description for a complete description). Others APIs are under development, for instance to retrieve operators from mcc/mnc.
Sample program
So now let’s use OpenCellID to create a small program, written in JavaME, which will just get the CellID from the phone, and obtain the device’s location by making a request to the OpenCellID server and displaying the results.
First, let’s get the CellID:
We use SonyEricsson specific properties. There properties are non standard, and only available on some devices.
1 2 3 4 5 6 |
// These properties are implemented on latest SonyEricsson phones // This does not work on others (Nokia, etc...) String cellid=System.getProperty("com.sonyericsson.net.cellid"); String mcc = System.getProperty("com.sonyericsson.net.cmcc"); String mnc = System.getProperty("com.sonyericsson.net.cmnc"); String lac = System.getProperty("com.sonyericsson.net.lac"); |
Cellid is of course the CellID, mcc is the mobile country code, mnc is the mobile network code and identify the operator and the country (cmcc stands for current mcc). That’s all we need to find our location. Then, verify if these properties are supported:
1 2 3 4 5 6 7 8 9 10 11 |
String info="This phone does not support CellID"; if(cellid!=null) { info="Cell:"+cellid+" mcc:"+mcc+" mnc:"+mnc+" lac:"+lac; url="http://www.opencellid.org/cell/get?cellid="+Integer.parseInt(cellid,16)+"&mcc="+mcc+"&mnc="+mnc+"&lac="+Integer.parseInt(lac,16)+"&fmt=txt"; cellInfo.setString(info); Thread t=new Thread(this); t.start(); posInfo.setString("Requesting position..."); } |
In this part, we have created the URL to request information on the server, and start the fetch of the information in a separate thread:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public void run(){ try { HttpConnection cnx = (HttpConnection)Connector.open(url); InputStream is=cnx.openInputStream(); StringBuffer b=new StringBuffer(); int car; while( (car=is.read())!= -1){ b.append((char)car); } is.close(); cnx.close(); String res=b.toString(); if(res.startsWith("err")){ posInfo.setString("Cell not found!"); } else { int pos=res.indexOf(','); String lat=res.substring(0,pos); int pos2=res.indexOf(',',pos+1); String lon=res.substring(pos+1,pos2); posInfo.setString(lat+" "+lon); } } catch (IOException ex) { ex.printStackTrace(); posInfo.setString(ex.toString()); } } |
If the API call returns something starting with “err”, we are probably in error. Otherwise, we will have our current CellID positioning.
And that’s it done! The complete source code is available to download from opencellid.org.
So, what happens if the CellID is not in the database? Then the program will display an error. You can improve the accuracy of OpenCellID by discovering more cells.
The second part of this article, published here, will build on this first part, by creating an interactive program displaying the device location on a map, using the free J2MeMap component.