Skip to content

Commit d570665

Browse files
authored
Merge pull request #1 from pkamps/master
Some code re-organization, quality translated to a value in meters
2 parents ba3fee5 + 6a600f2 commit d570665

File tree

10 files changed

+157
-56
lines changed

10 files changed

+157
-56
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Built application files
22
*.apk
33
*.ap_
4+
*.iml
45

56
# Files for the Dalvik VM
67
*.dex
@@ -25,3 +26,6 @@ proguard/
2526

2627
# Log Files
2728
*.log
29+
30+
#IDE
31+
.idea/

usbSerialExamples/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 22
4+
compileSdkVersion 23
55
buildToolsVersion "25"
66

77
defaultConfig {
88
minSdkVersion 21
99
targetSdkVersion 23
1010

11-
testApplicationId "com.mikey0000.android.usbserial.examples"
11+
applicationId "com.mikey0000.android.usbserial.examples"
12+
testApplicationId "com.mikey0000.android.usbserial.examples.test"
1213
testInstrumentationRunner "android.test.InstrumentationTestRunner"
1314
}
1415

@@ -21,4 +22,5 @@ android {
2122

2223
dependencies {
2324
compile project(':usbSerialForAndroid')
25+
testCompile 'junit:junit:4.12'
2426
}

usbSerialExamples/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.hoho.android.usbserial.examples"
3+
package="com.mikey0000.android.usbserial.examples"
44
android:versionCode="1"
55
android:versionName="1.0" >
66

@@ -43,7 +43,7 @@
4343
</activity>
4444

4545
<service
46-
android:name="src.com.hoho.android.usbserial.examples.BackgroundService"
46+
android:name="com.mikey0000.android.usbserial.examples.BackgroundService"
4747
android:enabled="true"
4848
android:exported="true" >
4949
</service>
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
package src.com.hoho.android.usbserial.examples;
1+
package com.mikey0000.android.usbserial.examples;
22

33
import android.app.Service;
4+
import android.app.AppOpsManager;
45
import android.content.BroadcastReceiver;
56
import android.content.Context;
67
import android.content.Intent;
78
import android.content.IntentFilter;
9+
import android.hardware.usb.UsbDevice;
10+
import android.hardware.usb.UsbDeviceConnection;
811
import android.hardware.usb.UsbManager;
912
import android.location.Location;
1013
import android.location.LocationManager;
14+
import android.os.Build;
1115
import android.os.IBinder;
12-
import android.hardware.usb.UsbDevice;
13-
import android.hardware.usb.UsbDeviceConnection;
1416
import android.provider.Settings;
1517
import android.util.Log;
1618
import android.widget.Toast;
@@ -20,7 +22,6 @@
2022
import com.hoho.android.usbserial.driver.UsbSerialDriver;
2123
import com.hoho.android.usbserial.driver.UsbSerialPort;
2224
import com.hoho.android.usbserial.driver.UsbSerialProber;
23-
import com.hoho.android.usbserial.examples.NMEAParser;
2425
import com.hoho.android.usbserial.util.SerialInputOutputManager;
2526

2627
import java.io.IOException;
@@ -48,18 +49,18 @@ public BackgroundService() {
4849

4950
public boolean isMockEnabled() {
5051

52+
boolean mock_location = false;
5153

52-
int mock_location = 0;
5354
try {
5455
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
5556

56-
AppOpsManager opsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
57+
AppOpsManager opsManager = (AppOpsManager) this.getApplicationContext().getSystemService(Context.APP_OPS_SERVICE);
5758
mock_location = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED);
5859

5960
} else {
6061

61-
mock_location = Settings.Secure.getInt(this.getContentResolver(), "mock_location");
62-
if (mock_location == 0) {
62+
mock_location = Settings.Secure.getInt(this.getContentResolver(), "mock_location") == 1;
63+
if (!mock_location) {
6364
try {
6465
Settings.Secure.putInt(this.getContentResolver(), "mock_location", 1);
6566
} catch (Exception ex) {
@@ -68,19 +69,15 @@ public boolean isMockEnabled() {
6869
}
6970
}
7071

71-
72-
if (mock_location == 0) {
72+
if (!mock_location) {
7373
Toast.makeText(this, "Turn on the mock locations in your Android settings", Toast.LENGTH_LONG).show();
74-
return false;
75-
} else {
76-
return true;
7774
}
7875

7976
} catch (Exception ex) {
8077
ex.printStackTrace();
8178
}
8279

83-
return false;
80+
return mock_location;
8481
}
8582

8683
private SerialInputOutputManager mSerialIoManager;
@@ -105,12 +102,12 @@ public void onNewData(final byte[] data) {
105102
}
106103
}
107104

108-
//final String message = "Read " + data.length + " bytes: \n"
109-
// + result.toString() + "\n\n";
105+
//final String message = "Read " + data.length + " bytes: "
106+
// + result.toString() + "\n";
110107

111108
Location loc = parser.location(result.toString());
112109

113-
//Log.i(TAG, "reading GPS");
110+
//Log.i(TAG, "reading GPS: " + message );
114111

115112
if (loc != null) {
116113
mockLocationProvider.pushLocation(loc);
@@ -158,6 +155,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
158155

159156
ProbeTable customTable = new ProbeTable();
160157
customTable.addProduct(0x1546, 0x01a7, CdcAcmSerialDriver.class);
158+
customTable.addProduct(0x1546, 0x01a6, CdcAcmSerialDriver.class);
159+
161160
UsbSerialProber prober = new UsbSerialProber(customTable);
162161

163162
mUsbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Project home page: https://github.com/mik3y/usb-serial-for-android
2020
*/
2121

22-
package com.hoho.android.usbserial.examples;
22+
package com.mikey0000.android.usbserial.examples;
2323

2424
import android.app.Activity;
2525
import android.app.PendingIntent;
@@ -54,8 +54,6 @@
5454
import java.util.ArrayList;
5555
import java.util.List;
5656

57-
import src.com.hoho.android.usbserial.examples.BackgroundService;
58-
5957
/**
6058
* Shows a {@link ListView} of available USB devices.
6159
*
@@ -112,6 +110,7 @@ public void onCreate(Bundle savedInstanceState) {
112110

113111
ProbeTable customTable = new ProbeTable();
114112
customTable.addProduct(0x1546, 0x01a7, CdcAcmSerialDriver.class);
113+
customTable.addProduct(0x1546, 0x01a6, CdcAcmSerialDriver.class);
115114
prober = new UsbSerialProber(customTable);
116115

117116
mAdapter = new ArrayAdapter<UsbSerialPort>(this,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.com.hoho.android.usbserial.examples;
1+
package com.mikey0000.android.usbserial.examples;
22

33
import android.content.Context;
44
import android.location.Location;
Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
package com.hoho.android.usbserial.examples;
1+
package com.mikey0000.android.usbserial.examples;
22

33
/**
44
* Created by michaelarthur on 2/09/15.
55
*/
66

77
import android.location.Location;
88
import android.os.SystemClock;
9-
import android.util.Log;
109

11-
import java.lang.reflect.InvocationTargetException;
12-
import java.lang.reflect.Method;
1310
import java.util.HashMap;
1411
import java.util.Map;
1512

1613

1714
public class NMEAParser {
1815

19-
// fucking java interfaces
16+
// java interfaces
2017
interface SentenceParser {
2118
public boolean parse(String[] tokens, GPSPosition position);
2219
}
@@ -30,8 +27,6 @@ static float Latitude2Decimal(String lat, String NS) {
3027
med = -med;
3128
}
3229
return med;
33-
34-
3530
}
3631

3732
static float Longitude2Decimal(String lon, String WE) {
@@ -41,7 +36,6 @@ static float Longitude2Decimal(String lon, String WE) {
4136
med = -med;
4237
}
4338
return med;
44-
4539
}
4640

4741
// parsers
@@ -51,6 +45,8 @@ public boolean parse(String[] tokens, GPSPosition position) {
5145
position.lat = Latitude2Decimal(tokens[2], tokens[3]);
5246
position.lon = Longitude2Decimal(tokens[4], tokens[5]);
5347
position.quality = Integer.parseInt(tokens[6]);
48+
position.numberOfSatellites = Integer.parseInt(tokens[7]);
49+
position.hdop = Float.parseFloat(tokens[8]);
5450
position.altitude = Float.parseFloat(tokens[9]);
5551
return true;
5652
}
@@ -94,32 +90,52 @@ public class GPSPosition {
9490
public float time = 0.0f;
9591
public float lat = 0.0f;
9692
public float lon = 0.0f;
97-
public boolean fixed = false;
9893
public int quality = 0;
9994
public float dir = 0.0f;
10095
public float altitude = 0.0f;
10196
public float velocity = 0.0f;
97+
public int numberOfSatellites = 0;
98+
// See: https://en.wikipedia.org/wiki/Dilution_of_precision_(navigation)
99+
public float hdop = 0.0f;
100+
101+
public boolean fixed = false;
102102

103103
public void updatefix() {
104104
fixed = quality > 0;
105105
}
106106

107107
public String toString() {
108-
return String.format("POSITION: lat: %f, lon: %f, time: %f, Q: %d, dir: %f, alt: %f, vel: %f", lat, lon, time, quality, dir, altitude, velocity);
108+
return String.format(
109+
"POSITION: lat: %f, lon: %f, time: %f, Q: %d, hdop: %f, dir: %f, alt: %f, vel: %f, # of sat: %d, fixed: %b",
110+
lat, lon, time, quality, hdop, dir, altitude, velocity, numberOfSatellites, fixed
111+
);
109112
}
110113
}
111114

112115
GPSPosition position = new GPSPosition();
113116

114117
private static final Map<String, SentenceParser> sentenceParsers = new HashMap<String, SentenceParser>();
115118

119+
/**
120+
* Maps a NMEA quality identifier to the best possible accuracy value (in meters)
121+
*/
122+
protected static final Map<Integer,Float> qualityAccuracy = new HashMap<Integer, Float>();
123+
116124
public NMEAParser() {
117125
sentenceParsers.put("GPGGA", new GPGGA());
118126
sentenceParsers.put("GPGGL", new GPGGL());
119127
sentenceParsers.put("GPRMC", new GPRMC());
120128
sentenceParsers.put("GPRMZ", new GPRMZ());
121129
//only really good GPS devices have this sentence but ...
122-
sentenceParsers.put("GPVTG", new GPVTG());
130+
//sentenceParsers.put("GPVTG", new GPVTG());
131+
132+
// concrete accuracy values (in meters) per NMEA quality level
133+
// this is used to calculate the position accuracy in NMEAParser.calculateAccuracy
134+
qualityAccuracy.put( 0, 0.0f ); // invalid
135+
qualityAccuracy.put( 1, 2.0f ); // GPS 2d/3d
136+
qualityAccuracy.put( 2, 0.7f ); // DGNSS
137+
qualityAccuracy.put( 4, 0.04f ); // RTK fixed
138+
qualityAccuracy.put( 5, 0.04f ); // RTK fixed
123139
}
124140

125141
public GPSPosition parse(String line) {
@@ -149,24 +165,28 @@ public GPSPosition parse(String line) {
149165
}
150166

151167
public Location location(String str) {
152-
Location localLocation;
153-
GPSPosition GPSPos = parse(str);
154-
155-
if (!GPSPos.fixed) {
156-
GPSPos = null;
157-
return null;
168+
Location localLocation = null;
169+
parse( str );
170+
171+
if( position.quality > 0 ) { // quality 0 is an invalid entry
172+
localLocation = new Location("gps");
173+
localLocation.setLongitude(position.lon);
174+
localLocation.setLatitude(position.lat);
175+
localLocation.setAltitude(position.altitude);
176+
localLocation.setSpeed(position.velocity);
177+
localLocation.setBearing(position.dir);
178+
localLocation.setTime(System.currentTimeMillis());
179+
localLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
180+
localLocation.setAccuracy(calculateAccuracy());
181+
182+
position = new GPSPosition();
158183
}
159184

160-
localLocation = new Location("gps");
161-
localLocation.setLongitude(GPSPos.lon);
162-
localLocation.setLatitude(GPSPos.lat);
163-
localLocation.setAltitude(GPSPos.altitude);
164-
localLocation.setSpeed(GPSPos.velocity);
165-
localLocation.setBearing(GPSPos.dir);
166-
localLocation.setTime(System.currentTimeMillis());
167-
localLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
168-
localLocation.setAccuracy(GPSPos.quality);
169-
170185
return localLocation;
171186
}
187+
188+
protected float calculateAccuracy() {
189+
190+
return qualityAccuracy.get( position.quality ) * position.hdop;
191+
}
172192
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Project home page: https://github.com/mik3y/usb-serial-for-android
2020
*/
2121

22-
package com.hoho.android.usbserial.examples;
22+
package com.mikey0000.android.usbserial.examples;
2323

2424
import android.app.Activity;
2525
import android.content.Context;
@@ -36,13 +36,11 @@
3636
import com.hoho.android.usbserial.driver.UsbSerialPort;
3737
import com.hoho.android.usbserial.util.HexDump;
3838
import com.hoho.android.usbserial.util.SerialInputOutputManager;
39-
import com.hoho.android.usbserial.examples.NMEAParser;
4039

4140
import java.io.IOException;
4241
import java.util.concurrent.ExecutorService;
4342
import java.util.concurrent.Executors;
4443

45-
import src.com.hoho.android.usbserial.examples.MockLocationProvider;
4644

4745
/**
4846
* Monitors a single {@link UsbSerialPort} instance, showing all data

usbSerialExamples/src/main/res/xml/device_filter.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<resources>
33
<!-- 0x1546 / 0x01a7: ublox 7020 -->
44
<usb-device vendor-id="5446" product-id="423" />
5-
5+
6+
<!-- 0x1546 / 0x01a6: ublox NEO-6P -->
7+
<usb-device vendor-id="5446" product-id="422" />
8+
69
<!-- 0x0403 / 0x6015: FTDI FT231X -->
710
<usb-device vendor-id="1027" product-id="24597" />
811

0 commit comments

Comments
 (0)