[英]Is there a way to pass current speed data along with latitude and longitude using the Android emulator and 'geo fix'?
I'm currently building an Android app that uses the Android emulator to pass GPS location data, specifically the latitude
, longitude
, and speed
. 我目前正在构建一个Android应用,该应用使用Android模拟器传递GPS位置数据,尤其是latitude
, longitude
和speed
。 I have used a real device and everything is okay, but I now need to do so using only an emulator. 我使用了真实的设备,一切正常,但是现在我只需要使用模拟器即可。
The getSpeed
method of the Location
class is used to get the speed
information. Location
类的getSpeed
方法用于获取speed
信息。 The speed
is reported in meters/second. speed
以米/秒为单位报告。 The Location
class also provides latitude
and longitude
using getLatitude
and getLongitude
, respectively. Location
类还分别使用getLatitude
和getLongitude
提供latitude
和longitude
。
Using geo fix longitude latitude
when "telnet"-ing into the emulator only allows you to pass latitude
and longitude
as parameters in the following format geo fix <longitude> <latitude>
. 在通过“ telnet”进入仿真器时,仅使用geo fix longitude latitude
可以将latitude
和longitude
作为参数传递为以下格式的geo fix <longitude> <latitude>
。
If I were to pass speed from the command line is there a way to do this? 如果我要从命令行传递速度,有没有办法做到这一点? I have already read into using the Ripple extention for the Chrome browser. 我已经读过将Ripple扩展用于Chrome浏览器的内容。
Thank you. 谢谢。
Regarding the units... Beware! 关于单位...当心! On my Android 2.2 phone, Location.getSpeed() returns the speed in KNOTS , not m/s as stated in the Android developer documentation :( 在我的Android 2.2手机上,Location.getSpeed()以KNOTS返回速度,而不是Android开发人员文档中所述的m / s:
There are a couple of other posts on the Net about this. 网络上还有其他一些与此相关的帖子。
In my Android app, I have a class to record NMEA data to a file. 在我的Android应用中,我有一个用于将NMEA数据记录到文件中的类。
package log.nmea;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;
/**
* NMEA logging... just write NMEA stuff to file on SD card
*
* @author Frank van der Hulst
*/
public class NmeaLog {
private static BufferedWriter logWriter = null;
private static FileWriter fstream = null;
private static File logFile;
private static LocationManager locationManager = null;
// Default pattern is to log any message starting with $GP
private static final Pattern pattern = Pattern.compile("^$GP\\w+,");
private static final String LOGNAME = "NmeaLog";
private static GpsStatus.NmeaListener nmeaListener = new GpsStatus.NmeaListener() {
public void onNmeaReceived(long timestamp, String nmea) {
Log.d(LOGNAME, "onNmeaReceived(" + nmea + ")");
if (logWriter == null)
return;
if (!pattern.matcher(nmea).matches())
return;
Log.d(LOGNAME, "onNmeaReceived appending");
try {
logWriter.append(timestamp + "," + nmea);
} catch (IOException ex) {
Log.e(LOGNAME, ex.getMessage());
}
}
};
/**
* Initialise NMEA log file & open for writing
*
* @param filepath
* @param append
* @param lm
*/
public static void open(String filepath, boolean append, LocationManager lm) {
if (logWriter != null)
return; // Already open
// Check SD card is available
String SDCardStatus = Environment.getExternalStorageState();
if (!SDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.e(LOGNAME, "SD card error: " + SDCardStatus);
return;
}
// SD card is OK... try to create/open file
logFile = new File(filepath);
if (logFile == null || logFile.isDirectory()) {
Log.e(LOGNAME, filepath + " cannot be opened");
return;
}
locationManager = lm;
if (!locationManager.addNmeaListener(nmeaListener)) {
Log.e(LOGNAME, "Failed to add NMEA listener");
return;
}
Log.d(LOGNAME, "opening " + filepath);
try {
fstream = new FileWriter(logFile, append);
logWriter = new BufferedWriter(fstream, 1024);
} catch (Exception ex) {
Log.e(LOGNAME, "Open failed: " + ex.getMessage());
return;
}
Log.d(LOGNAME, "NMEA listener added successfully");
}
/**
* Close log file. The file can be reopened, so don't null locationManager
*/
public static void close() {
Log.d(LOGNAME, "close()");
try {
locationManager.removeNmeaListener(nmeaListener);
logWriter.close();
} catch (Exception ex) {
Log.e(LOGNAME, "Close error: " + ex.getMessage());
}
logWriter = null;
}
}
Below is a regular Java class which plays these files to the emulator. 下面是一个常规Java类,它将这些文件播放到模拟器。 Very useful when debugging GPS apps. 在调试GPS应用程序时非常有用。
package sendgps;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import util.Log;
import util.OSSpecific;
import util.Util;
/**
*
* @author Frank van der Hulst
*/
public class SendGPS {
private static Socket socket;
private static OutputStreamWriter w;
private static BufferedReader r;
private static final String NMEA_dir = "/home/frank/NetBeansProjects/SendGPS/";
@SuppressWarnings("UseOfSystemOutOrSystemErr")
private static void write(String s) throws IOException, InterruptedException {
// System.out.println("writing '" + s + "'");
w.append(s).append("\r\n");
w.flush();
String result = r.readLine();
if (!result.equals("OK")) {
System.out.println(s + " -> " + result);
}
}
@SuppressWarnings("UseOfSystemOutOrSystemErr")
private static void read() throws IOException {
while (r.ready()) {
System.out.print((char) r.read());
}
}
@SuppressWarnings({"SleepWhileInLoop", "UseOfSystemOutOrSystemErr", "deprecation"})
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
socket = new Socket("localhost", 5554);
final String filename = NMEA_dir + "nmea.log";
socket.setKeepAlive(true);
r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
w = new OutputStreamWriter(socket.getOutputStream());
Thread.sleep(100);
read(); // Remove initial prompt
String[] lines = Util.getFileContents(filename);
long prevTimestamp = 0;
for (int line_num = 0; line_num < lines.length; line_num++) {
String[] parts = lines[line_num].split(",", 2); // Separate timestamp from NMEA string
if (parts[1].isEmpty()) {
continue;
}
long timestamp = Long.parseLong(parts[0]);
// System.out.println(timestamp + ", "+ prevTimestamp);
if (timestamp != prevTimestamp) {
if (prevTimestamp != 0) {
int interval = (int) (timestamp - prevTimestamp);
// interval /= 10;
// System.out.print("Sleep: " + interval);
if (interval > 50) {
Thread.sleep(interval - 50);
}
}
prevTimestamp = timestamp;
}
if (parts[1].startsWith("$GPGGA") || parts[1].startsWith("$GPRMC")) {
// Replace hhmmss.ss timestamp at field 1 with current time
Date now = new Date();
String[] data = parts[1].split(",", 3);
parts[1] = data[0] + "," + now.getHours() + "" + now.getMinutes() + "" + now.getSeconds() + "," + data[2];
}
write("geo nmea " + parts[1]);
System.out.println(parts[1]);
}
socket.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.