Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of satellites used in gps in android?

Tags:

android

gps

I have written like this. But i am getting 0 always.Please correct me where i am wrong.

public int getSatellites() { 

    GpsStatus gpsStatus = locationManager.getGpsStatus(null);
     int count=0;
     if(gpsStatus != null) {
         Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
         Iterator<GpsSatellite>sat = satellites.iterator();

         int i=0;

         while (sat.hasNext()) {
            count++;
             GpsSatellite satellite = sat.next();
             strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," 
             + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n";
             Log.v("value:"+"-", strGpsStats+= (i++) + ": " + satellite.getPrn() + "," + satellite.usedInFix() + "," 
                     + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()+ "\n\n");
         }
         //tv.setText(strGpsStats);
         Log.v("satellite", "satellite " +count);
     }

    return count;       
 }

ANd in the main activity class i have called it like this

  int satellites=gps.getSatellites();
like image 258
ShutterSoul Avatar asked Nov 10 '22 09:11

ShutterSoul


1 Answers

A couple of things that could cause you problems:

  • You have to have requested the ACCESS_COARSE_LOCATION permission.
  • Your device must have the GPS location service enabled.
  • The GPS satellites must be "visible".
  • The GPS hardware takes some period of time before acquiring the satellites. The usual way is to wait until the system tells you that something has changed by using a GpsStatus.Listener and waiting for the onGpsStatusChanged event.
like image 84
scottt Avatar answered Nov 15 '22 01:11

scottt