How to get information from Satellites through Locationservices

Asked

Viewed 132 times

2

There are app’s that show on how many satellites your GPS managed to communicate.

Follow an exmeplo: https://play.google.com/store/apps/details?id=com.chartcross.gpstest

I saw some examples where you use GpsStatus.Listener in a LocationManager.

But nowadays instead of LocationManager I’m using LocationServices :

 final LocationRequest request = LocationRequest.create();
 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);

Is there any way to get the information from the Satellite through the LocationServices ?

2 answers

2


Not that I know of.

The Locationservices replaces the Location Manager in several things but as far as that information is concerned it continues to be obtained through him.

For applications that need to run in versions prior to API24 use the classes Gpsstatus and Gpssatellite.

Basically these are the steps to take:

  • Get the Locationmanager

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
  • Register an implementation of Gpsstatus.Listener

    locationManager.addGpsStatusListener(GpsStatusListener);
    
  • In the implementation of Listener get an object Gpsstatus, by obtaining a Iterable<GpsSatellite> and carve it to get each of the Gpssatellite containing the information relating to the satellite in question.

    GpsStatus gpsStatus = locationManager.getGpsStatus(null);
    Iterable<GpsSatellite> satellites = gpsStatus.getSatellites();
    

From the api24 must use the class Gnssstatus.

Example of use:

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    GnssStatusCallback gnssStatusCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        gnssStatusCallback = new GnssStatusCallback(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_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.registerGnssStatusCallback(gnssStatusCallback);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.unregisterGnssStatusCallback(gnssStatusCallback);
    }

    private static class GnssStatusCallback extends GnssStatus.Callback{

        WeakReference<MainActivity> activity;
        public GnssStatusCallback(MainActivity activity){

            this.activity = new WeakReference<>(activity);
        }
        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {
            super.onSatelliteStatusChanged(status);

            //número de satélites
            int satelliteCount = status.getSatelliteCount();

            MainActivity activity = this.activity.get();
            if(activity != null) {

                Toast.makeText(activity,
                               "Satellite count = " + satelliteCount,
                               Toast.LENGTH_SHORT)
                     .show();

                for(int i = 0; i < satelliteCount; i++){

                    //Obtenha informações especificas de cada satélite usando
                    //status.nomeMetodo(i);
                }
            }
        }
    }
}

1

Just complementing the @ramaral response !

Implementation of GpsStatus.Listener:

   class GPSStatusCallBack implements GpsStatus.Listener {

        @Override
        public void onGpsStatusChanged(int event) {
            if (ActivityCompat.checkSelfPermission(Servico.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            GpsStatus gpsStatus = locationManager.getGpsStatus(null);
            Iterator<GpsSatellite> satellites = gpsStatus.getSatellites().iterator();
            while (satellites.hasNext()){
                final GpsSatellite satellite = satellites.next();
                final StringBuilder buff = new StringBuilder("");
                buff.append("\nAzimuth: "+satellite.getAzimuth());
                buff.append("\nElevation: "+satellite.getElevation());
                buff.append("\nPRN: "+satellite.getPrn());
                buff.append("\nSNR: "+satellite.getSnr());
                Log.w(TAG, buff.toString());

            }

        }
    }

Implementation of GnssStatus.Callback :

@RequiresApi(api = Build.VERSION_CODES.N)
class GnssStatusCallback extends GnssStatus.Callback{

    @Override
    public void onSatelliteStatusChanged(GnssStatus status) {

        int count = status.getSatelliteCount();

        Log.w(TAG, "Quantidade de satelites: "+count);
        for(int i = 0; i<count; i++){
            final StringBuilder buff = new StringBuilder("");
            buff.append("\nAzimuthDegrees: "+status.getAzimuthDegrees(i));
            buff.append("\nConstellationType: "+status.getConstellationType(i));
            buff.append("\nCn0DbHz: "+status.getCn0DbHz(i));
            buff.append("\nElevationDegrees: "+status.getElevationDegrees(i));
            buff.append("\nSvid: "+status.getSvid(i));

            Log.w(TAG, buff.toString());
        }

    }

Called as version:

 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locationManager.registerGnssStatusCallback(new GnssStatusCallback());
    }else{
        locationManager.addGpsStatusListener(new GPSStatusCallBack());
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.