简体   繁体   中英

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 . 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. The speed is reported in meters/second. The Location class also provides latitude and longitude using getLatitude and getLongitude , respectively.

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> .

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.

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 :(

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.

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. Very useful when debugging GPS apps.

  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();
      }
  }

You have to use the geo nmea command via telnet:

geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62

The definition of the format is here . In this example, 000.0 is the ground speed in knots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM