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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

安卓扫描周围基站信息,获取邻小区频段频点

發布時間:2023/12/2 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓扫描周围基站信息,获取邻小区频段频点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在上一家公司做了一個用安卓手機掃描周圍頻點的功能,雖然在安卓 9.0 能獲取到頻段、頻點,但是也只限于自己手機 sim 卡對應的鄰小區,作用不太大。

不過也是有人做這個,用 root 的手機底層開發,可以切換自身頻點,獲取到想要的鄰小區數據,軟件收費,當時手上還有這么個軟件,可是忘了叫什么名字了,有興趣可以自己找找。

下面記錄下我的代碼:

代碼

import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.telephony.CellInfo; import android.telephony.CellInfoGsm; import android.telephony.CellInfoLte; import android.telephony.CellInfoWcdma; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import static android.content.Context.TELEPHONY_SERVICE;public class LteUtils {private static TelephonyManager telephonymanager =(TelephonyManager) MyApplication.getContext().getSystemService(TELEPHONY_SERVICE);public static List<Map<String, Integer>> getLteInfo() {List<Map<String, Integer>> mapList = new ArrayList<>();try {//權限確認if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (MyApplication.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {//直接返回return mapList;}}//獲取周圍小區信息List<CellInfo> allCellinfo = telephonymanager.getAllCellInfo();if (allCellinfo != null) {for (CellInfo cellInfo : allCellinfo) {//GSMif (cellInfo instanceof CellInfoGsm) {CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;Map<String, Integer> map = new HashMap<>();map.put("Ci", cellInfoGsm.getCellIdentity().getCid());map.put("Rsrp", cellInfoGsm.getCellSignalStrength().getDbm());map.put("AsuLevel", cellInfoGsm.getCellSignalStrength().getAsuLevel());map.put("Lac", cellInfoGsm.getCellIdentity().getLac());map.put("Tac", cellInfoGsm.getCellSignalStrength().getDbm());mapList.add(map);//WCDMA} else if (cellInfo instanceof CellInfoWcdma) {CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;Map<String, Integer> map = new HashMap<>();map.put("Ci", cellInfoWcdma.getCellIdentity().getCid());map.put("Rsrp", cellInfoWcdma.getCellSignalStrength().getDbm());map.put("Psc", cellInfoWcdma.getCellIdentity().getPsc());map.put("AsuLevel", cellInfoWcdma.getCellSignalStrength().getAsuLevel());map.put("Lac", cellInfoWcdma.getCellIdentity().getLac());mapList.add(map);//Lte} else if (cellInfo instanceof CellInfoLte) {CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;Map<String, Integer> map = new HashMap<>();map.put("Ci", cellInfoLte.getCellIdentity().getCi());map.put("Tac", cellInfoLte.getCellIdentity().getTac());map.put("Rsrp", cellInfoLte.getCellSignalStrength().getDbm());map.put("Pci", cellInfoLte.getCellIdentity().getPci());//在7.0以上才能獲取頻點if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {map.put("Earfcn", cellInfoLte.getCellIdentity().getEarfcn());} else {map.put("Earfcn", 0);}//9.0才能過去頻段if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {map.put("Bandwidth", cellInfoLte.getCellIdentity().getBandwidth());}map.put("Mcc", cellInfoLte.getCellIdentity().getMcc());map.put("Mnc", cellInfoLte.getCellIdentity().getMnc());mapList.add(map);} else {//舊設備mapList.addAll(getServerCellInfoOnOlderDevices());}}}} catch (Exception e) {//舊設備mapList.addAll(getServerCellInfoOnOlderDevices());}return mapList;}/*** 舊設備(低安卓版本)獲取相關信息*/private static List<Map<String, Integer>> getServerCellInfoOnOlderDevices() {List<Map<String, Integer>> mapList = new ArrayList<>();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (MyApplication.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED&& MyApplication.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {//直接返回return mapList;}}GsmCellLocation location = (GsmCellLocation) telephonymanager.getCellLocation();Map<String, Integer> map = new HashMap<>();map.put("Ci", location.getCid());map.put("Tac", location.getLac());map.put("Psc", location.getPsc());mapList.add(map);return mapList;}/*** 將獲取到的數據轉成 SweepInfo* @param mapList 數據* @return SweepInfo list*/public static List<SweepInfo> LteMapToInfo(List<Map<String, Integer>> mapList) {List<SweepInfo> datas = new ArrayList<>();int index = 0;for (Map<String, Integer> map : mapList) {SweepInfo info = new SweepInfo();info.setOrder(index);info.setBand(Utils.getBandFromRate("" + map.get("Earfcn")));info.setOprater(Utils.getOpretorFromRate("" + map.get("Earfcn")));info.setCi("" + map.get("Ci"));info.setPci("" + map.get("Pci"));info.setRsrp("" + map.get("Rsrp"));info.setEarfcn("" + map.get("Earfcn"));index++;datas.add(info);}return datas;} }

MyApplication 是自己定義的 Application,自己寫一下吧,就是獲取個 context。

  • SweepInfo
public class SweepInfo implements Parcelable, Comparable<SweepInfo> {private int order;private String band;private String earfcn;private String oprater;private String pci;private String priority;private String rsrp;private String tac;private String ci;private String rsrq;private String bw;public String getPci() {return pci;}public void setPci(String pci) {this.pci = pci;}public String getCi() {return ci;}public void setCi(String ci) {this.ci = ci;}public int getOrder() {return order;}public void setOrder(int order) {this.order = order;}public String getOprater() {return oprater;}public void setOprater(String oprater) {this.oprater = oprater;}public String getBand() {return band;}public void setBand(String band) {this.band = band;}public String getEarfcn() {return earfcn;}public void setEarfcn(String earfcn) {this.earfcn = earfcn;}public String getRsrp() {return rsrp;}public void setRsrp(String rsrp) {this.rsrp = rsrp;}public String getPriority() {return priority;}public void setPriority(String priority) {this.priority = priority;}public String getTac() {return tac;}public void setTac(String tac) {this.tac = tac;}public String getRsrq() {return rsrq;}public void setRsrq(String rsrq) {this.rsrq = rsrq;}public String getBw() {return bw;}public void setBw(String bw) {this.bw = bw;}public static final Creator<SweepInfo> CREATOR = new Creator<SweepInfo>() {public SweepInfo createFromParcel(Parcel source) {SweepInfo info = new SweepInfo();info.setOrder(source.readInt());info.setBand(source.readString());info.setEarfcn(source.readString());info.setOprater(source.readString());info.setPci(source.readString());info.setPriority(source.readString());info.setRsrp(source.readString());info.setTac(source.readString());info.setCi(source.readString());info.setRsrq(source.readString());info.setBw(source.readString());return info;}public SweepInfo[] newArray(int size) {return new SweepInfo[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel parcel, int i) {parcel.writeInt(order);parcel.writeString(band);parcel.writeString(earfcn);parcel.writeString(oprater);parcel.writeString(pci);parcel.writeString(priority);parcel.writeString(rsrp);parcel.writeString(tac);parcel.writeString(ci);parcel.writeString(rsrq);parcel.writeString(bw);}@Overridepublic int compareTo(@NonNull SweepInfo sweepInfo) {return - Integer.parseInt(this.rsrp) + Integer.parseInt(sweepInfo.getRsrp());}}

這就是個數據類,用來存放掃描到的頻點信息。

使用

//申請權限 requestRunTimePermission(new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.ACCESS_COARSE_LOCATION}, new BaseActivity.PermissionListener() {@Overridepublic void onGranted() {//較為耗時,移到進程中處理new Thread(() -> {try{ArrayList<SweepInfo> infoTemp = new ArrayList<>(LteUtils.LteMapToInfo(LteUtils.getLteInfo()));//do u want in UI threadrunOnUiThread(new Runnable() {@Overridepublic void run() {}});}catch (Exception e){e.printStackTrace();}}).start();}@Overridepublic void onGranted(List<String> grantedPermission) {}@Overridepublic void onDenied(List<String> deniedPermission) {} });

這里需要確認下權限,記得在 manifest 里面加上,這個申請權限的方法是我封裝的,讀者自己申請下,很簡單的。

相關名詞介紹

  • MCC,Mobile Country Code,移動國家代碼(中國的為460);

  • MNC,Mobile Network Code,移動網絡號碼(中國移動為0,中國聯通為1,中國電信為2);

  • LAC,Location Area Code,位置區域碼;

  • CID,Cell Identity,基站編號;

  • BSSS,Base station signal strength,基站信號強度。

  • eNB E-UTRAN Node B 為LTE系統中E-UTRAN的組成部分,計算eNB的方式是 ci = eNB*256+cid

結語

這里雖然能獲取到一些頻段、頻點信息,可是限制性還剩挺大的,上面代碼也很簡陋。這里也推薦一個軟件,里面有獲取 LTE、WiFi、Gps 相關信息的功能,雖然有廣告,還是不錯的,名字叫 cellular - Z,可以自己去下載。

總結

以上是生活随笔為你收集整理的安卓扫描周围基站信息,获取邻小区频段频点的全部內容,希望文章能夠幫你解決所遇到的問題。

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