| 一、通過(guò)手機(jī)信號(hào)獲取基站信息 通過(guò)TelephonyManager 獲取lac:mcc:mnc:cell-id(基站信息)的解釋: MCC,Mobile Country Code,移動(dòng)國(guó)家代碼(中國(guó)的為460); MNC,Mobile Network Code,移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為0,中國(guó)聯(lián)通為1,中國(guó)電信為2); LAC,Location Area Code,位置區(qū)域碼; CID,Cell Identity,基站編號(hào); BSSS,Base station signal strength,基站信號(hào)強(qiáng)度。 具體實(shí)現(xiàn)代碼如下: | package com.easipass.test;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
/*** 功能描述:通過(guò)手機(jī)信號(hào)獲取基站信息* # 通過(guò)TelephonyManager 獲取lac:mcc:mnc:cell-id* # MCC,Mobile Country Code,移動(dòng)國(guó)家代碼(中國(guó)的為460);* # MNC,Mobile Network Code,移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為0,中國(guó)聯(lián)通為1,中國(guó)電信為2); * # LAC,Location Area Code,位置區(qū)域碼;* # CID,Cell Identity,基站編號(hào);* # BSSS,Base station signal strength,基站信號(hào)強(qiáng)度。* @author android_ls*/
public class GSMCellLocationActivity extends Activity {private static final String TAG = "GSMCellLocationActivity";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 獲取基站信息findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {TelephonyManager mTelephonyManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);// 返回值MCC + MNCString operator = mTelephonyManager.getNetworkOperator();int mcc = Integer.parseInt(operator.substring(0, 3));int mnc = Integer.parseInt(operator.substring(3));// 中國(guó)移動(dòng)和中國(guó)聯(lián)通獲取LAC、CID的方式GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();int lac = location.getLac();int cellId = location.getCid();Log.i(TAG, " MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cellId);// 中國(guó)電信獲取LAC、CID的方式/*CdmaCellLocation location1 = (CdmaCellLocation) mTelephonyManager.getCellLocation();lac = location1.getNetworkId();cellId = location1.getBaseStationId();cellId /= 16;*/// 獲取鄰區(qū)基站信息List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();StringBuffer sb = new StringBuffer("總數(shù) : " + infos.size() + "\n");for (NeighboringCellInfo info1 : infos) { // 根據(jù)鄰區(qū)總數(shù)進(jìn)行循環(huán)sb.append(" LAC : " + info1.getLac()); // 取出當(dāng)前鄰區(qū)的LACsb.append(" CID : " + info1.getCid()); // 取出當(dāng)前鄰區(qū)的CIDsb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 獲取鄰區(qū)基站信號(hào)強(qiáng)度}Log.i(TAG, " 獲取鄰區(qū)基站信息:" + sb.toString());}});}
}
| 在AndroidManifest.xml添加獲取位置信息的權(quán)限: | <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
| 點(diǎn)擊“獲取基站信息”的按鈕后,Logcat的日志輸出如下: 1、中國(guó)聯(lián)通: 2、中國(guó)移動(dòng): 一、通過(guò)手機(jī)信號(hào)獲取基站信息-單基站定位 | TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// 返回值MCC + MNCString operator = mTelephonyManager.getNetworkOperator();mcc = Integer.parseInt(operator.substring(0, 3));mnc = Integer.parseInt(operator.substring(3));// 中國(guó)移動(dòng)和中國(guó)聯(lián)通獲取LAC、CID的方式GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();lac = location.getLac();cid = location.getCid();Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);
| 二、調(diào)用第三方公開(kāi)的API(根據(jù)基站信息查找基站的經(jīng)緯度值及地址信息) 1、組拼JSON形式的請(qǐng)求參數(shù) | /*** 獲取JSON形式的基站信息* @param mcc 移動(dòng)國(guó)家代碼(中國(guó)的為460)* @param mnc 移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為0,中國(guó)聯(lián)通為1,中國(guó)電信為2); * @param lac 位置區(qū)域碼* @param cid 基站編號(hào)* @return json* @throws JSONException*/private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {JSONObject jsonCellPos = new JSONObject();jsonCellPos.put("version", "1.1.0");jsonCellPos.put("host", "maps.google.com");JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("location_area_code", "" + lac + "");json1.put("mobile_country_code", "" + mcc + "");json1.put("mobile_network_code", "" + mnc + "");json1.put("age", 0);json1.put("cell_id", "" + cid + "");array.put(json1);jsonCellPos.put("cell_towers", array);return jsonCellPos.toString();}
| 2、通過(guò)HTTP協(xié)議網(wǎng)絡(luò)請(qǐng)求源碼: | request URL:http://www.minigps.net/minigps/map/google/location
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:191
Content-Type:application/json; charset=UTF-8
Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372
Host:www.minigps.net
Origin:http://www.minigps.net
Referer:http://www.minigps.net/map.html
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko)
Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Request Payload
{"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,
"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
Response Headersview source
Content-Type:application/json
Date:Sat, 03 Jan 2013 14:03:15 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
| 3、用JAVA代碼具體實(shí)現(xiàn) | /*** 調(diào)用第三方公開(kāi)的API根據(jù)基站信息查找基站的經(jīng)緯度值及地址信息*/public String httpPost(String url, String jsonCellPos) throws IOException{byte[] data = jsonCellPos.toString().getBytes();URL realUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();httpURLConnection.setConnectTimeout(6 * 1000);httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setRequestMethod("POST");httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");httpURLConnection.setRequestProperty("Connection", "Keep-Alive");httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");httpURLConnection.setRequestProperty("Host", "www.minigps.net");httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko)
Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");httpURLConnection.setRequestProperty("Host", "www.minigps.net");DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());outStream.write(data);outStream.flush();outStream.close();if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = httpURLConnection.getInputStream();return new String(read(inputStream));}return null;} | 4、讀取返回的JSON數(shù)據(jù)流代碼: | /*** 讀取IO流并以byte[]形式存儲(chǔ)* @param inputSream InputStream* @return byte[]* @throws IOException*/public byte[] read(InputStream inputSream) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream();int len = -1;byte[] buffer = new byte[1024];while ((len = inputSream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();inputSream.close();return outStream.toByteArray();}
| 三、請(qǐng)求參數(shù)及返回結(jié)果的JSON形式: 1、請(qǐng)求的JSON參數(shù)值: | {"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
| 2、返回的JSON結(jié)果值: | {"location":{"latitude":"31.211389541625977","longitude":"121.60332489013672","address":{"city":"上海市浦東新區(qū)居里路432號(hào);浦東新區(qū)光啟安老院、第一三共制藥上海公司、SUNPLUS[附近]","country":"","country_code":"","county":"","postal_code":"","region":"","street":"","street_number":""}},"access_token":"dummytoken"}
| 四、完整代碼及所需權(quán)限: Java代碼: | {"location":{"latitude":"31.211389541625977","longitude":"121.60332489013672","address":{"city":"上海市浦東新區(qū)居里路432號(hào);浦東新區(qū)光啟安老院
、第一三共制藥上海公司、SUNPLUS[附近]","country":"","country_code":"","county":"","postal_code":"","region":"","street":"","street_number":""}},"access_token":"dummytoken"}
| 在AndroidManifest.xml添加獲取位置信息的權(quán)限: | package com.easipass.test;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
/*** 功能描述:單基站定位* @author android_ls*/
public class GSMCellLocationActivity extends Activity {private static final String TAG = "GSMCellLocationActivity";private int mcc;private int mnc;private int lac;private int cid;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 獲取基站信息findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// 返回值MCC + MNCString operator = mTelephonyManager.getNetworkOperator();mcc = Integer.parseInt(operator.substring(0, 3));mnc = Integer.parseInt(operator.substring(3));// 中國(guó)移動(dòng)和中國(guó)聯(lián)通獲取LAC、CID的方式GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();lac = location.getLac();cid = location.getCid();Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);new Thread() {@Overridepublic void run() {try {String json = getJsonCellPos(mcc, mnc, lac, cid);Log.i(TAG, "request = " + json);String url = "http://www.minigps.net/minigps/map/google/location";String result = httpPost(url, json);Log.i(TAG, "result = " + result);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}});}/*** 調(diào)用第三方公開(kāi)的API根據(jù)基站信息查找基站的經(jīng)緯度值及地址信息*/public String httpPost(String url, String jsonCellPos) throws IOException{byte[] data = jsonCellPos.toString().getBytes();URL realUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();httpURLConnection.setConnectTimeout(6 * 1000);httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setRequestMethod("POST");httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");httpURLConnection.setRequestProperty("Connection", "Keep-Alive");httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");httpURLConnection.setRequestProperty("Host", "www.minigps.net");httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome
/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");httpURLConnection.setRequestProperty("Host", "www.minigps.net");DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());outStream.write(data);outStream.flush();outStream.close();if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = httpURLConnection.getInputStream();return new String(read(inputStream));}return null;}/*** 獲取JSON形式的基站信息* @param mcc 移動(dòng)國(guó)家代碼(中國(guó)的為460)* @param mnc 移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為0,中國(guó)聯(lián)通為1,中國(guó)電信為2); * @param lac 位置區(qū)域碼* @param cid 基站編號(hào)* @return json* @throws JSONException*/private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {JSONObject jsonCellPos = new JSONObject();jsonCellPos.put("version", "1.1.0");jsonCellPos.put("host", "maps.google.com");JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("location_area_code", "" + lac + "");json1.put("mobile_country_code", "" + mcc + "");json1.put("mobile_network_code", "" + mnc + "");json1.put("age", 0);json1.put("cell_id", "" + cid + "");array.put(json1);jsonCellPos.put("cell_towers", array);return jsonCellPos.toString();}/*** 讀取IO流并以byte[]形式存儲(chǔ)* @param inputSream InputStream* @return byte[]* @throws IOException*/public byte[] read(InputStream inputSream) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream();int len = -1;byte[] buffer = new byte[1024];while ((len = inputSream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();inputSream.close();return outStream.toByteArray();}
}
| 基站:類(lèi)似于WIFI熱點(diǎn),官方解釋,移動(dòng)通信系統(tǒng)中,連接固定部分與無(wú)線部分,并通過(guò)空中的無(wú)線傳輸與移動(dòng)臺(tái)相連的設(shè)備。基站即公用移動(dòng)通信基站是無(wú)線電臺(tái)站的一種形式,是指在一定的無(wú)線電覆蓋區(qū)中,通過(guò)移動(dòng)通信交換中心,與移動(dòng)電話終端之間進(jìn)行信息傳遞的無(wú)線電收發(fā)信電臺(tái)。 單基站定位是指:通過(guò)手機(jī)獲取當(dāng)前連接到的基站信息,來(lái)確定用戶的大概位置(用戶可能在某個(gè)基站(手機(jī)當(dāng)前連接的基站)發(fā)出的無(wú)線電覆蓋區(qū)域中)。 三基站或多基站:通過(guò)手機(jī)獲取附近區(qū)域的基站信息(不是用戶當(dāng)前連接的基站,得到的是一組基站信息),來(lái)確定用戶的大概位置。 獲取鄰區(qū)基站信息: | // 獲取鄰區(qū)基站信息List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();StringBuffer sb = new StringBuffer("總數(shù) : " + infos.size() + "\n");for (NeighboringCellInfo info1 : infos) { // 根據(jù)鄰區(qū)總數(shù)進(jìn)行循環(huán)sb.append(" LAC : " + info1.getLac()); // 取出當(dāng)前鄰區(qū)的LACsb.append(" CID : " + info1.getCid()); // 取出當(dāng)前鄰區(qū)的CIDsb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 獲取鄰區(qū)基站信號(hào)強(qiáng)度} | 注:中國(guó)聯(lián)通的基本上獲取不到附近的基站,我測(cè)的時(shí)候使用的是中國(guó)移動(dòng)。 一、探索: 1、我猜想請(qǐng)求參數(shù)的Json是這樣組裝的: | JSONObject json = new JSONObject();json.put("version", "1.1.0");json.put("host", "maps.google.com");json.put("location_area_code", "6338");json.put("mobile_country_code", "460");json.put("mobile_network_code", "0");json.put("age", 0);JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("cell_id", "62291");json1.put("signal_strength", -95);array.put(json1);JSONObject json2 = new JSONObject();json2.put("cell_id", "62290");json2.put("signal_strength", -101);array.put(json2);json.put("cell_towers", array);
| 2、組拼好的JSON字符串: | {"mobile_network_code":"0","location_area_code":"6338","host":"maps.google.com","cell_towers":[{"signal_strength":-95,"cell_id":"62291"},{"signal_strength":-101,"cell_id":"62290"}],"age":0,"mobile_country_code":"460","version":"1.1.0"}
| 3、通過(guò)HTTP請(qǐng)求返回的JSON形式結(jié)果: | {"location":{"latitude":"0.0","longitude":"0.0","address":{"city":"基站信息不存在,請(qǐng)從手機(jī)上讀取正確的基站信息。","country":"","country_code":"","county":"","postal_code":"","region":"","street":"","street_number":""}},"access_token":"dummytoken"}
| 二、可行的方式: 1、通過(guò)單個(gè)附近的基站信息定位: 組裝參數(shù): | /*** 獲取JSON形式的基站信息* @param mcc 移動(dòng)國(guó)家代碼(中國(guó)的為460)* @param mnc 移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為0,中國(guó)聯(lián)通為1,中國(guó)電信為2); * @param lac 位置區(qū)域碼* @param cid 基站編號(hào)* @param bsss 基站信號(hào)強(qiáng)度* @return json* @throws JSONException*/private String getJsonCellPos(int mcc, int mnc, int lac, int cid, int bsss) throws JSONException {JSONObject jsonCellPos = new JSONObject();jsonCellPos.put("version", "1.1.0");jsonCellPos.put("host", "maps.google.com");JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("location_area_code", "" + lac + "");json1.put("mobile_country_code", "" + mcc + "");json1.put("mobile_network_code", "" + mnc + "");json1.put("age", 0);json1.put("cell_id", "" + cid + "");json1.put("signal_strength", bsss);array.put(json1);jsonCellPos.put("cell_towers", array);return jsonCellPos.toString();}
| 請(qǐng)求的JSON字符串: | {"cell_towers":[{"mobile_network_code":"0","location_area_code":"6338","cell_id":"62291","signal_strength":-95,"age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
| 返回JSON形式結(jié)果: | {
"location":
{
 
; "latitude":"31.214667405",
"longitude":"121.59903152499999",
&nbs
p; "address":
{
 
; "city":"
上海市浦東新區(qū)郭守敬路;上海奧威科技開(kāi)發(fā)公司、
科威國(guó)際技術(shù)轉(zhuǎn)移中心公司、張江高科技園區(qū)熱力中心[附近]",
"country":"",
"country_code":"",
 
; "county":"",
 
; "postal_code":"",
"region":"",
"street":"",
"street_number":""
}
},
"access_token":"dummytoken"
}
| 2、通過(guò)多個(gè)附近的基站信息定位: 又一次猜想: 請(qǐng)求JSON: | {"cell_towers":[{"mobile_network_code":"0","location_area_code":"6338","cell_id":"62291","signal_strength":-95,"age":0,"mobile_country_code":"460"},{"mobile_network_code":"0","location_area_code":"6338","cell_id":"62290","signal_strength":-101,"age":1,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
| 返回JSON: | {"location":{"latitude":"31.21485922285714","longitude":"121.59990774285711","address":{"city":"上海市浦東新區(qū)郭守敬路276號(hào);上海奧威科技開(kāi)發(fā)公司、科威國(guó)際技術(shù)轉(zhuǎn)移中心公司、
張江高科技園區(qū)熱力中心[附近]","country":"","country_code":"","county":"","postal_code":"","region":"","street":"","street_number":""}},"access_token":"dummytoken"}
| 結(jié)論:第三方服務(wù)器端可能有限制,每次只接受JSONArray中的第一個(gè)元素。如果要想一次性查找附近多個(gè)基站的經(jīng)緯度及所在的地址,就只能編寫(xiě)for循環(huán),一次一次的訪問(wèn)第三方的服務(wù)器。 3、個(gè)人觀點(diǎn):所謂的三點(diǎn)定位或多點(diǎn)定位,只是讓想知道用戶當(dāng)前位置的人多了一個(gè)選擇而已。三點(diǎn)定位得到的是三個(gè)點(diǎn),并非是一個(gè)比較準(zhǔn)確的點(diǎn)。 4、補(bǔ)充: 基站在線定位查詢連接:?http://www.gpsspg.com/bs.htm |