分享

Android编程实现WIFI定位

 WindySky 2016-07-15

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

 

01.import java.io.Serializable;
02. 
03.import android.content.Context;
04.import android.net.wifi.WifiManager;
05.import android.util.Log;
06. 
07./**
08.* @author yangzhiqiang
09.*
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服务器,代码如下:
www.it165.net

 

01.public static Location getWIFILocation(WifiInfo wifi) {
02.if (wifi == null) {
03.Log.i("TAG", "wifi is null.");
04.return null;
05.}
06.DefaultHttpClient client = new DefaultHttpClient();
07.HttpPost post = new HttpPost("http://www.google.com/loc/json");
08.JSONObject holder = new JSONObject();
09.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.}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多