Tima
Tima

Reputation: 12905

Test GPS in Android

Is it a way to test GPS in emulator?

I know how to send a single coordinate, I saw also possibility to load GPX- oder KML-files (haven't try it really), but what I mean, it would be nice to have some component, which would send coordinates periodically emulating normal human motion :)

Maybe there are possibilities to do it programmatically?

Thank you very much for any advice or link.

Mur

Upvotes: 3

Views: 903

Answers (2)

NickT
NickT

Reputation: 23873

I wrote a rough and ready java console app to implement a telnet client and read from a file of coordinates. It's not the prettiest code, but it works:

import org.apache.commons.net.telnet.TelnetClient;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class AutomatedTelnetClient {
   private TelnetClient telnet = new TelnetClient();
   private InputStream in;
   private PrintStream out;
   private String prompt = "#";

   public AutomatedTelnetClient(String server, String user, String password) {
      try {
         // Connect to the specified server
         telnet.connect("127.0.0.1", 5554);
         // Get input and output stream references
         in = telnet.getInputStream();
         out = new PrintStream(telnet.getOutputStream());

         readUntil("OK");
         sendGeoFixes("c:\\junk\\esk.txt");
         System.out.println("Finished");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private void delay() {
      long t = System.currentTimeMillis();
      long finish = t + 15000;
      boolean loop = true;
      while (loop) {
         t = System.currentTimeMillis();
         if (t > finish)
            loop = false;
      }
   }

   private void sendGeoFixes(String fn) {
      boolean loop = true;
      String line;
      BufferedReader br = null;
      String lat, lon;

      String formatStr = "000.000000";
      NumberFormat formatter = new DecimalFormat(formatStr);
      String gf = "geo fix ";
      try {
         br = new BufferedReader(new FileReader(fn));
         do {
            try {
               line = br.readLine();
               if (line == null)
                  break;
               String sa[] = line.split(",");
               if (sa.length < 2)
                  break;
               lat = sa[0];
               String slat = formatter.format(Float.parseFloat(lat));
               lon = sa[1];
               String slon = formatter.format(Float.parseFloat(lon));
               String sFix = slat + " " + slon;
               write(gf + sFix);
               String ans = readUntil("OK");
               System.out.println(ans);
               delay();
      } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
         } while (loop);
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public String readUntil(String pattern) {
      try {
         char lastChar = pattern.charAt(pattern.length() - 1);
         StringBuffer sb = new StringBuffer();
         char ch = (char) in.read();
         while (true) {
            System.out.print(ch);
            sb.append(ch);
            if (ch == lastChar) {
               if (sb.toString().endsWith(pattern)) {
                  return sb.toString();
               }
            }
            ch = (char) in.read();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

   public void write(String value) {
      try {
         out.println(value);
         out.flush();
         System.out.println(value);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }


   public static void main(String[] args) {
      try {
         AutomatedTelnetClient telnet = new AutomatedTelnetClient(
               "127.0.0.1", "username", "password");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

The "esk.txt" file has the following format:

-1.059408,54.476764
-1.056685,54.477731
-1.048233,54.479462
-1.044054,54.479968
-1.038213,54.482259
-1.034194,54.482496
-1.027904,54.480873
-1.01868,54.479361
-1.017364,54.47953

etc

Upvotes: 2

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

You can connect to your emulator via telnet like

telnet localhost 5554

then type

geo fix <longitude> <latitude> 

you can also provie NMEA sentences like

geo nmea <sentence>

perhaps not automatically, but its still enough to test the gps in your emulator.

Upvotes: 1

Related Questions