Android获取手机基站信息并进行基站定位(基站定位原理)

http://blog.csdn.net/mad1989/article/details/9970431

一,首先普及一下手机基站信息中相关的专业词汇:

 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释:
 MCC,Mobile Country Code,移动国家代码(中国的为460);
 MNC,Mobile Network Code,移动网络号码(中国移动为0,中国联通为1,中国电信为2); 
 LAC,Location Area Code,位置区域码;
 CID,Cell Identity,基站编号;
 BSSS,Base station signal strength,基站信号强度。




二,获取手机卡基站信息(前提需要有手机卡,模拟器无法实现):

[java]  view plain copy
  1. /** 
  2.  * 获取手机基站信息 
  3.  * @throws JSONException  
  4.  */  
  5. public void getGSMCellLocationInfo() throws JSONException{  
  6.       
  7.     TelephonyManager manager = (TelephonyManager) mAppMain.getSystemService(Context.TELEPHONY_SERVICE);  
  8.       
  9.     String operator = manager.getNetworkOperator();  
  10.     /**通过operator获取 MCC 和MNC */  
  11.     int mcc = Integer.parseInt(operator.substring(03));  
  12.     int mnc = Integer.parseInt(operator.substring(3));  
  13.       
  14.     GsmCellLocation location = (GsmCellLocation) manager.getCellLocation();  
  15.       
  16.     /**通过GsmCellLocation获取中国移动和联通 LAC 和cellID */  
  17.     int lac = location.getLac();  
  18.     int cellid = location.getCid();  
  19.     /**通过CdmaCellLocation获取中国电信 LAC 和cellID */  
  20.       <span style="white-space:pre">        </span> /*CdmaCellLocation location1 = (CdmaCellLocation) mTelephonyManager.getCellLocation();  
  21.        <span style="white-space:pre">   </span> lac = location1.getNetworkId();  
  22.       <span style="white-space:pre">        </span> cellId = location1.getBaseStationId();  
  23.       <span style="white-space:pre">        </span> cellId /= 16;*/    
  24.       
  25.     int strength = 0;  
  26.     /**通过getNeighboringCellInfo获取BSSS */  
  27.     List<NeighboringCellInfo> infoLists = manager.getNeighboringCellInfo();  
  28.     System.out.println("infoLists:"+infoLists+"     size:"+infoLists.size());  
  29.     for (NeighboringCellInfo info : infoLists) {  
  30.         strength+=(-133+2*info.getRssi());// 获取邻区基站信号强度    
  31.         //info.getLac();// 取出当前邻区的LAC   
  32.         //info.getCid();// 取出当前邻区的CID   
  33.         System.out.println("rssi:"+info.getRssi()+"   strength:"+strength);  
  34.     }  
  35.       
  36.       
  37.     //以下内容是把得到的信息组合成json体,然后发送给我的服务器,获取经纬度信息  
[java]  view plain copy
  1. <span style="white-space:pre">      </span>//如果你没有服务器支持,可以发送给BaiduMap,GoogleMap等地图服务商,具体看定位相关的API格式要求  
  2.         JSONObject item = new JSONObject();  
  3.         item.put("cid", cellid);  
  4.         item.put("lac", lac);  
  5.         item.put("mnc", mnc);  
  6.         item.put("mcc", mcc);  
  7.         item.put("strength", strength);  
  8.           
  9.         JSONArray cells = new JSONArray();  
  10.         cells.put(0, item);  
  11.           
  12.         JSONObject json = new JSONObject();  
  13.         json.put("cells", cells);  
  14.           
  15.         CellLocationTask task = new CellLocationTask(json);  
  16.         task.execute();  
  17.           
  18.     }  


三,通过基站信息,请求获取经纬度
CellLocationTask

[java]  view plain copy
  1. /** 
  2.  * 异步请求,通过封装的手机基站信息json体 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6. class CellLocationTask extends AsyncTask<String, Void, String>{  
  7.       
  8.     private JSONObject mJson;  
  9.     private HttpClient mClient;  
  10.     private HttpResponse response;  
  11.     private String responseString;  
  12.     public CellLocationTask(JSONObject json) {  
  13.         this.mJson = json;  
  14.           
  15.     }  
  16.     @Override  
  17.     protected void onPreExecute() {  
  18.         super.onPreExecute();  
  19.         mClient = new DefaultHttpClient();  
  20.         mClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);  
  21.         mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);  
  22.     }  
  23.       
  24.       
  25.     @Override  
  26.     protected String doInBackground(String... params) {  
  27.         String url = "http://我的服务器地址";  
  28.           
  29.         try {  
  30.             HttpPost post = new HttpPost(url);  
  31.             post.setEntity(new StringEntity(mJson.toString(), HTTP.UTF_8));  
  32.             post.addHeader("Content-Type""application/json");  
  33.             response = mClient.execute(post);  
  34.             int statusCode = response.getStatusLine().getStatusCode();  
  35.             System.out.println("doinbackground:"+statusCode);  
  36.             if (statusCode == 200) {  
  37.                 responseString  = EntityUtils.toString(response.getEntity());  
  38.                 System.out.println("返回结果:"+responseString);  
  39.             }  
  40.               
  41.               
  42.         } catch (ClientProtocolException e) {  
  43.             e.printStackTrace();  
  44.         } catch (IOException e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.           
  48.           
  49.           
  50.         return responseString;  
  51.     }  
  52.       
  53.     @Override  
  54.     protected void onPostExecute(String result) {  
  55.         super.onPostExecute(result);  
  56.         System.out.println("onPostExecute:"+result);  
  57.           
  58.         JSONObject json;  
  59.         try {  
  60.             json = new JSONObject(result);  
  61.             JSONObject mresult = json.getJSONObject("result");  
  62.             JSONObject geo = mresult.getJSONObject("geo");  
  63.             double lat = geo.getDouble("lat");  
  64.             double lng = geo.getDouble("lng");  
  65.             mLocationGeoPoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));  
  66.               
  67.             CustomOverlay overlay = new CustomOverlay(mAppMain);  
  68.             mMapView.getOverlays().add(overlay);  
  69.             mMapController.animateTo(mLocationGeoPoint);  
  70.               
  71.         } catch (JSONException e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.           
  75.     }  
  76.       
  77. }  


四,地图显示当前位置

CustomOverlay

[java]  view plain copy
  1. class  CustomOverlay extends ItemizedOverlay<OverlayItem>{  
  2.     public CustomOverlay(Context context) throws NumberFormatException, JSONException {  
  3.         super(context.getResources().getDrawable(R.drawable.mylocation));  
  4.         populate();  
  5.     }  
[java]  view plain copy
  1. @Override  
  2. protected OverlayItem createItem(int arg0) {  
  3.     //mLocationGeoPoint为全局变量,CellLocationTask异步得到的经纬度  
  4.     OverlayItem overlayItem = new OverlayItem(mLocationGeoPoint,  
  5.             """");  
  6.     return overlayItem;  
  7. }  
  8.   
  9. @Override  
  10. public int size() {  
  11.     return 1;  
  12. }  
  13. @Override  
  14. public boolean onTap(GeoPoint arg0, MapView arg1) {  
  15.     return super.onTap(arg0, arg1);  
  16. }  


演示效果:


放到百度地图中,位置有些偏差,这个就需要纠偏了,因为通过基站信息请求到的位置数据并非是百度的数据。以上就是通过基站信息,进行基站定位的实现原理,就是通过MCC,MNC,LAC,CID等属性,请求位置数据,大致就是这个样子。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值