GPS/GNSS测试工具

先说一下GPS和GNSS的区别, 很多人把GPS等同于GNSS,我们在生活和工作中也常以GPS的概念描述GNSS。其实不然。

GNSS全球导航卫星系统(Global Navigation Satellite System)。泛指所有的卫星导航系统,包括全球的、区域的和增强的导航系统,如美国的GPS、俄罗斯的Glonass、欧洲的Galileo、中国的北斗卫星导航系统,以及相关的增强系统,如美国的WAAS(广域增强系统)、欧洲的EGNOS(欧洲静地导航重叠系统)和日本的MSAS(多功能运输卫星增强系统)等,还涵盖在建和以后要建设的其他卫星导航系统。国际GNSS系统是个多系统、多层面、多模式的复杂组合系统。

用简洁的公式表述就是:

GNSS = 卫星导航系统(GPS+Glonass+Galileo+Beidou等)+ 区域增强/扩增系统(WAAS+EGNOS+MSAS+QZSS+SBAS等)+ ...

最近犹豫工作需要,需要写一个测试GNSS搜星能力的工具。

1. Qcom平台默认提供“garden_app”命令可测试GNSS。该命令需要root权限才可运行。

2. server call 调用LocationManager相关接口进行搜星,同时打开相关log,查看log中搜星信息。该方法遇到传参问题。

3. 写一个GNSS测试apk,可界面显示搜星信息,也可将搜星信息写进文件,供其它工具读取。

调用的关键类及方法:

LocationManager.requestLocationUpdates()  //调用该接口发起搜星,需要定义接口所需参数,如LocationListener

GpsStatus, GpsStatus.Listener //通过该类获取卫星信息

GPS (1-32) / SBAS (33-54) / GLONASS (65-96) / QZSS (193-200) / BEIDOU (201-235) / GALILEO (301-) //android中定义的各个GNSS系统中卫星prn所属阈值。

代码如下:

package com.example.gpstest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import android.app.Activity;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

	protected static final String TAG = "GNSSTEST";
	private String GNSS_RESULT_FILE;	
	private static final long MIN_TIME_INTERVAL = 1 * 1000;
	private static final float MIN_DISTANCE_INTERVAL = 10;
	LocationManager mLocationManager = null;
	LocationListener mLocationListener = null;
	GpsStatus.Listener mListener= null;
	TextView gnss_result = null;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // /data/user/0/com.example.gpstest/files/gnss_result.txt, need root to cat
    	// GNSS_RESULT_FILE= this.getFilesDir().getPath() + "/gnss_result.txt";
        GNSS_RESULT_FILE= "/sdcard/gnss_result.txt";
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocationListener = new LocationListener() {
			
			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(Location arg0) {
				// TODO Auto-generated method stub
				
			}
		};
		mListener = new GpsStatus.Listener() {
			
			@Override
			public void onGpsStatusChanged(int event) {
				// TODO Auto-generated method stub
				switch (event) {
				case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
					// get current status
					GpsStatus gpsStatus = mLocationManager.getGpsStatus(null);
					// get MAX default number of states
					int maxStatellites = gpsStatus.getMaxSatellites();
					// get all states
					Iterator iters = gpsStatus.getSatellites().iterator();
					// calculate number of states
					int stateNumber = 0;
					String type = "UNKNOWN";
					StringBuilder mStringBuilder = new StringBuilder();
					while (iters.hasNext() && stateNumber <= maxStatellites) {
						stateNumber++;
						GpsSatellite nextStatellite = iters.next();
						//get signal to noise ratio
						float snr = ((GpsSatellite) nextStatellite).getSnr();
						int prn = nextStatellite.getPrn();
						
						// check the type of statellite, GPS/GLONASS/BEIDOU or SBAS/QZSS
						// GPS (1-32) / SBAS (33-54) / GLONASS (65-96) / QZSS (193-200) / BEIDOU (201-235) / GALILEO (301-)
						if (1 <= prn && prn <= 32){
							type = "GPS";
						} else if (64 < prn && prn <=96) {
							type = "GLONASS";
						} else if (200 < prn && prn <= 235) {
							type = "BEIDOU";
						} else if (33 <= prn && prn <= 54) {
							type = "SBAS";
						} else if (193 <= prn && prn <= 200) {
							type = "QZSS";
						} else if (301 <= prn) {
							type = "GALILEO";
						}
						
						mStringBuilder.append("NO.").append(stateNumber).append("\t").append("snr: ").append(snr)
							.append("\t").append("prn: ").append(prn).append("\t").append("type: ").append(type).append("\n");
					}
					
					gnss_result = (TextView) findViewById(R.id.text_view);
					gnss_result.setMovementMethod(ScrollingMovementMethod.getInstance());
					gnss_result.setText("total search statellite's number: "+ stateNumber + "\n" + mStringBuilder.toString());

					try {
					    File file = new File(GNSS_RESULT_FILE);
					    // if file doesn't exists, then create it
					    if (!file.exists()) {
					        file.createNewFile();
					    }

					    FileWriter fw = new FileWriter(file.getAbsoluteFile());
					    BufferedWriter bw = new BufferedWriter(fw);
					    bw.write("total search statellite's number: "+ stateNumber + "\n" + mStringBuilder.toString());
					    bw.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						Log.e(TAG, "IOException in writing gnss informance to file");
						e.printStackTrace();
					}
					break;
				default:
					break;
				}
				
			}
		};
		
		mLocationManager.addGpsStatusListener(mListener);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_INTERVAL, MIN_DISTANCE_INTERVAL, mLocationListener);
    }
}

 

你可能感兴趣的:(常用命令&工具,Android,gps)