android 基站定位 wifi定位

来源:互联网 发布:nginx 反向代理 跨域 编辑:程序博客网 时间:2024/05/16 09:45

http://blog.csdn.net/yaoyeyzq/article/details/7885236

 android 基站定位

 

这里给大家分享下基站定位的实现,基站定位首先要通过TelephonyManager得到手机的信号信息,比如基站的国家编码,小区id等......得到这些后需要向google提供的接口提交这些参数,然后就会返回基站的相关信息。

废话不多说直接上代码吧:

[java] view plaincopyprint?
  1. import java.io.Serializable;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.telephony.NeighboringCellInfo;  
  7. import android.telephony.TelephonyManager;  
  8. import android.telephony.cdma.CdmaCellLocation;  
  9. import android.telephony.gsm.GsmCellLocation;  
  10. import android.util.Log;  
  11.   
  12. /** 
  13.  * @author yangzhiqiang 
  14.  *  
  15.  */  
  16. public class CellIdInfoManager implements Serializable {  
  17.   
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = 5481154371450408380L;  
  22.   
  23.     private Context context;  
  24.   
  25.     public CellIdInfoManager(Context context) {  
  26.         super();  
  27.         this.context = context;  
  28.     }  
  29.   
  30.     public List<CellInfo> getCellInfo() {  
  31.         List<CellInfo> listInfo = new ArrayList<CellInfo>();  
  32.   
  33.         int countryCode;  
  34.         int networkCode;  
  35.         int areaCode;  
  36.         CellInfo info = new CellInfo();  
  37.         GsmCellLocation gsm = null;  
  38.   
  39.         TelephonyManager manager = (TelephonyManager) context  
  40.                 .getSystemService(Context.TELEPHONY_SERVICE);  
  41.         if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {  
  42.             gsm = (GsmCellLocation) manager.getCellLocation();  
  43.             if (gsm == null) {  
  44.                 return null;  
  45.             }  
  46.             if (manager.getNetworkOperator() == null  
  47.                     || manager.getNetworkOperator().length() == 0) {  
  48.                 return null;  
  49.             }  
  50.             countryCode = Integer.parseInt(manager.getNetworkOperator()  
  51.                     .substring(03));  
  52.             networkCode = Integer.parseInt(manager.getNetworkOperator()  
  53.                     .substring(35));  
  54.             areaCode = gsm.getLac();  
  55.   
  56.             info.cellId = gsm.getCid();  
  57.             info.mobileCountryCode = countryCode;  
  58.             info.mobileNetworkCode = networkCode;  
  59.             info.locationAreaCode = areaCode;  
  60.             info.radio_type = "gsm";  
  61.   
  62.             listInfo.add(info);  
  63.   
  64.             List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();  
  65.             for (NeighboringCellInfo i : list) {  
  66.                 CellInfo ci = new CellInfo();  
  67.                 ci.cellId = i.getCid();  
  68.                 ci.mobileCountryCode = countryCode;  
  69.                 ci.mobileNetworkCode = networkCode;  
  70.                 ci.locationAreaCode = areaCode;  
  71.                 ci.radio_type = "gsm";  
  72.                 listInfo.add(ci);  
  73.             }  
  74.         } else if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {  
  75.             CdmaCellLocation cdma = (CdmaCellLocation) manager  
  76.                     .getCellLocation();  
  77.             if (cdma == null) {  
  78.                 return null;  
  79.             }  
  80.             if (manager.getNetworkOperator() == null  
  81.                     || manager.getNetworkOperator().length() == 0) {  
  82.                 return null;  
  83.             }  
  84.             Log.v("TAG""CDMA");  
  85.             info.cellId = cdma.getBaseStationId();  
  86.             info.mobileCountryCode = Integer.parseInt(manager  
  87.                     .getNetworkOperator());  
  88.             info.mobileNetworkCode = cdma.getSystemId();  
  89.             info.locationAreaCode = cdma.getNetworkId();  
  90.             info.radio_type = "cdma";  
  91.             listInfo.add(info);  
  92.         }  
  93.         return listInfo;  
  94.     }  
  95.   
  96.     public class CellInfo {  
  97.   
  98.         // 基站编号  
  99.         public int cellId;  
  100.         // 国家代码  
  101.         public int mobileCountryCode;  
  102.         // 网络代码  
  103.         public int mobileNetworkCode;  
  104.         // 区域代码  
  105.         public int locationAreaCode;  
  106.   
  107.         public String radio_type;  
  108.   
  109.         public CellInfo() {  
  110.             super();  
  111.         }  
  112.   
  113.     }  
  114. }  

上面是得到手机信号的信息,下面是将这些信息发送到google服务器并解析结果:

[java] view plaincopyprint?
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.HttpStatus;  
  9. import org.apache.http.client.methods.HttpPost;  
  10. import org.apache.http.entity.StringEntity;  
  11. import org.apache.http.impl.client.DefaultHttpClient;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14.   
  15. import android.location.Location;  
  16. import android.util.Log;  
  17.   
  18. import com.metarnet.gps.CellIdInfoManager.CellInfo;  
  19.   
  20. /** 
  21.  * @author Administrator 
  22.  *  
  23.  */  
  24. public class NetworkLocationManager implements Serializable {  
  25.   
  26.     /** 
  27.      *  
  28.      */  
  29.     private static final long serialVersionUID = 1185788569820321281L;  
  30.   
  31.     public static Location getBaseStationLocation(List<CellInfo> cellID) {  
  32.         if (cellID == null) {  
  33.             Log.i("TAG""cellId is null.");  
  34.             return null;  
  35.         }  
  36.         DefaultHttpClient client = new DefaultHttpClient();  
  37.         HttpPost post = new HttpPost("http://www.google.com/loc/json");  
  38.         JSONObject holder = new JSONObject();  
  39.         try {  
  40.             CellInfo info = cellID.get(0);  
  41.             holder.put("version""1.1.0");  
  42.             holder.put("host""maps.google.com");  
  43.             holder.put("home_mobile_country_code", info.mobileCountryCode);  
  44.             holder.put("home_mobile_network_code", info.mobileNetworkCode);  
  45.             holder.put("request_address"true);  
  46.             holder.put("radio_type", info.radio_type);  
  47.             if ("460".equals(info.mobileCountryCode)) {  
  48.                 holder.put("address_language""zh_CN");  
  49.             } else {  
  50.                 holder.put("address_language""en_US");  
  51.             }  
  52.   
  53.             JSONObject data, current_data;  
  54.             JSONArray array = new JSONArray();  
  55.   
  56.             current_data = new JSONObject();  
  57.             current_data.put("cell_id", info.cellId);  
  58.             current_data.put("location_area_code", info.locationAreaCode);  
  59.             current_data.put("mobile_country_code", info.mobileCountryCode);  
  60.             current_data.put("mobile_network_code", info.mobileNetworkCode);  
  61.             current_data.put("age"0);  
  62.             array.put(current_data);  
  63.   
  64.             if (cellID.size() > 2) {  
  65.                 for (int i = 1; i < cellID.size(); i++) {  
  66.                     data = new JSONObject();  
  67.                     data.put("cell_id", info.cellId);  
  68.                     data.put("location_area_code", info.locationAreaCode);  
  69.                     data.put("mobile_country_code", info.mobileCountryCode);  
  70.                     data.put("mobile_network_code", info.mobileNetworkCode);  
  71.                     data.put("age"0);  
  72.                     array.put(data);  
  73.                 }  
  74.             }  
  75.             holder.put("cell_towers", array);  
  76.   
  77.             StringEntity se = new StringEntity(holder.toString());  
  78.             post.setEntity(se);  
  79.             HttpResponse resp = client.execute(post);  
  80.             int state = resp.getStatusLine().getStatusCode();  
  81.             if (state == HttpStatus.SC_OK) {  
  82.                 HttpEntity entity = resp.getEntity();  
  83.                 if (entity != null) {  
  84.                     BufferedReader br = new BufferedReader(  
  85.                             new InputStreamReader(entity.getContent()));  
  86.                     StringBuffer sb = new StringBuffer();  
  87.                     String resute = "";  
  88.                     while ((resute = br.readLine()) != null) {  
  89.                         sb.append(resute);  
  90.                     }  
  91.                     br.close();  
  92.   
  93.                     data = new JSONObject(sb.toString());  
  94.                     data = (JSONObject) data.get("location");  
  95.   
  96.                     Location loc = new Location(  
  97.                             android.location.LocationManager.NETWORK_PROVIDER);  
  98.                     loc.setLatitude((Double) data.get("latitude"));  
  99.                     loc.setLongitude((Double) data.get("longitude"));  
  100.                     loc.setAccuracy(Float.parseFloat(data.get("accuracy")  
  101.                             .toString()));  
  102.                     loc.setTime(System.currentTimeMillis());  
  103.                     return loc;  
  104.                 } else {  
  105.                     return null;  
  106.                 }  
  107.             } else {  
  108.                 Log.v("TAG", state + "");  
  109.                 return null;  
  110.             }  
  111.   
  112.         } catch (Exception e) {  
  113.             Log.e("TAG", e.getMessage());  
  114.             return null;  
  115.         }  
  116.     }  
  117. }  

 

 

 http://blog.csdn.net/yaoyeyzq/article/details/7885474

WIFI定位其实和基站定位都差不多,只需要把WIFI的MAC地址取到传给google就行了,下面是具体实现!

[java] view plaincopyprint?
  1. import java.io.Serializable;  
  2.   
  3. import android.content.Context;  
  4. import android.net.wifi.WifiManager;  
  5. import android.util.Log;  
  6.   
  7. /** 
  8.  * @author yangzhiqiang 
  9.  *  
  10.  */  
  11. public class WiFiInfoManager implements Serializable {  
  12.   
  13.     /** 
  14.      *  
  15.      */  
  16.     private static final long serialVersionUID = -4582739827003032383L;  
  17.   
  18.     private Context context;  
  19.   
  20.     public WiFiInfoManager(Context context) {  
  21.         super();  
  22.         this.context = context;  
  23.     }  
  24.   
  25.     public WifiInfo getWifiInfo() {  
  26.         WifiManager manager = (WifiManager) context  
  27.                 .getSystemService(Context.WIFI_SERVICE);  
  28.         WifiInfo info = new WifiInfo();  
  29.         info.mac = manager.getConnectionInfo().getBSSID();  
  30.         Log.i("TAG""WIFI MAC is:" + info.mac);  
  31.         return info;  
  32.     }  
  33.   
  34.     public class WifiInfo {  
  35.   
  36.         public String mac;  
  37.   
  38.         public WifiInfo() {  
  39.             super();  
  40.         }  
  41.     }  
  42.   
  43. }  

上面是取到WIFI的mac地址的方法,下面是把地址发送给google服务器,代码如下:

[java] view plaincopyprint?
  1. public static Location getWIFILocation(WifiInfo wifi) {  
  2.         if (wifi == null) {  
  3.             Log.i("TAG""wifi is null.");  
  4.             return null;  
  5.         }  
  6.         DefaultHttpClient client = new DefaultHttpClient();  
  7.         HttpPost post = new HttpPost("http://www.google.com/loc/json");  
  8.         JSONObject holder = new JSONObject();  
  9.         try {  
  10.             holder.put("version""1.1.0");  
  11.             holder.put("host""maps.google.com");  
  12.   
  13.             JSONObject data;  
  14.             JSONArray array = new JSONArray();  
  15.             if (wifi.mac != null && wifi.mac.trim().length() > 0) {  
  16.                 data = new JSONObject();  
  17.                 data.put("mac_address", wifi.mac);  
  18.                 data.put("signal_strength"8);  
  19.                 data.put("age"0);  
  20.                 array.put(data);  
  21.             }  
  22.             holder.put("wifi_towers", array);  
  23.             Log.i("TAG""request json:" + holder.toString());  
  24.             StringEntity se = new StringEntity(holder.toString());  
  25.             post.setEntity(se);  
  26.             HttpResponse resp = client.execute(post);  
  27.             int state = resp.getStatusLine().getStatusCode();  
  28.             if (state == HttpStatus.SC_OK) {  
  29.                 HttpEntity entity = resp.getEntity();  
  30.                 if (entity != null) {  
  31.                     BufferedReader br = new BufferedReader(  
  32.                             new InputStreamReader(entity.getContent()));  
  33.                     StringBuffer sb = new StringBuffer();  
  34.                     String resute = "";  
  35.                     while ((resute = br.readLine()) != null) {  
  36.                         sb.append(resute);  
  37.                     }  
  38.                     br.close();  
  39.   
  40.                     Log.i("TAG""response json:" + sb.toString());  
  41.                     data = new JSONObject(sb.toString());  
  42.                     data = (JSONObject) data.get("location");  
  43.   
  44.                     Location loc = new Location(  
  45.                             android.location.LocationManager.NETWORK_PROVIDER);  
  46.                     loc.setLatitude((Double) data.get("latitude"));  
  47.                     loc.setLongitude((Double) data.get("longitude"));  
  48.                     loc.setAccuracy(Float.parseFloat(data.get("accuracy")  
  49.                             .toString()));  
  50.                     loc.setTime(System.currentTimeMillis());  
  51.                     return loc;  
  52.                 } else {  
  53.                     return null;  
  54.                 }  
  55.             } else {  
  56.                 Log.v("TAG", state + "");  
  57.                 return null;  
  58.             }  
  59.   
  60.         } catch (Exception e) {  
  61.             Log.e("TAG", e.getMessage());  
  62.             return null;  
  63.         }  
  64.     }