I am bashing around with android localization and already figured out how to receive NMEA data. Unfortunately, the results seem to be very chaotic. I do only need the GPRMC sentence but get GPGGA, GPVTG etc. returned. Is there any way to control the onNmeaReceived() function?
public class TrackingService extends Service {
private Intent broadcastIntent = new Intent("com.example.locationlogger.TestBroadcastReceiver");
private LocationManager lm;
private LocationListener ll = new LocationListener(){
//sample listener...
};
GpsStatus.NmeaListener nl = new GpsStatus.NmeaListener() {
@Override
public void onNmeaReceived(long timestamp, String nmea) {
/*
* Broadcast a message..
*/
broadcastIntent.putExtra("TESTVAR", "Received some nmea strings: " + nmea);
sendBroadcast(broadcastIntent);
}
};
@Override
public void onCreate() {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE开发者_开发知识库);
}
@Override
public void onStart(Intent intent, int startId) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval * 1000, 0, ll);
lm.addNmeaListener(nl);
}
}
Thank you for your answers - all valid points. Actually, I solved it with a regex pattern and used matcher.find() to filter relevant sentences. In my case, this was the GPRMC sentence and only this if it included valid data. Here's the java regex string - maybe someone can re-use this.
public static Pattern p = Pattern.compile("(\\$GPRMC)\\,((\\d{6})\\.?\\d*)\\,([AV]{1})\\,(\\d*\\.?\\d*)\\,([NS]{1})\\,(\\d*\\.?\\d*)\\,([EW]{1})\\,([0-9]*\\.?[0-9]*)\\,([0-9]*\\.?[0-9]*)\\,([0-9]{6})\\,([0-9]*\\.?[0-9]*)\\,([EW]?)\\,([ADEMSN]{1}.*.[0-9A-F]{2})");
To see how I grouped the regex, use this regex string
(\$GPRMC)\,((\d{6})\.?\d*)\,([AV]{1})\,(\d*\.?\d*)\,([NS]{1})\,(\d*\.?\d*)\,([EW]{1})\,([0-9]*\.?[0-9]*)\,([0-9]*\.?[0-9]*)\,([0-9]{6})\,([0-9]*\.?[0-9]*)\,([EW]?)\,([ADEMSN]{1}.*.[0-9A-F]{2})
at this website
http://www.regexplanet.com/simple/index.html
with this NMEA sentence
$GPRMC,090248,A,4745.012742,N,01304.143827,E,0.0,0.0,310710,5.3,E,A*48
Just to make sure.. by "chaotic", you just mean that you get all the NMEA sentences? i.e. there's no odd behaviour by the system?
Because that behaviour seems to be as documented — you get all the NMEA data from the GPS.
Since NMEA is plain text, can't you just filter out the prefixes you don't want?
Receiving multiple GPGSV sentences in a row is to be expected and I think if you check they are not identical, but parts of a single message as all the info for satellites does not fit into a single NMEA sentence. There is a sentence sequence number and the number of total sentences that make up the message at the first two positions of the message.
GPRMC are just single sentences with each containing the 'recommended minimum' fix data.
加载中,请稍侯......
精彩评论