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

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值