├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── maven-push.gradle ├── settings.gradle └── src ├── main ├── AndroidManifest.xml └── java │ └── com │ └── github │ └── petr_s │ └── nmea │ ├── GpsSatellite.java │ ├── LocationFactory.java │ ├── NMEAAdapter.java │ ├── NMEAHandler.java │ ├── NMEAParser.java │ └── basic │ ├── BasicNMEAAdapter.java │ ├── BasicNMEAHandler.java │ └── BasicNMEAParser.java └── test └── java └── com └── github └── petr_s └── nmea ├── Helper.java ├── NMEAParserTest.java └── basic └── BasicNMEAParserTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | /out 2 | .gradle 3 | .idea 4 | /build 5 | *.iml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - tools 6 | - platform-tools 7 | - build-tools-23.0.2 8 | - android-23 9 | 10 | jdk: 11 | - oraclejdk8 12 | - oraclejdk7 13 | - openjdk7 14 | 15 | before_install: 16 | - chmod +x gradlew 17 | 18 | script: 19 | - ./gradlew testDebugUnitTest jacocoTestDebugUnitTestReport coveralls -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Petr Skramovsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-nmea-parser [![Build Status](https://travis-ci.org/petr-s/android-nmea-parser.svg)](https://travis-ci.org/petr-s/android-nmea-parser) [![Coverage Status](https://coveralls.io/repos/petr-s/android-nmea-parser/badge.svg?branch=master&service=github)](https://coveralls.io/github/petr-s/android-nmea-parser?branch=master) 2 | 3 | Light-weight Android Java library for NMEA sentences parsing 4 | ## Supported sentences: 5 | * GPRMC 6 | * GPGGA 7 | * GPGSV 8 | * GPGSA 9 | 10 | ## NMEA Parser 11 | flow parser build on top of the [BasicNMEAParser](src/main/java/com/github/petr_s/nmea/basic/BasicNMEAParser.java) 12 | that maps raw NMEA data to useful Android objects such as [Location](https://developer.android.com/reference/android/location/Location.html) and [GpsSatellite](https://developer.android.com/reference/android/location/GpsSatellite.html) 13 | 14 | ### Location parsing 15 | To get an Android Location object you have to parse both RMC and GGA with the same time. 16 | ```java 17 | NMEAHandler handler = new NMEAHandler() { 18 | ... 19 | @Override 20 | public void onLocation(Location location) { 21 | 22 | } 23 | ... 24 | }; 25 | NMEAParser parser = new NMEAParser(handler); 26 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 27 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 28 | ``` 29 | 30 | ### Satellites parsing 31 | To get a list of gps satellites you have to parse all of GSVs and at least one GSA sentence. 32 | Since [Android GpsSatellite class](https://developer.android.com/reference/android/location/GpsSatellite.html) is inaccessible (only trough reflection), 33 | the package level [GpsSatellite](src/main/java/com/github/petr_s/nmea/GpsSatellite.java) is introduced. 34 | ```java 35 | NMEAHandler handler = new NMEAHandler() { 36 | ... 37 | @Override 38 | public void onSatellites(List satellites) { 39 | 40 | } 41 | ... 42 | }; 43 | NMEAParser parser = new NMEAParser(handler); 44 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 45 | parser.parse("$GPGSV,3,2,11,12,23,110,34,26,18,295,29,21,17,190,30,05,11,092,25*72"); 46 | parser.parse("$GPGSV,3,3,11,14,02,232,13,23,02,346,12,20,01,135,13*48"); 47 | parser.parse("$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"); 48 | ``` 49 | 50 | if you don't need all methods there's also an [Adapter](src/main/java/com/github/petr_s/nmea/NMEAAdapter.java) 51 | 52 | ## Basic NMEA Parser 53 | flow parser that allows you to access raw NMEA data 54 | 55 | ```java 56 | BasicNMEAHandler handler = new BasicNMEAHandler() { 57 | ... 58 | @Override 59 | public void onRMC(long date, long time, double latitude, double longitude, float speed, float direction) { 60 | } 61 | ... 62 | }; 63 | BasicNMEAParser parser = new BasicNMEAParser(handler); 64 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 65 | ``` 66 | if you don't need all methods there's also an [Adapter](src/main/java/com/github/petr_s/nmea/basic/BasicNMEAAdapter.java) 67 | 68 | ## Gradle 69 | ``` 70 | repositories { 71 | mavenCentral() 72 | } 73 | 74 | dependencies { 75 | compile 'com.github.petr-s:android-nmea-parser:0.5.0' 76 | } 77 | ``` 78 | 79 | ## Maven 80 | ``` 81 | 82 | com.github.petr-s 83 | android-nmea-parser 84 | 0.5.0 85 | aar 86 | 87 | ``` 88 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | jcenter() 5 | } 6 | 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.0.0' 9 | classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1' 10 | classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.4.0' 11 | } 12 | } 13 | 14 | apply plugin: 'com.android.library' 15 | apply plugin: 'jacoco-android' 16 | apply plugin: 'com.github.kt3k.coveralls' 17 | apply from: 'maven-push.gradle' 18 | 19 | 20 | android { 21 | compileSdkVersion 23 22 | buildToolsVersion '23.0.2' 23 | 24 | defaultConfig { 25 | minSdkVersion 15 26 | } 27 | 28 | buildTypes { 29 | debug { 30 | testCoverageEnabled true 31 | } 32 | } 33 | } 34 | 35 | repositories { 36 | mavenCentral() 37 | } 38 | 39 | dependencies { 40 | compile 'com.google.android:android:4.1.1.4' 41 | testCompile 'junit:junit:4.12' 42 | testCompile "org.mockito:mockito-core:1.9.5" 43 | } 44 | 45 | coveralls { 46 | jacocoReportPath = "${buildDir}/reports/jacoco/jacocoTestDebugUnitTestReport/jacocoTestDebugUnitTestReport.xml" 47 | } 48 | 49 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | VERSION_NAME=0.5.0 2 | VERSION_CODE=5 3 | GROUP=com.github.petr-s 4 | POM_NAME=android-nmea-parser 5 | POM_ARTIFACT_ID=android-nmea-parser 6 | POM_PACKAGING=aar 7 | POM_DESCRIPTION=Android NMEA parser 8 | POM_URL=https://github.com/petr-s/android-nmea-parser 9 | POM_SCM_URL=https://github.com/petr-s/android-nmea-parser 10 | POM_SCM_CONNECTION=scm:git@github.com:petr-s/android-nmea-parser.git 11 | POM_SCM_DEV_CONNECTION=scm:git@github.com:petr-s/android-nmea-parser.git 12 | POM_LICENCE_NAME=The MIT License (MIT) 13 | POM_LICENCE_URL=https://opensource.org/licenses/MIT 14 | POM_LICENCE_DIST=repo 15 | POM_DEVELOPER_ID=petr-s 16 | POM_DEVELOPER_NAME=Petr Skramovsky -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petr-s/android-nmea-parser/1abfb41cada7a37e2e296782ffa78d6036ef927d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 08 18:35:32 CEST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /maven-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'maven' 18 | apply plugin: 'signing' 19 | 20 | def isReleaseBuild() { 21 | return VERSION_NAME.contains("SNAPSHOT") == false 22 | } 23 | 24 | def getReleaseRepositoryUrl() { 25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL 26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 27 | } 28 | 29 | def getSnapshotRepositoryUrl() { 30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL 31 | : "https://oss.sonatype.org/content/repositories/snapshots/" 32 | } 33 | 34 | def getRepositoryUsername() { 35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 36 | } 37 | 38 | def getRepositoryPassword() { 39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 40 | } 41 | 42 | afterEvaluate { project -> 43 | uploadArchives { 44 | repositories { 45 | mavenDeployer { 46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 47 | 48 | pom.groupId = GROUP 49 | pom.artifactId = POM_ARTIFACT_ID 50 | pom.version = VERSION_NAME 51 | 52 | repository(url: getReleaseRepositoryUrl()) { 53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 54 | } 55 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 57 | } 58 | 59 | pom.project { 60 | name POM_NAME 61 | packaging POM_PACKAGING 62 | description POM_DESCRIPTION 63 | url POM_URL 64 | 65 | scm { 66 | url POM_SCM_URL 67 | connection POM_SCM_CONNECTION 68 | developerConnection POM_SCM_DEV_CONNECTION 69 | } 70 | 71 | licenses { 72 | license { 73 | name POM_LICENCE_NAME 74 | url POM_LICENCE_URL 75 | distribution POM_LICENCE_DIST 76 | } 77 | } 78 | 79 | developers { 80 | developer { 81 | id POM_DEVELOPER_ID 82 | name POM_DEVELOPER_NAME 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | 90 | signing { 91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 92 | sign configurations.archives 93 | } 94 | 95 | task androidSourcesJar(type: Jar) { 96 | classifier = 'sources' 97 | from android.sourceSets.main.java.sourceFiles 98 | } 99 | 100 | artifacts { 101 | archives androidSourcesJar 102 | } 103 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'android-nmea-parser' -------------------------------------------------------------------------------- /src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/GpsSatellite.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | 4 | public class GpsSatellite { 5 | boolean mHasEphemeris; 6 | boolean mHasAlmanac; 7 | boolean mUsedInFix; 8 | int mPrn; 9 | float mSnr; 10 | float mElevation; 11 | float mAzimuth; 12 | 13 | public GpsSatellite(int prn) { 14 | mPrn = prn; 15 | } 16 | 17 | public void setHasEphemeris(boolean hasEphemeris) { 18 | this.mHasEphemeris = hasEphemeris; 19 | } 20 | 21 | public void setHasAlmanac(boolean hasAlmanac) { 22 | this.mHasAlmanac = hasAlmanac; 23 | } 24 | 25 | public void setUsedInFix(boolean usedInFix) { 26 | this.mUsedInFix = usedInFix; 27 | } 28 | 29 | /** 30 | * Returns the PRN (pseudo-random number) for the satellite. 31 | * 32 | * @return PRN number 33 | */ 34 | public int getPrn() { 35 | return mPrn; 36 | } 37 | 38 | /** 39 | * Returns the signal to noise ratio for the satellite. 40 | * 41 | * @return the signal to noise ratio 42 | */ 43 | public float getSnr() { 44 | return mSnr; 45 | } 46 | 47 | public void setSnr(float snr) { 48 | this.mSnr = snr; 49 | } 50 | 51 | /** 52 | * Returns the elevation of the satellite in degrees. 53 | * The elevation can vary between 0 and 90. 54 | * 55 | * @return the elevation in degrees 56 | */ 57 | public float getElevation() { 58 | return mElevation; 59 | } 60 | 61 | public void setElevation(float elevation) { 62 | this.mElevation = elevation; 63 | } 64 | 65 | /** 66 | * Returns the azimuth of the satellite in degrees. 67 | * The azimuth can vary between 0 and 360. 68 | * 69 | * @return the azimuth in degrees 70 | */ 71 | public float getAzimuth() { 72 | return mAzimuth; 73 | } 74 | 75 | public void setAzimuth(float azimuth) { 76 | this.mAzimuth = azimuth; 77 | } 78 | 79 | /** 80 | * Returns true if the GPS engine has ephemeris data for the satellite. 81 | * 82 | * @return true if the satellite has ephemeris data 83 | */ 84 | public boolean hasEphemeris() { 85 | return mHasEphemeris; 86 | } 87 | 88 | /** 89 | * Returns true if the GPS engine has almanac data for the satellite. 90 | * 91 | * @return true if the satellite has almanac data 92 | */ 93 | public boolean hasAlmanac() { 94 | return mHasAlmanac; 95 | } 96 | 97 | /** 98 | * Returns true if the satellite was used by the GPS engine when 99 | * calculating the most recent GPS fix. 100 | * 101 | * @return true if the satellite was used to compute the most recent fix. 102 | */ 103 | public boolean usedInFix() { 104 | return mUsedInFix; 105 | } 106 | 107 | @Override 108 | public boolean equals(Object o) { 109 | if (this == o) return true; 110 | if (o == null || getClass() != o.getClass()) return false; 111 | 112 | GpsSatellite satellite = (GpsSatellite) o; 113 | 114 | if (mHasEphemeris != satellite.mHasEphemeris) return false; 115 | if (mHasAlmanac != satellite.mHasAlmanac) return false; 116 | if (mUsedInFix != satellite.mUsedInFix) return false; 117 | if (mPrn != satellite.mPrn) return false; 118 | if (Float.compare(satellite.mSnr, mSnr) != 0) return false; 119 | if (Float.compare(satellite.mElevation, mElevation) != 0) return false; 120 | return Float.compare(satellite.mAzimuth, mAzimuth) == 0; 121 | 122 | } 123 | 124 | @Override 125 | public int hashCode() { 126 | int result = (mHasEphemeris ? 1 : 0); 127 | result = 31 * result + (mHasAlmanac ? 1 : 0); 128 | result = 31 * result + (mUsedInFix ? 1 : 0); 129 | result = 31 * result + mPrn; 130 | result = 31 * result + (mSnr != +0.0f ? Float.floatToIntBits(mSnr) : 0); 131 | result = 31 * result + (mElevation != +0.0f ? Float.floatToIntBits(mElevation) : 0); 132 | result = 31 * result + (mAzimuth != +0.0f ? Float.floatToIntBits(mAzimuth) : 0); 133 | return result; 134 | } 135 | 136 | @Override 137 | public String toString() { 138 | return "GpsSatellite{" + 139 | "mHasEphemeris=" + mHasEphemeris + 140 | ", mHasAlmanac=" + mHasAlmanac + 141 | ", mUsedInFix=" + mUsedInFix + 142 | ", mPrn=" + mPrn + 143 | ", mSnr=" + mSnr + 144 | ", mElevation=" + mElevation + 145 | ", mAzimuth=" + mAzimuth + 146 | '}'; 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/LocationFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import android.location.Location; 4 | 5 | public abstract class LocationFactory { 6 | public abstract Location newLocation(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/NMEAAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import android.location.Location; 4 | 5 | import java.util.List; 6 | 7 | public class NMEAAdapter implements NMEAHandler { 8 | @Override 9 | public void onStart() { 10 | 11 | } 12 | 13 | @Override 14 | public void onLocation(Location location) { 15 | 16 | } 17 | 18 | @Override 19 | public void onSatellites(List satellites) { 20 | 21 | } 22 | 23 | @Override 24 | public void onUnrecognized(String sentence) { 25 | 26 | } 27 | 28 | @Override 29 | public void onBadChecksum(int expected, int actual) { 30 | 31 | } 32 | 33 | @Override 34 | public void onException(Exception e) { 35 | 36 | } 37 | 38 | @Override 39 | public void onFinish() { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/NMEAHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import android.location.Location; 4 | 5 | import java.util.List; 6 | 7 | public interface NMEAHandler { 8 | void onStart(); 9 | 10 | void onLocation(Location location); 11 | 12 | void onSatellites(List satellites); 13 | 14 | void onUnrecognized(String sentence); 15 | 16 | void onBadChecksum(int expected, int actual); 17 | 18 | void onException(Exception e); 19 | 20 | void onFinish(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/NMEAParser.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import android.location.Location; 4 | import com.github.petr_s.nmea.basic.BasicNMEAHandler; 5 | import com.github.petr_s.nmea.basic.BasicNMEAParser; 6 | 7 | import java.util.Arrays; 8 | import java.util.Set; 9 | 10 | public class NMEAParser implements BasicNMEAHandler { 11 | public static final String LOCATION_PROVIDER_NAME = "nmea-parser"; 12 | private static final int FLAG_RMC = 1; 13 | private static final int FLAG_GGA = 2; 14 | private static final int LOCATION_FLAGS = FLAG_RMC | FLAG_GGA; 15 | private static final int SATELLITES_COUNT = 24; 16 | private final NMEAHandler handler; 17 | private final BasicNMEAParser basicParser; 18 | private final LocationFactory locationFactory; 19 | private Location location; 20 | private long lastTime; 21 | private int flags; 22 | private int satellitesCount; 23 | private GpsSatellite[] tempSatellites = new GpsSatellite[SATELLITES_COUNT]; 24 | private Set activeSatellites; 25 | 26 | public NMEAParser(NMEAHandler handler) { 27 | this(handler, new LocationFactory() { 28 | @Override 29 | public Location newLocation() { 30 | return new Location(LOCATION_PROVIDER_NAME); 31 | } 32 | }); 33 | } 34 | 35 | public NMEAParser(NMEAHandler handler, LocationFactory locationFactory) { 36 | this.handler = handler; 37 | this.locationFactory = locationFactory; 38 | basicParser = new BasicNMEAParser(this); 39 | 40 | if (handler == null) { 41 | throw new NullPointerException(); 42 | } 43 | } 44 | 45 | public synchronized void parse(String sentence) { 46 | basicParser.parse(sentence); 47 | } 48 | 49 | private void resetLocationState() { 50 | flags = 0; 51 | lastTime = 0; 52 | } 53 | 54 | private void newLocation(long time) { 55 | if (location == null || time != lastTime) { 56 | location = locationFactory.newLocation(); 57 | resetLocationState(); 58 | } 59 | } 60 | 61 | private void yieldLocation(long time, int flag) { 62 | if ((flags | flag & LOCATION_FLAGS) == LOCATION_FLAGS) { 63 | handler.onLocation(location); 64 | resetLocationState(); 65 | } else { 66 | flags |= flag; 67 | lastTime = time; 68 | } 69 | } 70 | 71 | private boolean hasAllSatellites() { 72 | for (int i = 0; i < satellitesCount; i++) { 73 | if (tempSatellites[i] == null) { 74 | return false; 75 | } 76 | } 77 | 78 | return true; 79 | } 80 | 81 | private void yieldSatellites() { 82 | if (satellitesCount > 0 && hasAllSatellites() && activeSatellites != null) { 83 | for (GpsSatellite satellite : tempSatellites) { 84 | if (satellite == null) { 85 | break; 86 | } else { 87 | satellite.setUsedInFix(activeSatellites.contains(satellite.getPrn())); 88 | satellite.setHasAlmanac(true); // TODO: ... 89 | satellite.setHasEphemeris(true); // TODO: ... 90 | } 91 | } 92 | 93 | handler.onSatellites(Arrays.asList(Arrays.copyOf(tempSatellites, satellitesCount))); 94 | 95 | Arrays.fill(tempSatellites, null); 96 | activeSatellites = null; 97 | satellitesCount = 0; 98 | } 99 | } 100 | 101 | private void newSatellite(int index, int count, int prn, float elevation, float azimuth, int snr) { 102 | if (count != satellitesCount) { 103 | satellitesCount = count; 104 | } 105 | 106 | GpsSatellite satellite = new GpsSatellite(prn); 107 | satellite.setAzimuth(azimuth); 108 | satellite.setElevation(elevation); 109 | satellite.setSnr(snr); 110 | 111 | tempSatellites[index] = satellite; 112 | } 113 | 114 | @Override 115 | public synchronized void onStart() { 116 | handler.onStart(); 117 | } 118 | 119 | @Override 120 | public synchronized void onRMC(long date, long time, double latitude, double longitude, float speed, float direction) { 121 | newLocation(time); 122 | 123 | location.setTime(date | time); 124 | location.setSpeed(speed); 125 | location.setBearing(direction); 126 | 127 | yieldLocation(time, FLAG_RMC); 128 | } 129 | 130 | @Override 131 | public synchronized void onGGA(long time, double latitude, double longitude, float altitude, FixQuality quality, int satellites, float hdop) { 132 | newLocation(time); 133 | 134 | location.setLatitude(latitude); 135 | location.setLongitude(longitude); 136 | location.setAltitude(altitude); 137 | location.setAccuracy(hdop * 4.0f); 138 | 139 | yieldLocation(time, FLAG_GGA); 140 | } 141 | 142 | @Override 143 | public synchronized void onGSV(int satellites, int index, int prn, float elevation, float azimuth, int snr) { 144 | newSatellite(index, satellites, prn, elevation, azimuth, snr); 145 | 146 | yieldSatellites(); 147 | } 148 | 149 | @Override 150 | public void onGSA(FixType type, Set prns, float pdop, float hdop, float vdop) { 151 | activeSatellites = prns; 152 | 153 | yieldSatellites(); 154 | } 155 | 156 | @Override 157 | public synchronized void onUnrecognized(String sentence) { 158 | handler.onUnrecognized(sentence); 159 | } 160 | 161 | @Override 162 | public synchronized void onBadChecksum(int expected, int actual) { 163 | handler.onBadChecksum(expected, actual); 164 | } 165 | 166 | @Override 167 | public synchronized void onException(Exception e) { 168 | handler.onException(e); 169 | } 170 | 171 | @Override 172 | public synchronized void onFinished() { 173 | handler.onFinish(); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/basic/BasicNMEAAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea.basic; 2 | 3 | import java.util.Set; 4 | 5 | public class BasicNMEAAdapter implements BasicNMEAHandler { 6 | @Override 7 | public void onStart() { 8 | 9 | } 10 | 11 | @Override 12 | public void onRMC(long date, long time, double latitude, double longitude, float speed, float direction) { 13 | 14 | } 15 | 16 | @Override 17 | public void onGGA(long time, double latitude, double longitude, float altitude, FixQuality quality, int satellites, float hdop) { 18 | 19 | } 20 | 21 | @Override 22 | public void onGSV(int satellites, int index, int prn, float elevation, float azimuth, int snr) { 23 | 24 | } 25 | 26 | @Override 27 | public void onGSA(FixType type, Set prns, float pdop, float hdop, float vdop) { 28 | 29 | } 30 | 31 | @Override 32 | public void onUnrecognized(String sentence) { 33 | 34 | } 35 | 36 | @Override 37 | public void onBadChecksum(int expected, int actual) { 38 | 39 | } 40 | 41 | @Override 42 | public void onException(Exception e) { 43 | 44 | } 45 | 46 | @Override 47 | public void onFinished() { 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/basic/BasicNMEAHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea.basic; 2 | 3 | import java.util.Set; 4 | 5 | public interface BasicNMEAHandler { 6 | void onStart(); 7 | 8 | /*** 9 | * Called on GPRMC parsed. 10 | * 11 | * @param date milliseconds since midnight, January 1, 1970 UTC. 12 | * @param time actual UTC time (without date) 13 | * @param latitude angular y position on the Earth. 14 | * @param longitude angular x position on the Earth. 15 | * @param speed in meters per second. 16 | * @param direction angular bearing value to the North. 17 | */ 18 | void onRMC(long date, long time, double latitude, double longitude, float speed, float direction); 19 | 20 | /*** 21 | * Called on GPGGA parsed. 22 | * 23 | * @param time actual UTC time (without date) 24 | * @param latitude angular y position on the Earth. 25 | * @param longitude angular x position on the Earth. 26 | * @param altitude altitude in meters above corrected geoid 27 | * @param quality fix-quality type {@link FixQuality} 28 | * @param satellites actual number of satellites 29 | * @param hdop horizontal dilution of precision 30 | */ 31 | void onGGA(long time, double latitude, double longitude, float altitude, FixQuality quality, int satellites, float hdop); 32 | 33 | /*** 34 | * Called on GPGSV parsed. 35 | * Note that single nmea sentence contains up to 4 satellites therefore you can receive 4 calls per sentence. 36 | * 37 | * @param satellites total number of satellites 38 | * @param index index of satellite 39 | * @param prn pseudo-random noise number 40 | * @param elevation elevation in degrees 41 | * @param azimuth azimuth in degrees 42 | * @param snr signal to noise ratio 43 | */ 44 | void onGSV(int satellites, int index, int prn, float elevation, float azimuth, int snr); 45 | 46 | /*** 47 | * Called on GPGSA parsed. 48 | * 49 | * @param type type of fix 50 | * @param prns set of satellites used for the current fix 51 | * @param pdop position dilution of precision 52 | * @param hdop horizontal dilution of precision 53 | * @param vdop vertical dilution of precision 54 | */ 55 | void onGSA(FixType type, Set prns, float pdop, float hdop, float vdop); 56 | 57 | void onUnrecognized(String sentence); 58 | 59 | void onBadChecksum(int expected, int actual); 60 | 61 | void onException(Exception e); 62 | 63 | void onFinished(); 64 | 65 | enum FixQuality { 66 | Invalid(0), 67 | GPS(1), 68 | DGPS(2), 69 | PPS(3), 70 | IRTK(4), 71 | FRTK(5), 72 | Estimated(6), 73 | Manual(7), 74 | Simulation(8); 75 | 76 | public final int value; 77 | 78 | FixQuality(int value) { 79 | this.value = value; 80 | } 81 | } 82 | 83 | enum FixType { 84 | Invalid(0), 85 | None(1), 86 | Fix2D(2), 87 | Fix3D(3); 88 | 89 | public final int value; 90 | 91 | FixType(int value) { 92 | this.value = value; 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/petr_s/nmea/basic/BasicNMEAParser.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea.basic; 2 | 3 | import com.github.petr_s.nmea.basic.BasicNMEAHandler.FixQuality; 4 | 5 | import java.io.UnsupportedEncodingException; 6 | import java.text.SimpleDateFormat; 7 | import java.util.*; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | import static com.github.petr_s.nmea.basic.BasicNMEAHandler.FixType; 12 | 13 | public class BasicNMEAParser { 14 | private static final float KNOTS2MPS = 0.514444f; 15 | private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss", Locale.US); 16 | private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("ddMMyy", Locale.US); 17 | private static final String COMMA = ","; 18 | private static final String CAP_FLOAT = "(\\d*[.]?\\d+)"; 19 | private static final String HEX_INT = "[0-9a-fA-F]"; 20 | private static final Pattern GENERAL_SENTENCE = Pattern.compile("^\\$(\\w{5}),(.*)[*](" + HEX_INT + "{2})$"); 21 | private static final Pattern GPRMC = Pattern.compile("(\\d{5})?" + 22 | "(\\d[.]?\\d*)?" + COMMA + 23 | regexify(Status.class) + COMMA + 24 | "(\\d{2})(\\d{2}[.]\\d+)?" + COMMA + 25 | regexify(VDir.class) + "?" + COMMA + 26 | "(\\d{3})(\\d{2}[.]\\d+)?" + COMMA + 27 | regexify(HDir.class) + "?" + COMMA + 28 | CAP_FLOAT + "?" + COMMA + 29 | CAP_FLOAT + "?" + COMMA + 30 | "(\\d{6})?" + COMMA + 31 | CAP_FLOAT + "?" + COMMA + 32 | regexify(HDir.class) + "?" + COMMA + "?" + 33 | regexify(FFA.class) + "?"); 34 | private static final Pattern GPGGA = Pattern.compile("(\\d{5})?" + 35 | "(\\d[.]?\\d*)?" + COMMA + 36 | "(\\d{2})(\\d{2}[.]\\d+)?" + COMMA + 37 | regexify(VDir.class) + "?" + COMMA + 38 | "(\\d{3})(\\d{2}[.]\\d+)?" + COMMA + 39 | regexify(HDir.class) + "?" + COMMA + 40 | "(\\d)?" + COMMA + 41 | "(\\d{2})?" + COMMA + 42 | CAP_FLOAT + "?" + COMMA + 43 | CAP_FLOAT + "?,[M]" + COMMA + 44 | CAP_FLOAT + "?,[M]" + COMMA + 45 | CAP_FLOAT + "?" + COMMA + 46 | "(\\d{4})?"); 47 | private static final Pattern GPGSV = Pattern.compile("(\\d+)" + COMMA + 48 | "(\\d+)" + COMMA + 49 | "(\\d{2})" + COMMA + 50 | 51 | "(\\d{2})" + COMMA + 52 | "(\\d{2})" + COMMA + 53 | "(\\d{3})" + COMMA + 54 | "(\\d{2})" + COMMA + 55 | 56 | "(\\d{2})?" + COMMA + "?" + 57 | "(\\d{2})?" + COMMA + "?" + 58 | "(\\d{3})?" + COMMA + "?" + 59 | "(\\d{2})?" + COMMA + "?" + 60 | 61 | "(\\d{2})?" + COMMA + "?" + 62 | "(\\d{2})?" + COMMA + "?" + 63 | "(\\d{3})?" + COMMA + "?" + 64 | "(\\d{2})?" + COMMA + "?" + 65 | 66 | "(\\d{2})?" + COMMA + "?" + 67 | "(\\d{2})?" + COMMA + "?" + 68 | "(\\d{3})?" + COMMA + "?" + 69 | "(\\d{2})?"); 70 | private static final Pattern GPGSA = Pattern.compile(regexify(Mode.class) + COMMA + 71 | "(\\d)" + COMMA + 72 | 73 | "(\\d{2})?" + COMMA + 74 | "(\\d{2})?" + COMMA + 75 | "(\\d{2})?" + COMMA + 76 | "(\\d{2})?" + COMMA + 77 | "(\\d{2})?" + COMMA + 78 | "(\\d{2})?" + COMMA + 79 | "(\\d{2})?" + COMMA + 80 | "(\\d{2})?" + COMMA + 81 | "(\\d{2})?" + COMMA + 82 | "(\\d{2})?" + COMMA + 83 | "(\\d{2})?" + COMMA + 84 | "(\\d{2})?" + COMMA + 85 | 86 | CAP_FLOAT + "?" + COMMA + 87 | CAP_FLOAT + "?" + COMMA + 88 | CAP_FLOAT + "?"); 89 | private static HashMap functions = new HashMap<>(); 90 | 91 | static { 92 | TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); 93 | DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); 94 | functions.put("GPRMC", new ParsingFunction() { 95 | @Override 96 | public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception { 97 | return parseGPRMC(handler, sentence); 98 | } 99 | }); 100 | functions.put("GPGGA", new ParsingFunction() { 101 | @Override 102 | public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception { 103 | return parseGPGGA(handler, sentence); 104 | } 105 | }); 106 | functions.put("GPGSV", new ParsingFunction() { 107 | @Override 108 | public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception { 109 | return parseGPGSV(handler, sentence); 110 | } 111 | }); 112 | functions.put("GPGSA", new ParsingFunction() { 113 | @Override 114 | public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception { 115 | return parseGPGSA(handler, sentence); 116 | } 117 | }); 118 | } 119 | 120 | private final BasicNMEAHandler handler; 121 | 122 | public BasicNMEAParser(BasicNMEAHandler handler) { 123 | this.handler = handler; 124 | 125 | if (handler == null) { 126 | throw new NullPointerException(); 127 | } 128 | } 129 | 130 | private static boolean parseGPRMC(BasicNMEAHandler handler, String sentence) throws Exception { 131 | ExMatcher matcher = new ExMatcher(GPRMC.matcher(sentence)); 132 | if (matcher.matches()) { 133 | long time = TIME_FORMAT.parse(matcher.nextString("time") + "0").getTime(); 134 | Float ms = matcher.nextFloat("time-ms"); 135 | if (ms != null) { 136 | time += ms * 1000; 137 | } 138 | if (Status.valueOf(matcher.nextString("status")) == Status.A) { 139 | double latitude = toDegrees(matcher.nextInt("degrees"), 140 | matcher.nextFloat("minutes")); 141 | VDir vDir = VDir.valueOf(matcher.nextString("vertical-direction")); 142 | double longitude = toDegrees(matcher.nextInt("degrees"), 143 | matcher.nextFloat("minutes")); 144 | HDir hDir = HDir.valueOf(matcher.nextString("horizontal-direction")); 145 | float speed = matcher.nextFloat("speed") * KNOTS2MPS; 146 | float direction = matcher.nextFloat("direction", 0.0f); 147 | long date = DATE_FORMAT.parse(matcher.nextString("date")).getTime(); 148 | Float magVar = matcher.nextFloat("magnetic-variation"); 149 | String magVarDir = matcher.nextString("direction"); 150 | String faa = matcher.nextString("faa"); 151 | 152 | handler.onRMC(date, 153 | time, 154 | vDir.equals(VDir.N) ? latitude : -latitude, 155 | hDir.equals(HDir.E) ? longitude : -longitude, 156 | speed, 157 | direction); 158 | 159 | return true; 160 | } 161 | } 162 | 163 | return false; 164 | } 165 | 166 | private static boolean parseGPGGA(BasicNMEAHandler handler, String sentence) throws Exception { 167 | ExMatcher matcher = new ExMatcher(GPGGA.matcher(sentence)); 168 | if (matcher.matches()) { 169 | long time = TIME_FORMAT.parse(matcher.nextString("time") + "0").getTime(); 170 | Float ms = matcher.nextFloat("time-ms"); 171 | if (ms != null) { 172 | time += ms * 1000; 173 | } 174 | double latitude = toDegrees(matcher.nextInt("degrees"), 175 | matcher.nextFloat("minutes")); 176 | VDir vDir = VDir.valueOf(matcher.nextString("vertical-direction")); 177 | double longitude = toDegrees(matcher.nextInt("degrees"), 178 | matcher.nextFloat("minutes")); 179 | HDir hDir = HDir.valueOf(matcher.nextString("horizontal-direction")); 180 | FixQuality quality = FixQuality.values()[matcher.nextInt("quality")]; 181 | int satellites = matcher.nextInt("n-satellites"); 182 | float hdop = matcher.nextFloat("hdop"); 183 | float altitude = matcher.nextFloat("altitude"); 184 | float separation = matcher.nextFloat("separation"); 185 | Float age = matcher.nextFloat("age"); 186 | Integer station = matcher.nextInt("station"); 187 | 188 | handler.onGGA(time, 189 | vDir.equals(VDir.N) ? latitude : -latitude, 190 | hDir.equals(HDir.E) ? longitude : -longitude, 191 | altitude - separation, 192 | quality, 193 | satellites, 194 | hdop); 195 | 196 | return true; 197 | } 198 | 199 | return false; 200 | } 201 | 202 | private static boolean parseGPGSV(BasicNMEAHandler handler, String sentence) throws Exception { 203 | ExMatcher matcher = new ExMatcher(GPGSV.matcher(sentence)); 204 | if (matcher.matches()) { 205 | int sentences = matcher.nextInt("n-sentences"); 206 | int index = matcher.nextInt("sentence-index") - 1; 207 | int satellites = matcher.nextInt("n-satellites"); 208 | 209 | for (int i = 0; i < 4; i++) { 210 | Integer prn = matcher.nextInt("prn"); 211 | Integer elevation = matcher.nextInt("elevation"); 212 | Integer azimuth = matcher.nextInt("azimuth"); 213 | Integer snr = matcher.nextInt("snr"); 214 | 215 | if (prn != null) { 216 | handler.onGSV(satellites, index * 4 + i, prn, elevation, azimuth, snr); 217 | } 218 | } 219 | 220 | return true; 221 | } 222 | return false; 223 | } 224 | 225 | private static boolean parseGPGSA(BasicNMEAHandler handler, String sentence) { 226 | ExMatcher matcher = new ExMatcher(GPGSA.matcher(sentence)); 227 | if (matcher.matches()) { 228 | Mode mode = Mode.valueOf(matcher.nextString("mode")); 229 | FixType type = FixType.values()[matcher.nextInt("fix-type")]; 230 | Set prns = new HashSet<>(); 231 | for (int i = 0; i < 12; i++) { 232 | Integer prn = matcher.nextInt("prn"); 233 | if (prn != null) { 234 | prns.add(prn); 235 | } 236 | } 237 | float pdop = matcher.nextFloat("pdop"); 238 | float hdop = matcher.nextFloat("hdop"); 239 | float vdop = matcher.nextFloat("vdop"); 240 | 241 | handler.onGSA(type, prns, pdop, hdop, vdop); 242 | 243 | return true; 244 | } 245 | return false; 246 | } 247 | 248 | private static int calculateChecksum(String sentence) throws UnsupportedEncodingException { 249 | byte[] bytes = sentence.substring(1, sentence.length() - 3).getBytes("US-ASCII"); 250 | int checksum = 0; 251 | for (byte b : bytes) { 252 | checksum ^= b; 253 | } 254 | return checksum; 255 | } 256 | 257 | private static double toDegrees(int degrees, float minutes) { 258 | return degrees + minutes / 60.0; 259 | } 260 | 261 | private static > String regexify(Class clazz) { 262 | StringBuilder sb = new StringBuilder(); 263 | sb.append("(["); 264 | for (T c : clazz.getEnumConstants()) { 265 | sb.append(c.toString()); 266 | } 267 | sb.append("])"); 268 | 269 | return sb.toString(); 270 | } 271 | 272 | public synchronized void parse(String sentence) { 273 | if (sentence == null) { 274 | throw new NullPointerException(); 275 | } 276 | 277 | handler.onStart(); 278 | try { 279 | ExMatcher matcher = new ExMatcher(GENERAL_SENTENCE.matcher(sentence)); 280 | if (matcher.matches()) { 281 | String type = matcher.nextString("type"); 282 | String content = matcher.nextString("content"); 283 | int expected_checksum = matcher.nextHexInt("checksum"); 284 | int actual_checksum = calculateChecksum(sentence); 285 | 286 | if (actual_checksum != expected_checksum) { 287 | handler.onBadChecksum(expected_checksum, actual_checksum); 288 | } else if (!functions.containsKey(type) || !functions.get(type).parse(handler, content)) { 289 | handler.onUnrecognized(sentence); 290 | } 291 | } else { 292 | handler.onUnrecognized(sentence); 293 | } 294 | } catch (Exception e) { 295 | handler.onException(e); 296 | } finally { 297 | handler.onFinished(); 298 | } 299 | } 300 | 301 | private enum Status { 302 | A, 303 | V 304 | } 305 | 306 | private enum HDir { 307 | E, 308 | W 309 | } 310 | 311 | private enum VDir { 312 | N, 313 | S, 314 | } 315 | 316 | private enum Mode { 317 | A, 318 | M 319 | } 320 | 321 | private enum FFA { 322 | A, 323 | D, 324 | E, 325 | M, 326 | S, 327 | N 328 | } 329 | 330 | private static abstract class ParsingFunction { 331 | public abstract boolean parse(BasicNMEAHandler handler, String sentence) throws Exception; 332 | } 333 | 334 | private static class ExMatcher { 335 | Matcher original; 336 | int index; 337 | 338 | ExMatcher(Matcher original) { 339 | this.original = original; 340 | reset(); 341 | } 342 | 343 | void reset() { 344 | index = 1; 345 | } 346 | 347 | boolean matches() { 348 | return original.matches(); 349 | } 350 | 351 | String nextString(String name) { 352 | return original.group(index++); 353 | } 354 | 355 | Float nextFloat(String name, Float defaultValue) { 356 | Float next = nextFloat(name); 357 | return next == null ? defaultValue : next; 358 | } 359 | 360 | Float nextFloat(String name) { 361 | String next = nextString(name); 362 | return next == null ? null : Float.parseFloat(next); 363 | } 364 | 365 | Integer nextInt(String name) { 366 | String next = nextString(name); 367 | return next == null ? null : Integer.parseInt(next); 368 | } 369 | 370 | Integer nextHexInt(String name) { 371 | String next = nextString(name); 372 | return next == null ? null : Integer.parseInt(next, 16); 373 | } 374 | } 375 | } 376 | -------------------------------------------------------------------------------- /src/test/java/com/github/petr_s/nmea/Helper.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import org.hamcrest.Description; 4 | import org.mockito.ArgumentMatcher; 5 | 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | public class Helper { 10 | public static ArgumentMatcher roughlyEq(final double expected) { 11 | return roughlyEq(expected, 0.0001); 12 | } 13 | 14 | public static ArgumentMatcher roughlyEq(final double expected, final double delta) { 15 | return new ArgumentMatcher() { 16 | @Override 17 | public boolean matches(Object argument) { 18 | return Math.abs(expected - (Double) argument) <= delta; 19 | } 20 | 21 | @Override 22 | public void describeTo(Description description) { 23 | description.appendText(Double.toString(expected) + "±" + Double.toString(delta)); 24 | } 25 | }; 26 | } 27 | 28 | public static ArgumentMatcher roughlyEq(final float expected) { 29 | return roughlyEq(expected, 0.0001f); 30 | } 31 | 32 | public static ArgumentMatcher roughlyEq(final float expected, final float delta) { 33 | return new ArgumentMatcher() { 34 | @Override 35 | public boolean matches(Object argument) { 36 | return Math.abs(expected - (Float) argument) <= delta; 37 | } 38 | 39 | @Override 40 | public void describeTo(Description description) { 41 | description.appendText(Float.toString(expected) + "±" + Float.toString(delta)); 42 | } 43 | }; 44 | } 45 | 46 | public static ArgumentMatcher eq(final Set expected) { 47 | return new ArgumentMatcher() { 48 | @Override 49 | public boolean matches(Object argument) { 50 | return argument.equals(expected); 51 | } 52 | 53 | @Override 54 | public void describeTo(Description description) { 55 | description.appendText(expected.toString()); 56 | } 57 | }; 58 | } 59 | 60 | public static ArgumentMatcher eq(final List expected) { 61 | return new ArgumentMatcher() { 62 | @Override 63 | public boolean matches(Object argument) { 64 | return argument.equals(expected); 65 | } 66 | 67 | @Override 68 | public void describeTo(Description description) { 69 | description.appendText(expected.toString()); 70 | } 71 | }; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/github/petr_s/nmea/NMEAParserTest.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea; 2 | 3 | import android.location.Location; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.mockito.Mock; 8 | import org.mockito.Spy; 9 | import org.mockito.runners.MockitoJUnitRunner; 10 | 11 | import java.util.Arrays; 12 | 13 | import static com.github.petr_s.nmea.Helper.*; 14 | import static org.mockito.Mockito.*; 15 | 16 | 17 | @RunWith(MockitoJUnitRunner.class) 18 | public class NMEAParserTest { 19 | @Spy 20 | NMEAHandler handler = new NMEAAdapter(); 21 | 22 | @Mock 23 | Location location; 24 | 25 | @Mock 26 | LocationFactory locationFactory; 27 | 28 | NMEAParser parser; 29 | 30 | @Before 31 | public void setUp() { 32 | parser = new NMEAParser(handler, locationFactory); 33 | } 34 | 35 | 36 | @Test(expected = NullPointerException.class) 37 | public void testConstructorNull() throws Exception { 38 | new NMEAParser(null); 39 | } 40 | 41 | @Test(expected = NullPointerException.class) 42 | public void testParseNull() throws Exception { 43 | parser.parse(null); 44 | } 45 | 46 | @Test 47 | public void testParseLocationRMCGGA() throws Exception { 48 | when(locationFactory.newLocation()).thenReturn(location); 49 | 50 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 51 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 52 | 53 | verify(handler, times(2)).onStart(); 54 | verify(handler, times(2)).onFinish(); 55 | verify(handler).onLocation(location); 56 | verifyNoMoreInteractions(handler); 57 | 58 | verify(location).setTime(eq(1460954639384L)); 59 | verify(location).setLatitude(doubleThat(roughlyEq(50.07914))); 60 | verify(location).setLongitude(doubleThat(roughlyEq(14.39825))); 61 | verify(location).setAltitude(doubleThat(roughlyEq(240.2))); 62 | verify(location).setAccuracy(floatThat(roughlyEq(6.8f))); 63 | verify(location).setSpeed(floatThat(roughlyEq(0.02057f))); 64 | verify(location).setBearing(floatThat(roughlyEq(36.97f))); 65 | verifyNoMoreInteractions(location); 66 | 67 | verify(locationFactory).newLocation(); 68 | verifyNoMoreInteractions(locationFactory); 69 | } 70 | 71 | @Test 72 | public void testParseLocationGGARMC() throws Exception { 73 | when(locationFactory.newLocation()).thenReturn(location); 74 | 75 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 76 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 77 | 78 | verify(handler, times(2)).onStart(); 79 | verify(handler, times(2)).onFinish(); 80 | verify(handler).onLocation(location); 81 | verifyNoMoreInteractions(handler); 82 | 83 | verify(location).setTime(eq(1460954639384L)); 84 | verify(location).setLatitude(doubleThat(roughlyEq(50.079141))); 85 | verify(location).setLongitude(doubleThat(roughlyEq(14.39825))); 86 | verify(location).setAltitude(doubleThat(roughlyEq(240.2))); 87 | verify(location).setAccuracy(floatThat(roughlyEq(6.8f))); 88 | verify(location).setSpeed(floatThat(roughlyEq(0.02057f))); 89 | verify(location).setBearing(floatThat(roughlyEq(36.97f))); 90 | verifyNoMoreInteractions(location); 91 | 92 | verify(locationFactory).newLocation(); 93 | verifyNoMoreInteractions(locationFactory); 94 | } 95 | 96 | @Test 97 | public void testParseLocationRMCGGADiffTime() throws Exception { 98 | when(locationFactory.newLocation()).thenReturn(location); 99 | 100 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 101 | parser.parse("$GPGGA,163408.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*50"); 102 | 103 | verify(handler, times(2)).onStart(); 104 | verify(handler, times(2)).onFinish(); 105 | verifyNoMoreInteractions(handler); 106 | 107 | verify(locationFactory, times(2)).newLocation(); 108 | verifyNoMoreInteractions(locationFactory); 109 | } 110 | 111 | @Test 112 | public void testParseLocationGGARMCDiffTime() throws Exception { 113 | when(locationFactory.newLocation()).thenReturn(location); 114 | 115 | parser.parse("$GPGGA,163408.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*50"); 116 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 117 | 118 | verify(handler, times(2)).onStart(); 119 | verify(handler, times(2)).onFinish(); 120 | verifyNoMoreInteractions(handler); 121 | 122 | verify(locationFactory, times(2)).newLocation(); 123 | verifyNoMoreInteractions(locationFactory); 124 | } 125 | 126 | @Test 127 | public void testParseLocationGGAGGA() throws Exception { 128 | when(locationFactory.newLocation()).thenReturn(location); 129 | 130 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 131 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 132 | 133 | verify(handler, times(2)).onStart(); 134 | verify(handler, times(2)).onFinish(); 135 | verifyNoMoreInteractions(handler); 136 | 137 | verify(locationFactory).newLocation(); 138 | verifyNoMoreInteractions(locationFactory); 139 | } 140 | 141 | @Test 142 | public void testParseLocationRMCRMC() throws Exception { 143 | when(locationFactory.newLocation()).thenReturn(location); 144 | 145 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 146 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 147 | 148 | verify(handler, times(2)).onStart(); 149 | verify(handler, times(2)).onFinish(); 150 | verifyNoMoreInteractions(handler); 151 | 152 | verify(locationFactory).newLocation(); 153 | verifyNoMoreInteractions(locationFactory); 154 | } 155 | 156 | @Test 157 | public void testParseLocationRMCGGAGGASameTime() throws Exception { 158 | when(locationFactory.newLocation()).thenReturn(location, mock(Location.class)); 159 | 160 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 161 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 162 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 163 | 164 | verify(handler, times(3)).onStart(); 165 | verify(handler, times(3)).onFinish(); 166 | verify(handler).onLocation(location); 167 | verifyNoMoreInteractions(handler); 168 | 169 | verify(location).setTime(eq(1460954639384L)); 170 | verify(location).setLatitude(doubleThat(roughlyEq(50.079141))); 171 | verify(location).setLongitude(doubleThat(roughlyEq(14.39825))); 172 | verify(location).setAltitude(doubleThat(roughlyEq(240.2))); 173 | verify(location).setAccuracy(floatThat(roughlyEq(6.8f))); 174 | verify(location).setSpeed(floatThat(roughlyEq(0.02057f))); 175 | verify(location).setBearing(floatThat(roughlyEq(36.97f))); 176 | verifyNoMoreInteractions(location); 177 | 178 | verify(locationFactory, times(2)).newLocation(); 179 | verifyNoMoreInteractions(locationFactory); 180 | } 181 | 182 | @Test 183 | public void testParseLocationRMCGGARMCSameTime() throws Exception { 184 | when(locationFactory.newLocation()).thenReturn(location, mock(Location.class)); 185 | 186 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 187 | parser.parse("$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"); 188 | parser.parse("$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"); 189 | 190 | verify(handler, times(3)).onStart(); 191 | verify(handler, times(3)).onFinish(); 192 | verify(handler).onLocation(location); 193 | verifyNoMoreInteractions(handler); 194 | 195 | verify(location).setTime(eq(1460954639384L)); 196 | verify(location).setLatitude(doubleThat(roughlyEq(50.07914))); 197 | verify(location).setLongitude(doubleThat(roughlyEq(14.39825))); 198 | verify(location).setAltitude(doubleThat(roughlyEq(240.2))); 199 | verify(location).setAccuracy(floatThat(roughlyEq(6.8f))); 200 | verify(location).setSpeed(floatThat(roughlyEq(0.02057f))); 201 | verify(location).setBearing(floatThat(roughlyEq(36.97f))); 202 | verifyNoMoreInteractions(location); 203 | 204 | verify(locationFactory, times(2)).newLocation(); 205 | verifyNoMoreInteractions(locationFactory); 206 | } 207 | 208 | private GpsSatellite newSatellite(int prn, float elevation, float azimuth, int snr, boolean fix) { 209 | GpsSatellite satellite = new GpsSatellite(prn); 210 | satellite.setAzimuth(azimuth); 211 | satellite.setElevation(elevation); 212 | satellite.setSnr(snr); 213 | satellite.setUsedInFix(fix); 214 | satellite.setHasAlmanac(true); 215 | satellite.setHasEphemeris(true); 216 | 217 | return satellite; 218 | } 219 | 220 | @Test 221 | public void testParseSatelliteGSVGSA() throws Exception { 222 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 223 | parser.parse("$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"); 224 | 225 | verify(handler, times(2)).onStart(); 226 | verify(handler, times(2)).onFinish(); 227 | verifyNoMoreInteractions(handler); 228 | } 229 | 230 | @Test 231 | public void testParseSatellite3GSVGGA() throws Exception { 232 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 233 | parser.parse("$GPGSV,3,2,11,12,23,110,34,26,18,295,29,21,17,190,30,05,11,092,25*72"); 234 | parser.parse("$GPGSV,3,3,11,14,02,232,13,23,02,346,12,20,01,135,13*48"); 235 | parser.parse("$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"); 236 | 237 | verify(handler, times(4)).onStart(); 238 | verify(handler, times(4)).onFinish(); 239 | verify(handler).onSatellites(argThat(eq(Arrays.asList(new GpsSatellite[]{ 240 | newSatellite(29, 86.0f, 273.0f, 30, true), 241 | newSatellite(25, 60.0f, 110.0f, 38, true), 242 | newSatellite(31, 52.0f, 278.0f, 47, true), 243 | newSatellite(2, 28.0f, 50.0f, 39, true), 244 | newSatellite(12, 23.0f, 110.0f, 34, true), 245 | newSatellite(26, 18.0f, 295.0f, 29, true), 246 | newSatellite(21, 17.0f, 190.0f, 30, true), 247 | newSatellite(5, 11.0f, 92.0f, 25, true), 248 | newSatellite(14, 2.0f, 232.0f, 13, false), 249 | newSatellite(23, 2.0f, 346.0f, 12, false), 250 | newSatellite(20, 1.0f, 135.0f, 13, false)})))); 251 | verifyNoMoreInteractions(handler); 252 | } 253 | 254 | @Test 255 | public void testParseSatelliteGGA3GSV() throws Exception { 256 | parser.parse("$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"); 257 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 258 | parser.parse("$GPGSV,3,2,11,12,23,110,34,26,18,295,29,21,17,190,30,05,11,092,25*72"); 259 | parser.parse("$GPGSV,3,3,11,14,02,232,13,23,02,346,12,20,01,135,13*48"); 260 | 261 | verify(handler, times(4)).onStart(); 262 | verify(handler, times(4)).onFinish(); 263 | verify(handler).onSatellites(argThat(eq(Arrays.asList(new GpsSatellite[]{ 264 | newSatellite(29, 86.0f, 273.0f, 30, true), 265 | newSatellite(25, 60.0f, 110.0f, 38, true), 266 | newSatellite(31, 52.0f, 278.0f, 47, true), 267 | newSatellite(2, 28.0f, 50.0f, 39, true), 268 | newSatellite(12, 23.0f, 110.0f, 34, true), 269 | newSatellite(26, 18.0f, 295.0f, 29, true), 270 | newSatellite(21, 17.0f, 190.0f, 30, true), 271 | newSatellite(5, 11.0f, 92.0f, 25, true), 272 | newSatellite(14, 2.0f, 232.0f, 13, false), 273 | newSatellite(23, 2.0f, 346.0f, 12, false), 274 | newSatellite(20, 1.0f, 135.0f, 13, false)})))); 275 | verifyNoMoreInteractions(handler); 276 | } 277 | 278 | @Test 279 | public void testParseSatellite2GSVGGAGSV() throws Exception { 280 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 281 | parser.parse("$GPGSV,3,2,11,12,23,110,34,26,18,295,29,21,17,190,30,05,11,092,25*72"); 282 | parser.parse("$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"); 283 | parser.parse("$GPGSV,3,3,11,14,02,232,13,23,02,346,12,20,01,135,13*48"); 284 | 285 | verify(handler, times(4)).onStart(); 286 | verify(handler, times(4)).onFinish(); 287 | verify(handler).onSatellites(argThat(eq(Arrays.asList(new GpsSatellite[]{ 288 | newSatellite(29, 86.0f, 273.0f, 30, true), 289 | newSatellite(25, 60.0f, 110.0f, 38, true), 290 | newSatellite(31, 52.0f, 278.0f, 47, true), 291 | newSatellite(2, 28.0f, 50.0f, 39, true), 292 | newSatellite(12, 23.0f, 110.0f, 34, true), 293 | newSatellite(26, 18.0f, 295.0f, 29, true), 294 | newSatellite(21, 17.0f, 190.0f, 30, true), 295 | newSatellite(5, 11.0f, 92.0f, 25, true), 296 | newSatellite(14, 2.0f, 232.0f, 13, false), 297 | newSatellite(23, 2.0f, 346.0f, 12, false), 298 | newSatellite(20, 1.0f, 135.0f, 13, false)})))); 299 | verifyNoMoreInteractions(handler); 300 | } 301 | } -------------------------------------------------------------------------------- /src/test/java/com/github/petr_s/nmea/basic/BasicNMEAParserTest.java: -------------------------------------------------------------------------------- 1 | package com.github.petr_s.nmea.basic; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.mockito.Spy; 6 | import org.mockito.runners.MockitoJUnitRunner; 7 | 8 | import java.util.Arrays; 9 | import java.util.HashSet; 10 | 11 | import static com.github.petr_s.nmea.Helper.eq; 12 | import static com.github.petr_s.nmea.Helper.roughlyEq; 13 | import static com.github.petr_s.nmea.basic.BasicNMEAHandler.FixType.Fix3D; 14 | import static org.mockito.Matchers.doubleThat; 15 | import static org.mockito.Matchers.eq; 16 | import static org.mockito.Matchers.floatThat; 17 | import static org.mockito.Mockito.argThat; 18 | import static org.mockito.Mockito.*; 19 | 20 | @RunWith(MockitoJUnitRunner.class) 21 | public class BasicNMEAParserTest { 22 | @Spy 23 | BasicNMEAHandler handler = new BasicNMEAAdapter(); 24 | 25 | @Test(expected = NullPointerException.class) 26 | public void testConstructorNull() throws Exception { 27 | new BasicNMEAParser(null); 28 | } 29 | 30 | @Test(expected = NullPointerException.class) 31 | public void testParseNullSentence() throws Exception { 32 | new BasicNMEAParser(handler).parse(null); 33 | } 34 | 35 | @Test 36 | public void testParseEmpty() throws Exception { 37 | new BasicNMEAParser(handler).parse(""); 38 | 39 | verify(handler).onStart(); 40 | verify(handler).onUnrecognized(""); 41 | verify(handler).onFinished(); 42 | verifyNoMoreInteractions(handler); 43 | } 44 | 45 | @Test 46 | public void testParseGPRMC() throws Exception { 47 | String sentence = "$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*38"; 48 | new BasicNMEAParser(handler).parse(sentence); 49 | 50 | verify(handler).onStart(); 51 | verify(handler).onRMC(eq(1460937600000L), 52 | eq(59647000L), 53 | doubleThat(roughlyEq(50.07914)), 54 | doubleThat(roughlyEq(14.39825)), 55 | floatThat(roughlyEq(0.02057f)), 56 | floatThat(roughlyEq(36.97f))); 57 | verify(handler).onFinished(); 58 | verifyNoMoreInteractions(handler); 59 | } 60 | 61 | @Test 62 | public void testParseGPRMC_2_3() throws Exception { 63 | String sentence = "$GPRMC,093933.40,A,5004.52493,N,01424.28771,E,0.277,,130616,,,A*76"; 64 | new BasicNMEAParser(handler).parse(sentence); 65 | 66 | verify(handler).onStart(); 67 | verify(handler).onRMC(eq(1465776000000L), 68 | eq(34773400L), 69 | doubleThat(roughlyEq(50.075415)), 70 | doubleThat(roughlyEq(14.404795)), 71 | floatThat(roughlyEq(0.142501f)), 72 | floatThat(roughlyEq(0.0f))); 73 | verify(handler).onFinished(); 74 | verifyNoMoreInteractions(handler); 75 | } 76 | 77 | @Test 78 | public void testParseGPRMCBadChecksum() throws Exception { 79 | String sentence = "$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*42"; 80 | new BasicNMEAParser(handler).parse(sentence); 81 | 82 | verify(handler).onStart(); 83 | verify(handler).onBadChecksum(66, 56); 84 | verify(handler).onFinished(); 85 | verifyNoMoreInteractions(handler); 86 | } 87 | 88 | @Test 89 | public void testParseGPRMCEOL() throws Exception { 90 | String sentence = "\n$GPRMC,163407.000,A,5004.7485,N,01423.8956,E,0.04,36.97,180416,,*42"; 91 | new BasicNMEAParser(handler).parse(sentence); 92 | 93 | verify(handler).onStart(); 94 | verify(handler).onUnrecognized(sentence); 95 | verify(handler).onFinished(); 96 | verifyNoMoreInteractions(handler); 97 | } 98 | 99 | @Test 100 | public void testParseGPGGA() throws Exception { 101 | String sentence = "$GPGGA,163407.000,5004.7485,N,01423.8956,E,1,07,1.7,285.7,M,45.5,M,,0000*5F"; 102 | new BasicNMEAParser(handler).parse(sentence); 103 | 104 | verify(handler).onStart(); 105 | verify(handler).onGGA(eq(59647000L), 106 | doubleThat(roughlyEq(50.07914)), 107 | doubleThat(roughlyEq(14.39825)), 108 | floatThat(roughlyEq(240.2f)), 109 | eq(BasicNMEAHandler.FixQuality.GPS), 110 | eq(7), 111 | floatThat(roughlyEq(1.7f))); 112 | verify(handler).onFinished(); 113 | verifyNoMoreInteractions(handler); 114 | } 115 | 116 | @Test 117 | public void testParseGPGSVSingle() throws Exception { 118 | String sentence = "$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"; 119 | new BasicNMEAParser(handler).parse(sentence); 120 | 121 | verify(handler).onStart(); 122 | verify(handler).onGSV(eq(11), 123 | eq(0), 124 | eq(29), 125 | eq(86.0f), 126 | eq(273.0f), 127 | eq(30)); 128 | verify(handler).onGSV(eq(11), 129 | eq(1), 130 | eq(25), 131 | eq(60.0f), 132 | eq(110.0f), 133 | eq(38)); 134 | verify(handler).onGSV(eq(11), 135 | eq(2), 136 | eq(31), 137 | eq(52.0f), 138 | eq(278.0f), 139 | eq(47)); 140 | verify(handler).onGSV(eq(11), 141 | eq(3), 142 | eq(2), 143 | eq(28.0f), 144 | eq(50.0f), 145 | eq(39)); 146 | verify(handler).onFinished(); 147 | verifyNoMoreInteractions(handler); 148 | } 149 | 150 | @Test 151 | public void testParseGPGSVFull() throws Exception { 152 | BasicNMEAParser parser = new BasicNMEAParser(handler); 153 | parser.parse("$GPGSV,3,1,11,29,86,273,30,25,60,110,38,31,52,278,47,02,28,050,39*7D"); 154 | parser.parse("$GPGSV,3,2,11,12,23,110,34,26,18,295,29,21,17,190,30,05,11,092,25*72"); 155 | parser.parse("$GPGSV,3,3,11,14,02,232,13,23,02,346,12,20,01,135,13*48"); 156 | 157 | verify(handler, times(3)).onStart(); 158 | verify(handler).onGSV(eq(11), 159 | eq(0), 160 | eq(29), 161 | eq(86.0f), 162 | eq(273.0f), 163 | eq(30)); 164 | verify(handler).onGSV(eq(11), 165 | eq(1), 166 | eq(25), 167 | eq(60.0f), 168 | eq(110.0f), 169 | eq(38)); 170 | verify(handler).onGSV(eq(11), 171 | eq(2), 172 | eq(31), 173 | eq(52.0f), 174 | eq(278.0f), 175 | eq(47)); 176 | verify(handler).onGSV(eq(11), 177 | eq(3), 178 | eq(2), 179 | eq(28.0f), 180 | eq(50.0f), 181 | eq(39)); 182 | verify(handler).onGSV(eq(11), 183 | eq(4), 184 | eq(12), 185 | eq(23.0f), 186 | eq(110.0f), 187 | eq(34)); 188 | verify(handler).onGSV(eq(11), 189 | eq(5), 190 | eq(26), 191 | eq(18.0f), 192 | eq(295.0f), 193 | eq(29)); 194 | verify(handler).onGSV(eq(11), 195 | eq(6), 196 | eq(21), 197 | eq(17.0f), 198 | eq(190.0f), 199 | eq(30)); 200 | verify(handler).onGSV(eq(11), 201 | eq(7), 202 | eq(5), 203 | eq(11.0f), 204 | eq(92.0f), 205 | eq(25)); 206 | verify(handler).onGSV(eq(11), 207 | eq(8), 208 | eq(14), 209 | eq(2.0f), 210 | eq(232.0f), 211 | eq(13)); 212 | verify(handler).onGSV(eq(11), 213 | eq(9), 214 | eq(23), 215 | eq(2.0f), 216 | eq(346.0f), 217 | eq(12)); 218 | verify(handler).onGSV(eq(11), 219 | eq(10), 220 | eq(20), 221 | eq(1.0f), 222 | eq(135.0f), 223 | eq(13)); 224 | verify(handler, times(3)).onFinished(); 225 | verifyNoMoreInteractions(handler); 226 | } 227 | 228 | @Test 229 | public void testParseGPGSA() throws Exception { 230 | String sentence = "$GPGSA,A,3,25,02,26,05,29,31,21,12,,,,,1.6,1.0,1.3*3B"; 231 | new BasicNMEAParser(handler).parse(sentence); 232 | 233 | verify(handler).onStart(); 234 | verify(handler).onGSA(eq(Fix3D), 235 | argThat(eq(new HashSet<>(Arrays.asList(new Integer[]{2, 5, 21, 25, 26, 12, 29, 31})))), 236 | floatThat(roughlyEq(1.6f)), 237 | floatThat(roughlyEq(1.0f)), 238 | floatThat(roughlyEq(1.3f))); 239 | verify(handler).onFinished(); 240 | verifyNoMoreInteractions(handler); 241 | } 242 | } --------------------------------------------------------------------------------