WIFI定位其实和基站定位都差不多,只需要把WIFI的MAC地址取到传给google就行了,下面是具体实现!
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服务器,代码如下:
public static Location getWIFILocation(WifiInfo wifi) {
if (wifi == null) {
Log.i("TAG", "wifi is null.");
return null;
}
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
JSONObject holder = new JSONObject();
try {
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
JSONObject data;
JSONArray array = new JSONArray();
if (wifi.mac != null && wifi.mac.trim().length() > 0) {
data = new JSONObject();
data.put("mac_address", wifi.mac);
data.put("signal_strength", 8);
data.put("age", 0);
array.put(data);
}
holder.put("wifi_towers", array);
Log.i("TAG", "request json:" + holder.toString());
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
int state = resp.getStatusLine().getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity entity = resp.getEntity();
if (entity != null) {
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String resute = "";
while ((resute = br.readLine()) != null) {
sb.append(resute);
}
br.close();
Log.i("TAG", "response json:" + sb.toString());
data = new JSONObject(sb.toString());
data = (JSONObject) data.get("location");
Location loc = new Location(
android.location.LocationManager.NETWORK_PROVIDER);
loc.setLatitude((Double) data.get("latitude"));
loc.setLongitude((Double) data.get("longitude"));
loc.setAccuracy(Float.parseFloat(data.get("accuracy")
.toString()));
loc.setTime(System.currentTimeMillis());
return loc;
} else {
return null;
}
} else {
Log.v("TAG", state + "");
return null;
}
} catch (Exception e) {
Log.e("TAG", e.getMessage());
return null;
}
}
分享到:
相关推荐
打开清单文件,查看使用WiFi定位的权限; 打开程序文件MainActivity,查看检测WiFi是否打开及开启WiFi的代码; 查看位置管理器方法requestLocationUpdates()中位置监听器的用法,并与GPSLocation工程中的位置器用法...
基于此,本课题重点研究并改进指纹定位算法,设计实现基于Android的WIFI室内定位系统。 首先,通过阅读大量相关的文献资料,对比分析了当前国内外WIFI室内指纹定位技术的研究现状对其中涉及到的相关技术的原理和特点...
Android Wifi Lbs 定位
android google 定位程序 wifi基站GPS定位 等综合定位代码,
众所周知的,在OPhone和大部分国产的Android定制机里不支持最简单实用的基站和WIFI定位,只能使用速度慢而耗电的GPS定位,但OPhone和华为/中兴生产的一些Android定制机却占据了一定的市场,因此导致了很多使用了定位...
安卓Android源码——androidGPS及WIFI基站定位坐标源码.zip
android GPS和基站定位程序及源码.rar 实现基站定位,以及WIFI定位,以及GPS定位,参考使用。
这个android wifi定位demo亲自试了,很好!我上传的使用android studio写的,里边的API key要你们自己去百度地图API官网去申请,反正也很简单,一分钟就能搞定。
WiFiLocation是一款基于K-NN算法的简易WiFi定位系统。它借助 LitePal 建立本地的WiFi指纹数据库,通过一系列API实现目标地点的WiFi指纹搜集、更新、重置或删除,以及当前位置的定位识别。
Android支持GPS、WIFI、网络定位
本文首先设计了整体的系统框架,通过研究分析了该算法目前存在的问题,提出了改进方案,并在Android平台上实现完整的定位系统。
Android的基站、WIFI、GPS定位三点定位结合
androidGPS及WIFI基站定位坐标源码.zip
WIFI室内定位Android APP,包括定位APP和数据采集APP
androidGPS及WIFI基站定位坐标源码
基于Android的室内WIFI定位系统设计源码,包含54个文件,主要文件类型有17个xml文件,16个png图像文件,9个java源文件,3个gradle构建文件,2个gitignore文件用于版本控制,2个properties配置文件,1个Markdown文档...
android LBS定位实例,gps定位, wifi 定位,
android齐全的定位源码。wifi,GPS,基站三种定位方式都有。还有界面
Android系列之Wifi定位
定位的源码,包含基站,wifi,gps。