java wifi定位原理_android WIFI定位和基站定位实现

该博客介绍了在Android中如何利用基站信息(MCC, MNC, LAC, CID)和WiFi信号进行定位。首先通过TelephonyManager获取GSM基站信息,然后构造POST请求将数据发送到定位服务API,解析返回的JSON以获取经纬度坐标。" 105994000,8407145,知识图谱可视化:应用现状与关键技术,"['知识图谱', '数据可视化', '自然语言处理']
摘要由CSDN通过智能技术生成

import java.io.BufferedReader;

import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONObject;

import com.demo.location.Util;

import android.content.Context;

import android.telephony.TelephonyManager;

import android.telephony.gsm.GsmCellLocation;

public class CellLocationManager {

private Context mContext;

public CellLocationManager(Context context){

mContext = context;

}

/** 基站信息结构体 */

public class SCell{

public int MCC;

public int MNC;

public int LAC;

public int CID;

}

/**

* 获取基站信息

*

* @throws Exception

*/

private SCell getCellInfo() throws Exception {

SCell cell = new SCell();

/** 调用API获取基站信息 */

TelephonyManager mTelNet = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);

GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();

if (location == null)

throw new Exception("获取基站信息失败");

String operator = mTelNet.getNetworkOperator();

int mcc = Integer.parseInt(operator.substring(0, 3));

int mnc = Integer.parseInt(operator.substring(3));

int cid = location.getCid();

int lac = location.getLac();

/** 将获得的数据放到结构体中 */

cell.MCC = mcc;

cell.MNC = mnc;

cell.LAC = lac;

cell.CID = cid;

return cell;

}

public String getLocationCell(){

SCell cell = null;

try {

cell = getCellInfo();

} catch (Exception e1) {

Util.loge("getLocationCell: getCellInfo: error: " + e1.getMessage());

return null;

}

/** 采用Android默认的HttpClient */

HttpClient client = new DefaultHttpClient();

/** 采用POST方法 */

HttpPost post = new HttpPost(Util.LOCATION_URL);

try {

/** 构造POST的JSON数据 */

JSONObject holder = new JSONObject();

holder.put("version", "1.1.0");

holder.put("host", Util.LOCATION_HOST);

holder.put("address_language", "zh_CN");

holder.put("request_address", true);

holder.put("radio_type", "gsm");

holder.put("carrier", "HTC");

JSONObject tower = new JSONObject();

tower.put("mobile_country_code", cell.MCC);

//          Util.logi("getLocationCell: mobile_country_code = " + cell.MCC );

tower.put("mobile_network_code", cell.MNC);

//          Util.logi("getLocationCell: mobile_network_code = " + cell.MNC );

tower.put("cell_id", cell.CID);

//          Util.logi("getLocationCell: cell_id = " + cell.CID );

tower.put("location_area_code", cell.LAC);

//          Util.logi("getLocationCell: location_area_code = " + cell.LAC );

JSONArray towerarray = new JSONArray();

towerarray.put(tower);

holder.put("cell_towers", towerarray);

StringEntity query = new StringEntity(holder.toString());

Util.logi("getLocationCell: holder: " + holder.toString());

post.setEntity(query);

/** 发出POST数据并获取返回数据 */

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();

BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));

StringBuffer strBuff = new StringBuffer();

String result = null;

while ((result = buffReader.readLine()) != null) {

strBuff.append(result);

}

/** 解析返回的JSON数据获得经纬度 */

JSONObject json = new JSONObject(strBuff.toString());

JSONObject subjosn = new JSONObject(json.getString("location"));

String latitude = subjosn.getString("latitude");

String longitude = subjosn.getString("longitude");

return Util.getLocation(latitude, longitude);

} catch (Exception e) {

Util.loge("getLocationCell: error: " + e.getMessage());

} finally{

post.abort();

client = null;

}

return null;

}

}

随着人们对基于位置的服务(Location Based Service,LBS)需求日益增大,以及无线通信技术的快速发展,无线定位技术成为了一个研究热点。人们在室外广泛使用目前较成熟的GPS,A-GPS等定位系统进行定位,但是在复杂的室内环境中,这些技术的定位精度不高,不能满足室内定位的需求。WIFI网络具有通信快速、部署方便的特点,它在室内场所广受欢迎.Android系统从几年前发布以来在智能手机操作系统市场占有率不断升高,成为目前使用最为广泛的智能手机操作系统,同时Android移动终端自身具备WIFI无线连接功能。指纹定位算法以其独特的优势减小了对室内难以精确定义的信号传播模型的依赖性,成为定位技术中的一个研究热点。基于此,本课题重点研究并改进指纹定位算法,设计实现基于AndroidWIFI室内定位系统。 首先,通过阅读大量相关的文献资料,对比分析了当前国内外WIFI室内指纹定位技术的研究现状对其中涉及到的相关技术的原理和特点进行介绍分析,包括WIF1无线通信技术,室内无线定位技术以及位置指纹定位技术,并根据室内WIFI指纹定位技术的特征对定位过程中的影响因素进行分析。 其次,根据前面提到的定位过程中的关键影响因素,介绍了对应的解决方案。分析与研究了几种典型的指纹定位算法,包括最近邻法(NN).K近邻法(KNN)、K加权近邻法(WKNN),并提出算法的改进方案,使用MATLAB软件进行算法的仿真分析,寻求其中的最佳参数值以及定位性能差异。通过分析几种算法的性能仿真结果,拟定了基于最强AP法的改进算法作为定位系统采纳的算法。 然后,通过对基于AndroidWIFI室内定位系统的需求分析,提出了一种基于Android 的WIF1室内定位系统设计方案。接着介绍了定位系统软件开发环境,并设计了定位系统总体架构,以及定位系统的各个功能模块。在各项设计确定以后,采用JAVA语言编程实现定位系统的各项功能。 最后,搭建了WIFI室内定位实验环境,使用完成的室内定位系统结合硬件资源,在实验环境下,进行离线阶段创建数据库以及在线阶段的定位测试,并记录呈现在定位客户端上定位结果,分析对应的定位性能.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值