开发者

Trying to send GPS coordinates to the android emulator

I am trying to write a program that will send GPS coordinates using telnet.

I keep getting the following exception:

Exception in thread "Timer-0" java.lang.NullPointerException
at org.apache.commons.net.telnet.Telnet._sendByte(Telnet.java:1060)
at org.apache.commons.net.telnet.TelnetOutputStream.write(TelnetOutputStream.java:87)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:77)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:111)
at java.io.PrintStream.write(PrintStream.java:430)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
at java.io.PrintStream.write(PrintStream.java:477)
at java.io.PrintStream.print(PrintStream.java:619)
at java.io.PrintStream.println(PrintStream.java:756)
at com.example.myandroid.gpsSender$1.run(gpsSender.java:34)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

I don't know why I am getting this. Can you please tell me? thanks Here is my code:

package com.example.myandroid;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.Timer;
import java.util.TimerTask;

public class gpsSender {
    private TelnetClient telnet = new TelnetClient();

    public static void main(String[] args) throws Exception {
        gpsSender client = new gpsSender();
        client.start();
    }

    public String start() throws Exception {

        // Connect to the specified server

        telnet.connect("localhost", 5554);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            float longitude = 1;
            float latitude = 1;
            int count = 0;
            PrintStream out = new PrintStream(telnet.getOutputStream());

            public void run() {
                out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                out.flush();
                System.out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                longitude++;
                latitude++;
                count++;
           开发者_JAVA技巧     if (count > 1000) {
                    cancel();
                }
            }
        }, 0, 1000);
        try {
            telnet.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ("Done");
    }

    public void write(String value) {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}


The line

 telnet.disconnect();

is going to execute, so you won't have an output stream to write to, hence the NPE. You should remove that line.


Instead of using localhost, try 10.0.2.2 - that's the IP address the emulator is usually on but I'm not sure if you can establish telnet comms with the emulator.

Edit: Here's a link to telnet the emulator but it's from a command window - perhaps you could write a small batch script for your tests to send gps coordinates but if you have to syncronise this somehow with your running test, you would have to do it from the Android app. It looks like the emulator is indeed on localhost and your 'pc' is on 10.0.2.2


Of course Use the 10.0.2.2 iP and also make sure you're giving the right permissions in your manifest .. but still, if you need to input the long. and lat. to use them in another app .. you don't really need telnet and for the emulator there's a little window called "Emulator Control" where you input manually the long. and lat. but using telnet communication with the emulator, that probably doesn't happen. and on a real phone you can use the "NetwProvider.getLocation()" but of course you can't test that on an emulator as well..


Thanks for publishing this question. This approach lets me easily manipulate the hardware and the sensors of the emulator from within my Robotium tests. Here is a code snippet to set the battery charge of the emulator to 100%:

TelnetClient telnet = new TelnetClient();
telnet.connect("10.0.2.2",5554);
PrintStream out = new PrintStream(telnet.getOutputStream());
out.println("power capacity 100");
out.flush();    
telnet.disconnect();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜