Wednesday, March 21, 2012

Bluetooth GPS simulator for J2ME phone

Bluetooth GPS simulator for J2ME phone

 

This BlueGPS application is used to simulate Bluetooth GPS receiver on J2ME phone. It will turn a bluetooth enable phone become a bluetooth GPS receiver.

If you want to develop a J2ME application for mobile phone to read NMEA sentence from a bluetooth GPS receiver, you need to have a bluetooth GPS receiver hardware.

If you do not have a bluetooth GPS receiver hardware, will it stop you from develop a GPS application for mobile phone?

I do not have bluetooth GPS receiver hardware but I want to develop a GPS application for mobile phone so I am looking for a bluetooth GPS simulator.

I spend a lot of time to search on the net. However, I could not find any bluetooth GPS simulator available anywhere. So I wrote a simple application BlueGPS that can run on any bluetooth enable phone to simulate a bluetooth GPS receiver.

I installed and ran BlueGPS on one mobile phone. Then I wrote another GPS application and ran on another mobile phone. The GPS application on the second phone used bluetooth connection to retrieve NMEA sentences from the first phone.

BlueGPS did help me a lot when I develop GPS apllication without having bluetooth GPS receiver hardware. I hope it helpful for you as well even in case you already have a bluetooth GPS receiver hardware because BlueGPS will help you to test your GPS application faster (No need to go outdoor, no need to wait for satellites fix, you can simulate any type of sentences, valid or invalid sentences, error or incomplete sentences…)

Here is the source code of BlueGPS:
/************************************************************
Description:
This BlueGPS application is used to simulate Bluetooth GPS receiver on J2ME phone. It will turn a bluetooth enable phone become a bluetooth GPS receiver.
When this BlueGPS application is running on a phone, it will wait for bluetooth connection from other bluetooth device.
When a bluetooth connection is made, BlueGPS will send NMEA sentences continuously.
Created By: ThreeSearch 2007-01-29
www.digitalmobilemap.com
*************************************************************/
/*————————————————–
* BlueGPS.java
*
*————————————————-*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;
public class BlueGPS extends MIDlet implements Runnable
{
private Display display; // The display
private TestCanvas canvas; // Canvas
LocalDevice localDevice;
ServiceRecord serviceRecord;
StreamConnectionNotifier notifier = null;
StreamConnection conn = null;
private static final UUID L2CAP_UUID = new UUID(256L); // simulate L2CAP service provided by real bluetooth GPS receiver.
private static String serverUrl = “btspp://localhost:” + L2CAP_UUID + “;authorize=false;encrypt=false”; // simulate no authorize and no encrypt required by real bluetooth GPS receiver.
OutputStream output;
InputStream input;
String[] sentence;
// Time in ms to wait until send next NMEA Sentence.
private static final long BREAK = 400;
public BlueGPS()
{
display = Display.getDisplay(this);
canvas = new TestCanvas(this);
display.setCurrent(canvas);
}
protected void startApp()
{
new Thread(this).start();
}
public void run()
{
DataInputStream in;
int i = 0, numSentence = 0;
String str;
long time;
canvas.message = “Starting BlueGPS…”;
canvas.paintScreen();
try
{
conn = null;
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
notifier = (StreamConnectionNotifier)Connector.open(serverUrl);
}
catch (BluetoothStateException e)
{
canvas.message = e.getMessage();
canvas.paintScreen();
return;
}
catch (IOException e)
{
canvas.message = e.getMessage();
canvas.paintScreen();
return;
}
numSentence = 18; // number of sentences
sentence = new String[numSentence];
// real bluetooth GPS recevier send a lot of different sentence types
// I only simulate 3 important sentence types:
// $GPGSA - GPS DOP and active satellites. This sentence provides details on the nature of the fix
// $GPGGA - Essential fix data which provide 3D location and accuracy data
// $GPRMC - Recommended Minimum Sentence C which provide essential gps pvt (position, velocity, time) data.
// you can modify these NMEA sentences as you wish
// you read more detail about NMEA sentence types here NMEA sentences
sentence[0] = “$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39″;
sentence[1] = “$GPGGA,123519,0117.349,N,10350.488,E,1,08,0.9,545.4,M,46.9,M,,*47″;
sentence[2] = “$GPGGA,123519,0117.358,N,10350.495,E,1,08,0.9,545.4,M,46.9,M,,*47″;
sentence[3] = “$GPRMC,130843,A,0117.3740,N,10350.5070,E,005.0,315.0,011206,000.0,E*6F”;
sentence[4] = “$GPRMC,130844,A,0117.3890,N,10350.5190,E,005.0,315.0,011206,000.0,E*68″;
sentence[5] = “$GPRMC,130845,A,0117.3960,N,10350.5290,E,005.0,315.0,011206,000.0,E*69″;
sentence[6] = “$GPRMC,130846,A,0117.3980,N,10350.5420,E,005.0,315.0,011206,000.0,E*6A”;
sentence[7] = “$GPRMC,130847,A,0117.3970,N,10350.5460,E,005.0,315.0,011206,000.0,E*6B”;
sentence[8] = “$GPRMC,130848,A,0117.3940,N,10350.5640,E,005.0,315.0,011206,000.0,E*64″;
sentence[9] = “$GPRMC,130849,A,0117.3710,N,10350.6080,E,005.0,315.0,011206,000.0,E*65″;
sentence[10] = “$GPRMC,130850,A,0117.3420,N,10350.6470,E,005.0,315.0,011206,000.0,E*6D”;
sentence[11] = “$GPRMC,130851,A,0117.3200,N,10350.6740,E,005.0,315.0,011206,000.0,E*62″;
sentence[12] = “$GPRMC,130852,A,0117.2900,N,10350.7060,E,005.0,315.0,011206,000.0,E*6C”;
sentence[13] = “$GPRMC,130853,A,0117.2670,N,10350.7310,E,005.0,315.0,011206,000.0,E*6D”;
sentence[14] = “$GPRMC,130854,A,0117.2510,N,10350.7620,E,005.0,315.0,011206,000.0,E*6F”;
sentence[15] = “$GPRMC,130855,A,0117.2690,N,10350.7800,E,005.0,315.0,011206,000.0,E*68″;
sentence[16] = “$GPRMC,130856,A,0117.2930,N,10350.8010,E,005.0,315.0,011206,000.0,E*69″;
sentence[17] = “$GPRMC,130857,A,0117.3170,N,10350.8210,E,005.0,315.0,011206,000.0,E*6A”;
try
{
canvas.message = “Waiting to send…”;
canvas.sentence = sentence[0];
canvas.paintScreen();
conn = notifier.acceptAndOpen();
output = conn.openOutputStream();
}
catch (IOException e)
{
canvas.message = e.getMessage();
canvas.paintScreen();
}
i = 0;
while (true)
{
try
{
canvas.message = “Waiting to send…”;
canvas.sentence = sentence[i];
canvas.paintScreen();
output.write(sentence[i].getBytes());
// simulate carry return character
output.write(13);
output.write(10);
output.flush();
}
catch (IOException e)
{
canvas.message = e.getMessage();
canvas.paintScreen();
}
try
{
Thread.sleep(BREAK); // wait for a while before send next sentence
}
catch (Exception e) {}
if (i < numSentence - 1)
i++;
else
i = 0;
} // while (true)
}
protected void pauseApp() { }
protected void destroyApp( boolean unconditional ) { }
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
/*————————————————–
* Class TestCanvas
*
*————————————————-*/
class TestCanvas extends Canvas implements CommandListener
{
private BlueGPS midlet;
public String message;
private int width, height;
String sentence;
/*————————————————–
* Constructor
*————————————————-*/
public TestCanvas(BlueGPS pmidlet)
{
midlet = pmidlet;
setFullScreenMode(true);
width = getWidth();
height = getHeight();
message = new String();
sentence = new String();
setCommandListener(this);
}
public void paintScreen()
{
repaint();
serviceRepaints();
}
/*————————————————–
* Paint canvas
*————————————————-*/
protected void paint(Graphics g)
{
g.setColor(0xCCFFCC);
g.fillRect(0, 0, width, height);
g.setColor(0×0000FF);
g.drawString(message,0,g.getFont().getHeight()*1,g.BASELINE|g.LEFT);
g.drawString(sentence,0,g.getFont().getHeight()*2,g.BASELINE|g.LEFT);
g.drawString(”Press any key to exit”,0,g.getFont().getHeight()*3,g.BASELINE|g.LEFT);
}
protected void keyPressed(int key)
{
midlet.exitMIDlet();
}
public void commandAction(Command c, Displayable d) {}
}

How to use BlueGPS:

1. You need to have 2 bluetooth enable phones.

2. Create a project BlueGPS in Sun Java Wireless Toolkit. Project’s settings as follow:

JSR082: true <– this setting to enable bluetooth API JSR082
JSR172: false
JSR177: false
JSR179: false
JSR184: false
JSR211: false
JSR75: false
MMAPI: false
WMA0: true
WMA1.1: false
WMA2.0: false
configuration: CLDC1.0
platform: CUSTOM
profile: MIDP2.0

3. Copy the Bluetooth’s source code above and save it as BlueGPS.java in src folder of BlueGPS project.

4. Open BlueGPS.java and modify NMEA sentences to latitude and longitude of your area.

5. Compile BlueGPS and generate .jar file

6. Install and run BlueGPS.jar on one mobile phone.

7. Install and run your GPS application on another mobile phone. Your GPS application need to be programmed to discover/connect/retrieve NMEA sentence from BlueGPS run on the first phone. You can read a sample program here: How to connect and retrieve NMEA sentences from bluetooth GPS

8. You can download BlueGPS to test with your GPS application from here: Download BlueGPS.rar

9. You can buy Connect Phone to GPS Source Code Templates from here: www.j2megps.com




Link: http://www.digitalmobilemap.com/bluetooth-gps-simulator-for-j2me-phone

No comments:

Post a Comment