How to locate GPs on any mobile phone

Source: Internet
Author: User
Keywords We nbsp; Google http
Tags array broadcasting code data get get rid of google google +
"IT168 Technology" some time ago I mentioned in another article that I would like to take the position of broadcasting ideas and develop follow-up mobile solutions. The problem at the time was that I couldn't get rid of the phone to get location information, or a way to make location data useful. In this article, I'll show you how to get GPS coordinates for your phone. Even if your phone doesn't have a built-in global positioning system.

Why is it so difficult to locate?

The speed at which telephone technology matures is staggering. There are a large number of mobile phones built in the Global Positioning system, "smart" mobile phone has hundreds of people's average price tag, and most of us did not rushed to buy to replace our current mobile phone. In fact, the Global Positioning system does not include the ubiquitous location, which is the real difficulty. Our goal is to get the phone to any location, not just a GPS phone. This is the difficulty.

Google can do it, why can't we?

If you have not yet competed with Google's mobile map, I strongly recommend it. This is really a handy little application, especially when you're on the road and need your map. But I also have a mobile map on my phone, and I don't have a global positioning system. How is that possible? In the absence of a global positioning system, Google has found out how to determine your location by using the data on their mobile phones through their phone maps. In other words, it is possible to find locations without a global positioning system. In fact, let's first deconstruct the Google Mobile Map project and build it back up with C #.

Protocol analysis

I do not intend to dwell on the Protocol or Packet Analyzer (I use Microsoft Network Monitor). I just want to say that the first step to deconstruct Google's mobile phone map is to analyze HTTP requests, and mobile maps are run through ActiveSync.

A quick glance at the mobile map reveals several things. First, Google publishes a request for Http://www.google.com/glm/mmap location data. I've scoured their APIs. This means that we are looking for ourselves how to wrap and send data. Second, their data sends four key parts:

1, Cell Tower number

2, location area code (LAC)

3. Mobile network Code (MNC)

4. Mobile Country Code (MCC)

That's good news! Virtually all mobile phones have such readily available data, it turns out.

Cell Tower data-Wireless interface layer

We need to crack open Windows Mobile Wireless Interface layer cell tower to get data. In addition, you can retrieve the details of the entire Ril library from the MSDN Search Ril. We only care about the Ril cell Tower port section. The first thing we need to do is add the necessary PInvoke DLL import signature and call the wireless interface layer.

Code fragment:

1 [DllImport ("Ril.dll")]


2


3 private static extern IntPtr ril_initialize (uint dwindex, Rilresultcallback Pfnresult,


4


5 rilnotifycallback pfnnotify, uint dwnotificationclasses,


6


7 uint Dwparam, out IntPtr lphril);


8


9 [DllImport ("Ril.dll", EntryPoint = "Ril_getcelltowerinfo")]


10


private static extern IntPtr Ril_getcelltowerinfo (IntPtr hril);


12


[DllImport ("Ril.dll", EntryPoint = "Ril_hangup")]


14


private static extern IntPtr Ril_hangup (IntPtr hril);


16


[DllImport ("Ril.dll")]


18


private static extern IntPtr Ril_deinitialize (IntPtr hril);


20


21

These four methods are included in the Ril.dll, which is the access to the cell tower data. With these methods and their callback rilresultcallback and rilnotifycallback structures, we can easily invoke windows to get our cell Tower data.

Code fragment:

1 public static Celltower Getcelltowerinfo ()


2


3 {


4


5 IntPtr radiointerfacelayerhandle = IntPtr.Zero;


6


7 IntPtr radioresponsehandle = IntPtr.Zero;


8


9//Initialize the radio layer with a result callback parameter.


10


Radioresponsehandle = ril_initialize (1, New Rilresultcallback (Celldatacallback),


12


NULL, 0, 0, out Radiointerfacelayerhandle);


14


//The Initialize API call would synch return 0 if initialization is successful.


16


if (radioresponsehandle!= IntPtr.Zero)


18


19 {


20


return null;


22


23}


24


//Query for the current tower data.


26


Radioresponsehandle = Ril_getcelltowerinfo (Radiointerfacelayerhandle);


28


//wait for cell tower info to is returned since Ril_getcelltowerinfo invokes the


30


//callback method asynchronously.


32


WaitHandle. WaitOne ();


34


//Release the RIL handle


36


Notoginseng ril_deinitialize (radiointerfacelayerhandle);


38


//Convert The RAW tower data businessesflat-out data into a Celltower object


40


return new Celltower ()


42


43 {


44


Towerid = Convert.ToInt32 (_towerdetails.dwcellid),


46


Locationareacode = Convert.ToInt32 (_towerdetails.dwlocationareacode),


48


Mobilecountrycode = Convert.ToInt32 (_towerdetails.dwmobilecountrycode),


50


Wuyi Mobilenetworkcode = Convert.ToInt32 (_towerdetails.dwmobilenetworkcode),


52


53};


54


55}


56


57

Now we can call Getcelltowerinfo () and we'll get the strongly typed Celltower class and all the detail return values. I always prefer a strong type of PInvoke output, rather than a pointer to the grouped work.

Note: This code will not run in an emulator unless you configure the Honeycomb emulation and perform well. I suggest using a mobile phone instead of an emulator.

Cell Data Services

We have cell tower data, but what can we do now? We do not have GPS coordinates yet. We know that Google is making this conversion happen, cell tower data for latitude and long, we want to continue to dissect Google Mobile map. They are uploading binary data to the URL above. Similarly, the analysis protocol is beyond the scope of this article. You will find that we need to change a 55-byte array of data. Most of the bytes are empty (0), but we do need to fill several key items in the array. The following code creates and populates a byte array with the cell tower data:

Code fragment:

1 private static byte] getformpostdata (int celltowerid, int mobilecountrycode,


2


3 int mobilenetworkcode, int locationareacode)


4


5 {


6


7 byte] pd = new BYTE[55];


8


9 pd[1] = 14; 0x0e;


10


pd[16] = 27; 0x1b;


12


pd[47] = 255; 0xFF;


14


pd[48] = 255; 0xFF;


16


pd[49] = 255; 0xFF;


18


pd[50] = 255; 0xFF;


20


//GSM uses 4 digits while UTMS used 6 digits (hex)


22


pd[28] = ((Int64) Celltowerid > 65536)? (byte) 5: (byte) 3;


24


Shift (PD, Mobilenetworkcode);


26


Shift (PD, Mobilecountrycode);


28


Shift (PD, Celltowerid);


30


Shift (PD, Locationareacode);


32


Shift (PD, Mobilenetworkcode);


34


Shift (PD, Mobilecountrycode);


36


Notoginseng return PD;


38


39}


40


41///


42


///shifts specified data in the byte array starting at the specified array index.


44


45///


46


///the data.


48


///the start index.


50


Wuyi///the left operand.


52


private static void Shift (byte] data, int startIndex, int leftoperand)


54


55 {


56


int rightoperand = 24;


58


for (int i = 0; i < 4; i++, Rightoperand = 8)


60


61 {


62


data[startindex++] = (byte) (Leftoperand >> Rightoperand) & 255);


64


65}


66


67}


68


69

In short, we can pass in our 4 parameters: Cell Tower ID,LAV,MNC, and MCC, we get an array of bytes and can be uploaded via HTTP to google. The following code demonstrates the entire HTTP request, which requires our strongly typed cell tower data (from the above PInvoke) to return latitude and longitude from Google's database!

Code fragment:

1 Internal static geolocation GetLocation (Celltower Tower)


2


3 {


4


5 Try


6


7 {


8


9//Translate cell tower data into HTTP POST parameter data


10


One byte] FormData = Getformpostdata (tower. Towerid,


12


Tower. Mobilecountrycode,


14


Tower. Mobilenetworkcode,


16


Tower. Locationareacode);


18


HttpWebRequest request = (HttpWebRequest) webrequest.create (


20


new Uri (Google_mobile_service_uri));


22


request. method = "POST";


24


request. ContentLength = Formdata.length;


26


request. ContentType = "Application/binary";


28


Stream outputstream = Request. GetRequestStream ();


30


//Write the cell data to the HTTP stream


32


outputstream.write (formData, 0, formdata.length);


34

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.