日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于基站定位及wifi定位

發布時間:2023/12/18 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于基站定位及wifi定位 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基站定位


因為公司的需要,在網上查基站定位,結果發現大部分都是無法實現的,不是代碼有問題就是已經過期,只能自己研究,經過一天的努力,有了一些心得,首先基站定位需要這幾個參數,不管你用的什么接口

lac //連接基站位置區域碼 cellid //連接基站編碼 mcc //MCC國家碼 mnc //MNC網號 signalstrength//連接基站信號強度

然后在基站定位方面,有通過一個基站定位的,有通過幾個基站定位的,一個基站定位就是通過查找這個基站的位置,然后將這個位置作為用戶的位置,準確度不是很高,還有多基站定位,需要得到附近的所有基站數據和信號強度,通過一定的算法得到用戶的位置,這個方法比單基站定位的準確度要高一些。我用的是多基站定位,下面介紹的也是這個方法。

在查資料時,看到聚合的官網有介紹關于獲取基站數據的方法,網站如下,各位可以去看看

http://code.juhe.cn/docs/1965

但是這個網站上面寫的方法照著寫的話會報錯,經過查找,錯誤在這一行代碼:

CellInfoCdma cellInfoCdma = (CellInfoCdma) info;

這個強制轉換的方法是有問題,因為不同的手機返回的數據是不一樣的,我去查了一下android的官方SDK

http://www.android-doc.com/reference/android/telephony/CellInfo.html

關于CellInfo對象的介紹,發現這個對象有四個子類,可以理解為代表不同基站的數據的類,分別是:

CellInfoCdma //電信3G的基站數據 CellInfoGsm //通用的移動聯通電信2G的基站數據 CellInfoLte //4g網絡的基站數據 CellInfoWcdma //聯通3G的基站數據

我在網上查到移動的3G基站是scdma,但這個對象沒有這個子類,原因不明。
到這一步已經可以知道正確的寫法是怎么樣的了,我直接列出我的代碼:

/** 得到基站數據 */@SuppressLint("NewApi")public static void getBaseData(final Context mContext) {// lac連接基站位置區域碼 cellid連接基站編碼 mcc MCC國家碼 mnc MNC網號// signalstrength連接基站信號強度List<BaseDataBean> list = new ArrayList<BaseDataBean>();BaseDataBean beans = new BaseDataBean();TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);String operator = telephonyManager.getNetworkOperator();beans.setMcc(operator.substring(0, 3));beans.setMnc(operator.substring(3));if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {// 這是電信的CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) telephonyManager.getCellLocation();beans.setCell_id(cdmaCellLocation.getBaseStationId() + "");beans.setLac(cdmaCellLocation.getNetworkId() + "");} else {// 這是移動和聯通的GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();beans.setCell_id(gsmCellLocation.getCid() + "");beans.setLac(gsmCellLocation.getLac() + "");}beans.setSignalstrength("0");list.add(beans);List<CellInfo> infoLists = telephonyManager.getAllCellInfo();if (infoLists.size() != 0) {for (CellInfo info : infoLists) {/** 1、GSM是通用的移動聯通電信2G的基站。2、CDMA是3G的基站。3、LTE,則證明支持4G的基站。*/BaseDataBean bean = new BaseDataBean();if (info.toString().contains("CellInfoLte")) {CellInfoLte cellInfoLte = (CellInfoLte) info;CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();bean.setSignalstrength(cellSignalStrengthLte.getDbm() + "");bean.setCell_id(cellIdentityLte.getCi() + "");bean.setLac(cellIdentityLte.getTac() + "");bean.setMcc(cellIdentityLte.getMcc() + "");bean.setMnc(cellIdentityLte.getMnc() + "");} else if (info.toString().contains("CellInfoGsm")) {CellInfoGsm cellInfoGsm = (CellInfoGsm) info;CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();bean.setSignalstrength(cellSignalStrengthGsm.getDbm() + "");bean.setCell_id(cellIdentityGsm.getCid() + "");bean.setLac(cellIdentityGsm.getLac() + "");bean.setMcc(cellIdentityGsm.getMcc() + "");bean.setMnc(cellIdentityGsm.getMnc() + "");} else if (info.toString().contains("CellInfoCdma")) {CellInfoCdma cellInfoCdma = (CellInfoCdma) info;CellIdentityCdma cellIdentityCdma = cellInfoCdma.getCellIdentity();CellSignalStrengthCdma cellSignalStrengthCdma = cellInfoCdma.getCellSignalStrength();bean.setCell_id(cellIdentityCdma.getBasestationId() + "");bean.setSignalstrength(cellSignalStrengthCdma.getCdmaDbm()+ "");/**因為待會我要把這個list轉成gson,所以這個對象的所有屬性我都賦一下值,不必理會這里*/bean.setLac("0");bean.setMcc("0");bean.setMnc("0");}list.add(bean);}}/**到了這里這個list就是你得到的你附近的所有基站數據了。現在可以調用你的通過基站數據定位的接口,我的接口是公司后臺寫給我的。*/}

順便把BaseDataBean這個bean也發出來吧

/*** 基站數據*/ public class BaseDataBean {private String cell_id;// cellid連接基站編碼private String lac;// lac連接基站位置區域碼private String mcc;// mcc MCC國家碼private String mnc;// mnc MNC網號private String signalstrength;// signalstrength連接基站信號強度public String getCell_id() {return cell_id;}public void setCell_id(String cell_id) {this.cell_id = cell_id;}public String getLac() {return lac;}public void setLac(String lac) {this.lac = lac;}public String getMcc() {return mcc;}public void setMcc(String mcc) {this.mcc = mcc;}public String getMnc() {return mnc;}public void setMnc(String mnc) {this.mnc = mnc;}public String getSignalstrength() {return signalstrength;}public void setSignalstrength(String signalstrength) {this.signalstrength = signalstrength;}}

WIFI定位

同樣的,wifi定位接口的話你們自己找,我的接口是公司后臺提供的,wifi定位需要的數據如下

BSSID //wifi的Mac地址 level //wifi的強度,即信號的強弱 SSID //wifi的名字

獲取的方法也很簡單:

/** 得到Wifi數據 */public static void getWifiData(final Context mContext) {WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);List<ScanResult> list = wifiManager.getScanResults();List<WifiDataBean> data = new ArrayList<WifiDataBean>();for (int i = 0; i < list.size(); i++) {WifiDataBean bean = new WifiDataBean();bean.setMac_address(list.get(i).BSSID);bean.setSignal_strength(list.get(i).level+"");bean.setWifi_name(list.get(i).SSID);data.add(bean);}/**到了這里這個data就是你得到的你附近的所有wifi數據了。*/}

把WifiDataBean也發出來吧。

/** * wifi數據*/ public class WifiDataBean {private String wifi_name;//wifi信息名字private String mac_address;//wifi的MAC地址private String signal_strength;//wifi信號強度public String getWifi_name() {return wifi_name;}public void setWifi_name(String wifi_name) {this.wifi_name = wifi_name;}public String getMac_address() {return mac_address;}public void setMac_address(String mac_address) {this.mac_address = mac_address;}public String getSignal_strength() {return signal_strength;}public void setSignal_strength(String signal_strength) {this.signal_strength = signal_strength;}}

調用這倆個方法記得加以下權限(其實沒這么多,但我懶得挑了):

<!-- 用于進行網絡定位 --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" /><!-- 用于訪問GPS定位 --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><!-- 用于獲取運營商信息,用于支持提供運營商信息相關的接口 --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><!-- 用于訪問wifi網絡信息,wifi信息會用于進行網絡定位 --><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!-- 用于獲取wifi的獲取權限,wifi信息會用來進行網絡定位 --><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><!-- 用于訪問網絡,網絡定位需要上網 --><uses-permission android:name="android.permission.INTERNET" /><!-- 用于申請調用A-GPS模塊 --><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

ps:我問了一下后臺這倆個接口是哪找的,然后他表示這倆個接口都是收費的,所以咯,你們還是自己找吧。

總結

以上是生活随笔為你收集整理的关于基站定位及wifi定位的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。