How to get information from Satellites via LocationServices

2

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

Here's an example: link

I've seen some examples where GpsStatus.Listener is used in 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 LocationServices ?

    
asked by anonymous 27.10.2016 / 18:46

2 answers

2

As far as I know not.

LocationServices replaces the Location Manager in several things but with respect to this information it continues to be obtained through it.

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 LocationManager

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

    locationManager.addGpsStatusListener(GpsStatusListener);
    
  • In the implementation of the listener get a GpsStatus object, through it get a Iterable<GpsSatellite> and iterate it to get each of the GpsSatellite containing the information regarding the satellite in question.

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

From api24 you should use the GnssStatus .

Example usage:

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);
                }
            }
        }
    }
}
    
27.10.2016 / 19:15
1

Just complementing the answer from @ramaral!

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());
    }
    
28.10.2016 / 17:34