|
|
|
![]() :: Research Group Embedded Interaction :: Media Informatics :: Ludwig-Maximilians-University Munich Simple J2ME NMEA parserThis documents describes the implementation of simple NMEA-0183 parser for mobile devices on a J2ME base. NMEA-0183 is a protocol that is widely supported by GPS receivers. This example includes methods to access a GPS receiver via bluetooth using a serial connection (see accessing GPS receiver from mobile phone via bluetooth). Most of the work done by the parser consists of determining the record type and separating the different items. The parsed data are stored as Strings. The parser recognizes if the data coming from the GPS receiver is valid. Used Software Used HardwareSource code The source code includes a thread-based implementation of the communication between mobile device and GPS receiver and the parser itself consisting of Tokenizer.java and Parser.java (download source code). There is a test midlet in Starter.java which displays some GPS information.Description Tokenizer The tokenizer splits a single record into its components using a delimiter character (here: ","). It provides therefore the common methods next (which will return the next token as a String) and hasNext (which will return true if there are more tokens to come). The implementation is very simple, using standard String methods such as indexOf or substring. Here is a short code snippet taken from the next method:
String next = s.substring(currentPosition, nextPosition);
currentPosition = nextPosition + 1;
nextPosition = s.indexOf(DELIMITER, currentPosition);
Parser The parse provides one single (and static) method: public static int parse(String s, Record record) that throws an exception if the record type is not supported. String s is a line coming directly from the GPS receiver. Record is a class used as datatype consisting of different Strings to store the GPS data. The first step is to determine which record type is used (by using the first token as identifier). In this implementation GPRMC and GPGGA is being recognized (but not all data contained in these records are actually used). The following procedure is very simple again: A tokenizer is used to separate the different items which are then stored in the record (time and date are converted in a human readably format). If several record types are necessary for an application the invoking object has to take care of that (as shown in GPS.java).
String token = tokenizer.next();
if (token.equals("$GPRMC")) {
type = TYPE_GPRMC;
token = tokenizer.next();
record.dateTimeOfFix = token.substring(0, 2) + ":"
+ token.substring(2, 4) + ":" + token.substring(4, 6);
record.warning = tokenizer.next().equals(Record.WARNING);
record.lattitude = tokenizer.next();
record.lattitudeDirection = tokenizer.next();
record.longitude = tokenizer.next();
// [...]
}
Conclusion If there is no further processing of the data necessary the implementation of a NMEA-0183 parser is very straight forward and easily possible on a mobile device. References If you have any comments or questions, please feel free to ask us! |