Skip to content

Instantly share code, notes, and snippets.

@iyashamihsan
Created April 29, 2019 11:59
Show Gist options
  • Save iyashamihsan/c7d437981f0988f88f4d82ad0f666d76 to your computer and use it in GitHub Desktop.
Save iyashamihsan/c7d437981f0988f88f4d82ad0f666d76 to your computer and use it in GitHub Desktop.
An example to get cell towers info on Android
package com.mirrordust.gpscellinfodemo;
public class BaseStation {
private int mcc; // Mobile Country Code
private int mnc; // Mobile Network Code
private int lac; // Location Area Code or TAC(Tracking Area Code) for LTE
private int cid; // Cell Identity
private int arfcn; // Absolute RF Channel Number (or UMTS Absolute RF Channel Number for WCDMA)
private int bsic_psc_pci; /* bsic for GSM, psc for WCDMA, pci for LTE,
GSM has #getPsc() but always get Integer.MAX_VALUE,
psc is undefined for GSM */
private double lon; // Base station longitude
private double lat; // Base station latitude
private int asuLevel; /* Signal level as an asu value, asu is calculated based on 3GPP RSRP
for GSM, between 0..31, 99 is unknown
for WCDMA, between 0..31, 99 is unknown
for LTE, between 0..97, 99 is unknown
for CDMA, between 0..97, 99 is unknown */
private int signalLevel; // Signal level as an int from 0..4
private int dbm; // Signal strength as dBm
private String type; // Signal type, GSM or WCDMA or LTE or CDMA
// Getter and Setter for all fields
public int getMcc() {
return mcc;
}
public void setMcc(int mcc) {
this.mcc = mcc;
}
public int getMnc() {
return mnc;
}
public void setMnc(int mnc) {
this.mnc = mnc;
}
public int getLac() {
return lac;
}
public void setLac(int lac) {
this.lac = lac;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getArfcn() {
return arfcn;
}
public void setArfcn(int arfcn) {
this.arfcn = arfcn;
}
public int getBsic_psc_pci() {
return bsic_psc_pci;
}
public void setBsic_psc_pci(int bsic_psc_pci) {
this.bsic_psc_pci = bsic_psc_pci;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public int getAsuLevel() {
return asuLevel;
}
public void setAsuLevel(int asuLevel) {
this.asuLevel = asuLevel;
}
public int getSignalLevel() {
return signalLevel;
}
public void setSignalLevel(int signalLevel) {
this.signalLevel = signalLevel;
}
public int getDbm() {
return dbm;
}
public void setDbm(int dbm) {
this.dbm = dbm;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "BaseStation{" +
"mcc=" + mcc +
", mnc=" + mnc +
", lac=" + lac +
", cid=" + cid +
", arfcn=" + arfcn +
", bsic_psc_pci=" + bsic_psc_pci +
", lon=" + lon +
", lat=" + lat +
", asuLevel=" + asuLevel +
", signalLevel=" + signalLevel +
", dbm=" + dbm +
", type='" + type + '\'' +
'}';
}
}
package com.mirrordust.gpscellinfodemo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
//用于获取GPS
private LocationManager locationManager;
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//若要实时获取GPS更新,可在此处理
Log.i(TAG, "Location changed...");
Log.i(TAG, "Latitude : " + location.getLatitude());
Log.i(TAG, "Longitude : " + location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
//用于获取基站信息
private TelephonyManager telephonyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 1000, 1, locationListener);//参数依次为provider(GPS,或者NETWORK_PROVIDER或PASSIVE_PROVIDER),执行更新的最小时间,执行更新的最小距离,更新后的listener
// locationManager.requestSingleUpdate(provider, locationListener, null);//或者仅仅进行单词更新
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
@Override
protected void onStop() {
super.onStop();
locationManager.removeUpdates(locationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showGps(View view) {
TextView tv = findViewById(R.id.gps_value);
if (locationManager != null) {
String provider = LocationManager.GPS_PROVIDER;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
tv.setText("无权限");
return;
}
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) {
Double lat = lastKnownLocation.getLatitude();
Double lon = lastKnownLocation.getLongitude();
Double alt = lastKnownLocation.getAltitude();
Float acc = lastKnownLocation.getAccuracy();
Float spd = lastKnownLocation.getSpeed();
tv.setText("Latitude: " + lat + ", Longitude: " + lon + ",\nAltitude: " + alt + ", Accuracy: " + acc + ", Speed: " + spd);
} else {
tv.setText("未获取到GPS");
}
}
}
public void showCellinfo(View view) {
TextView tv = findViewById(R.id.cell_value);
List<CellInfo> cellInfoList = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
tv.setText("无权限");
return;
}
cellInfoList = telephonyManager.getAllCellInfo();
if (cellInfoList == null) {
tv.setText("getAllCellInfo()返回null");
} else if (cellInfoList.size() == 0) {
tv.setText("基站列表为空");
} else {
int cellNumber = cellInfoList.size();
BaseStation main_BS = bindData(cellInfoList.get(0));
tv.setText("获取到" + cellNumber + "个基站, \n主基站信息:\n" + main_BS.toString());
for (CellInfo cellInfo : cellInfoList) {
BaseStation bs = bindData(cellInfo);
Log.i(TAG, bs.toString());
}
}
}
private BaseStation bindData(CellInfo cellInfo) {
BaseStation baseStation = null;
//基站有不同信号类型:2G,3G,4G
if (cellInfo instanceof CellInfoWcdma) {
//联通3G
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();
baseStation = new BaseStation();
baseStation.setType("WCDMA");
baseStation.setCid(cellIdentityWcdma.getCid());
baseStation.setLac(cellIdentityWcdma.getLac());
baseStation.setMcc(cellIdentityWcdma.getMcc());
baseStation.setMnc(cellIdentityWcdma.getMnc());
baseStation.setBsic_psc_pci(cellIdentityWcdma.getPsc());
if (cellInfoWcdma.getCellSignalStrength() != null) {
baseStation.setAsuLevel(cellInfoWcdma.getCellSignalStrength().getAsuLevel()); //Get the signal level as an asu value between 0..31, 99 is unknown Asu is calculated based on 3GPP RSRP.
baseStation.setSignalLevel(cellInfoWcdma.getCellSignalStrength().getLevel()); //Get signal level as an int from 0..4
baseStation.setDbm(cellInfoWcdma.getCellSignalStrength().getDbm()); //Get the signal strength as dBm
}
} else if (cellInfo instanceof CellInfoLte) {
//4G
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();
baseStation = new BaseStation();
baseStation.setType("LTE");
baseStation.setCid(cellIdentityLte.getCi());
baseStation.setMnc(cellIdentityLte.getMnc());
baseStation.setMcc(cellIdentityLte.getMcc());
baseStation.setLac(cellIdentityLte.getTac());
baseStation.setBsic_psc_pci(cellIdentityLte.getPci());
if (cellInfoLte.getCellSignalStrength() != null) {
baseStation.setAsuLevel(cellInfoLte.getCellSignalStrength().getAsuLevel());
baseStation.setSignalLevel(cellInfoLte.getCellSignalStrength().getLevel());
baseStation.setDbm(cellInfoLte.getCellSignalStrength().getDbm());
}
} else if (cellInfo instanceof CellInfoGsm) {
//2G
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
baseStation = new BaseStation();
baseStation.setType("GSM");
baseStation.setCid(cellIdentityGsm.getCid());
baseStation.setLac(cellIdentityGsm.getLac());
baseStation.setMcc(cellIdentityGsm.getMcc());
baseStation.setMnc(cellIdentityGsm.getMnc());
baseStation.setBsic_psc_pci(cellIdentityGsm.getPsc());
if (cellInfoGsm.getCellSignalStrength() != null) {
baseStation.setAsuLevel(cellInfoGsm.getCellSignalStrength().getAsuLevel());
baseStation.setSignalLevel(cellInfoGsm.getCellSignalStrength().getLevel());
baseStation.setDbm(cellInfoGsm.getCellSignalStrength().getDbm());
}
} else {
//电信2/3G
Log.e(TAG, "CDMA CellInfo................................................");
}
return baseStation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment