Friday, May 2, 2008

GPS device emulation how to

I do not have a GPS device. Though I dare to write applications for it. Somehow I have managed a way to emulate a GPS device.

So, what is the source of sample GPS data ? I use public gpsd servers as NMEA data source ..
You can do google to find a gpsd server.

How can we emulate ? I use blueproxy to proxy the gpsd server connection over bluetooth. So you just need to download and install blueproxy.

Finally it is simple, start your server with the following command,

shell #> blueproxy -v -c 11 -h gpsd_server.address -p gpsd_port

Please replace the "gpsd_server.address" and "gpsd_port" with something appropriate. Note that gpsd servers needs to receive R before it can start to broadcast NMEA messages. So just send 'R\r\n' before you wait for data.

As I am midlet developer, I tried the idea of proxy in midlet too .. Finally I was able to test the whole thing in WTK emulator.

Here is the code,(HTML generated by highlight 2.6.9, http://www.andre-simon.de/)

package blueProxy;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import java.io.OutputStream;
import java.io.InputStream;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.DiscoveryAgent;

public class BlueProxy extends MIDlet implements CommandListener {
// UI Specific
private final TextField url = new TextField("URL", "socket://gpsd.mainframe.cx:2947", 200, TextField.ANY);
private final StringItem log = new StringItem("Log", "no clients are connected ..");
private final Command quit = new Command("Quit", Command.EXIT, 1);
private final Command start = new Command("Start", Command.OK, 1);

public void startApp() {
// test ui
Form myForm = new Form("Blue Proxy");
myForm.append(url);
myForm.append(log);
myForm.addCommand(quit);
myForm.addCommand(start);
myForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(myForm);
}

public void pauseApp() {
}

/**
* Destroy must cleanup everything. The thread is signaled to stop and no
* result is produced.
*/
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}

public void commandAction(Command cmd, Displayable arg1) {
if(cmd == start) {
((Form)arg1).removeCommand(start);
new Thread() {
public void run() {
System.out.println("Started thread#1");
LocalDevice ld = null;
try {
ld = LocalDevice.getLocalDevice();
ld.setDiscoverable(DiscoveryAgent.GIAC);
} catch( BluetoothStateException e) {
e.printStackTrace();
return;
}
try {
// open server socket ..
log.setText("opening connection notifier ..");
StreamConnectionNotifier connn = (StreamConnectionNotifier) Connector.open("btspp://localhost:1;encrypt=false;authorize=false;authenticate=false");
while(true) {
log.setText("waiting for a new client ..");
try {
final StreamConnection conn = (StreamConnection)connn.acceptAndOpen();
// open client socket ..
log.setText("opening socket to " + url.getString());
final StreamConnection client = (StreamConnection) Connector.open(url.getString());
// peering ..
new Thread() {
public void run() {
System.out.println("Started thread#2");
try {
InputStream is = conn.openInputStream();
OutputStream os = client.openOutputStream();
byte data[] = new byte[200];
int res = 0;
while((res = is.read(data)) != 0) {
System.out.println("bt > " + new String(data, 0, res));
os.write(data, 0, res);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}.start();
InputStream is = client.openInputStream();
OutputStream os = conn.openOutputStream();
byte data[] = new byte[200];
int res = 0;
while((res = is.read(data)) != 0) {
System.out.println("bt < " + new String(data, 0, res));
os.write(data, 0, res);
}
} catch(IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} else {
destroyApp(true);
}


}
}

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!