android WIFI定位

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

Java代码 复制代码  收藏代码
  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. }  
import java.io.Serializable;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * @author yangzhiqiang
 * 
 */
public class WiFiInfoManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4582739827003032383L;

	private Context context;

	public WiFiInfoManager(Context context) {
		super();
		this.context = context;
	}

	public WifiInfo getWifiInfo() {
		WifiManager manager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = new WifiInfo();
		info.mac = manager.getConnectionInfo().getBSSID();
		Log.i("TAG", "WIFI MAC is:" + info.mac);
		return info;
	}

	public class WifiInfo {

		public String mac;

		public WifiInfo() {
			super();
		}
	}

}

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

Java代码 复制代码  收藏代码
  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.     }  

猜你喜欢

转载自zhouxuebao87.iteye.com/blog/1639873