如何使用GnssMeasurement回调获取Android 7.0中每个检测到的GPS卫星的SNR?

时间:2018-01-11 06:02:04

标签: android gps

在下面显示的代码中,我可以使用 GnssStatus.callback 获取Android 7.0及更高版本API中可见卫星的数量。但我无法获得每个检测到的satelitte的相关SNR,因为 GnssStatus.callback 没有支持的功能。

我检查了this链接,发现我们应该使用 GnssMeasurement 回调并调用( getsnrindb())作为here

如何使用(GnssMeasurement回调)获取SNR值?

我的代码:

    tv1 = (TextView) findViewById(R.id.Gps_location);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mGnssStatusCallback = new GnssStatus.Callback() {
        // TODO: add your code here!
        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {

            satelliteCount = status.getSatelliteCount();
            sat_id = status.getSvid(1);
            constellationType = status.getConstellationType(1);
            v = " Scan: "+ (count)+  " ,Sat-count=" +(satelliteCount) +"id="+ sat_id +" type"+  constellationType + " ,Eacc= "+accuracy+ "\n\n";
            v += tv1.getText();
            tv1.setText(v);
            count= count + 1;
        }
    };


 @Override
protected void onStart() {
    super.onStart();
    mLocationManager.registerGnssStatusCallback(mGnssStatusCallback);
    mLocationManager.registerGnssMeasurementsCallback(mGnssMeasurmentscallbak);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
}

@Override
protected void onStop() {
    mLocationManager.removeUpdates(this);
    mLocationManager.unregisterGnssStatusCallback(
            mGnssStatusCallback
            );
    super.onStop();
}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    if (locationManager != null) {
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();  
            longitude = location.getLongitude();
            accuracy = location.getAccuracy();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

使用GnssStatus.Callback()到getSatelliteCount()来计算GPS信号强度,这将向我们提供可到达范围内的卫星数,并且每个卫星都有一个信噪比(getCn0DbHz(i) )。

// Android N(7.0)及更高版本和侦听器

    private GnssStatus.Callback mGnssStatusCallback ;

    mGnssStatusCallback = new GnssStatus.Callback() {

        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {

            int satelliteCount = status.getSatelliteCount();

            int usedSatellites = 0;
            float totalSnr = 0;
            for (int i = 0; i < satelliteCount; i++){
                if (status.usedInFix(i)) {
                    usedSatellites++;
                    totalSnr += status.getCn0DbHz(i); //this method obtains the signal from each satellite
                }
            }
            // we calculate the average of the power of the GPS signal
            float avgSnr = (usedSatellites > 0) ? totalSnr / usedSatellites: 0.0f;


            Log.d(TAG, "Number used satelites: " + usedSatellites + " SNR: " + totalSnr+"avg SNR: "+avgSnr);
        }
    };
    try {
        mLocationManager.registerGnssStatusCallback(mGnssStatusCallback);
    } catch (SecurityException e) {
        Log.e(TAG,"Error "+e);
    }