开发者

Getting GPS strength in Android

In my app, I have to find out GPS strength. However, when I use the following code, I get 0 only for count no of satellites. So, I could not get GPS strength.

My code:

public void onCreate(Bundle savedInstanceState) {
  locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  locMgr.addGpsStatusListener(onGpsStatusChange) ;
  onGpsStatusChange.onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS);                     
 // Listener for GPS Status...
}

            final Listener onGpsStatusChange=new GpsStatus.Listener()
            {       
                    public void onGpsStatusChanged(int event)
                    {
                        Log.e("gps","in class");
                            switch(event)
                            {
                             case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                                  GpsStatus xGpsStatus =locMgr.getGpsStatus(null) ;

                                  Iterable<GpsSatellite> iSatellites               =xGpsStatus.getSatellites();
  开发者_运维问答                                Iterator<GpsSatellite> it = iSatellites.iterator();
                                   int count=0;
                                   while(it.hasNext()){
                                    count=count+1;
                                        GpsSatellite oSat = (GpsSatellite) it.next();
                                        

                                        Log.e("gps",""+oSat.getSnr());   
                                   }
                                 Log.e("count",""+count);
                                 tv1.setText(""+count);
                             break;
                            }
                    }
            };


You can get GPS signal to noise ratio of each sat from the NMEA sentances

 $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75

Where:

      GSV          Satellites in view
      2            Number of sentences for full data
      1            sentence 1 of 2
      08           Number of satellites in view

      01           Satellite PRN number
      40           Elevation, degrees
      083          Azimuth, degrees
      46           SNR - higher is better
           for up to 4 satellites per sentence
      *75          the checksum data, always begins with *

To listen to NMEA data you need to create a NMEA listener.


I found that the best way to obtain (and filter) the accuracy and fix location is to use the solution found at: http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestEstimate , and that's the BEST result I got so far.

Suppose you already have a good location and you make use of it (e.g. animateTo ..) then let's suppose you save that location in Location variable and you call it currentBestLocation.

What you should do is put the isBetterLocation() (from "Maintaining a current best estimate" I just posted in google site upward) method in your activity, and then when in your onLocationChanged() you should send the currentBestLocation and the location you just got from the onLocationChanged() to isBetterLocation() (e.g. from onLocationChanged() you write isBetterLocation(location, currentBestLocation).

in isBetterLocation() it will send you back true if the new location is better than the last location you have right now (the last location is currentBestLocation)

If it returns true, that means that the accuracy is good.. you should use that location (don't forget to update --> currentBestLocation = location )


private class MyGPSListener implements GpsStatus.Listener {
    public void onGpsStatusChanged(int event) {
        switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                if (mLastLocation != null)
                    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

                if (isGPSFix) { // A fix has been acquired.
                    // Do something.
                } else { // The fix has been lost.
                    // Do something.
                }

                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // Do something.
                isGPSFix = true;

                break;
        }
    }
}

OK, now in onLocationChanged() you should add the following:

@Override
public void onLocationChanged(Location location) {
    if (location == null) return;
    mLastLocationMillis = SystemClock.elapsedRealtime();
    // Do something.
    mLastLocation = location;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜