📄 stdgpstest.java
字号:
package com.libGPS4j2me.examples;
// J2ME
import javax.microedition.io.CommConnection;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// J2SE
import javax.bluetooth.*;
import com.libGPS4j2me.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;
// libGPS4j2me
import com.libGPS4j2me.devices.StdGPS.*;
import com.libGPS4j2me.GPS;
import com.libGPS4j2me.io.bt.*;
/**
* A simple test-program that queries the GPS for a description of itself. <br/>
* A good way to initially test the connection to the GPS. <br/>Attempts to
* turn off the GPS after retrieving the description.
*/
public class StdGPSTest extends MIDlet implements Runnable, IGPSlistener,
StdGPSListener, CommandListener
{
/*
* The url connection string
*/
private String BT_url;
/**
* The GPS unit
*/
private GPS gps;
//Define UI CMDs
private Command exitCommand;
private Command pauseCommand;
private Command resumeCommand;
// Define UIs
private Display display;
private Canvas canvas;
private Form screen;
private static final long BREAK = 3000;
private Thread runner;
private boolean isTransferComplete;
public StdGPSTest() {
// Manage CMDs
exitCommand = new Command("Exit", Command.SCREEN, 0);
pauseCommand = new Command("Pause", Command.SCREEN, 2);
resumeCommand = new Command("Resume", Command.SCREEN, 2);
// Manage UI
display = Display.getDisplay(this);
screen = new Form("Bluetooth-GPS Device Test");
screen.addCommand(exitCommand);
screen.addCommand(pauseCommand);
screen.addCommand(resumeCommand);
screen.setCommandListener(this);
isTransferComplete = false;
}
//MIDlet Life Cycle
protected void startApp() {
// start bluetooth devices search
canvas = new BTCanvas(this);
// set current UI
display.setCurrent(canvas);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
// stop this and gps unit
stop();
gps.shutdown(true);
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if(c == pauseCommand) {
stop();
}
else if(c == resumeCommand) {
stop();
start();
}
}
public void run() {
// set current UI
display.setCurrent(screen);
// get selected BT device url
BT_url = BTConnector.url;
System.out.println("Connecting to GPS...");
//connect to GPS unit by BT_url
gps = new StdGPS(BT_url);
//add listeners
((StdGPS) gps).addGPSlistener(this);
((StdGPS) gps).addStdGPSListener(this);
System.out.println("Starting GPS...");
//Start reading from GPS
((StdGPS) gps).start();
waitForResponse();
System.out.println("Waiting...");
try {
Thread.sleep(BREAK);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Shutting down...");
// Disconnect from GPS
//((StdGPS) gps).shutdown(true);
}
/** Wait for transfer completed */
private void waitForResponse() {
//wait for GPS response
System.out.println("Waiting for GPS...");
while (!isTransferComplete) {
try {
Thread.sleep(BREAK);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
isTransferComplete = false;
}
/** Start a new thread if not started yet.*/
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
/** Stop this thread. */
public void stop() {
runner = null;
((StdGPS) gps).shutdown(true);
}
/** This method will be called for each sentence received from the GPS. */
public void DataRecordReceived(DataRecord r) {
isTransferComplete = true;
System.out.println("NMEA Record data received:\n" + r.toString());
}
/** Invoked when the GPS transmits time-data.*/
public void timeReceived(ITime t) {
TimeDataRecord td = (TimeDataRecord) t;
isTransferComplete = true;
System.out.println("NMEA Time data received:\n" + td.toString());
}
/** Invoked when the GPS transmits date-data.*/
public void dateReceived(IDate d) {
TimeDataRecord td = (TimeDataRecord) d;
isTransferComplete = true;
System.out.println("NMEA Date data received:\n" + td.toString());
}
/** Invoked when the GPS transmits position-data.*/
public void positionReceived(IPosition pos) {
PositionDataRecord p = (PositionDataRecord) pos;
isTransferComplete = true;
System.out.println("NMEA Position data received:\n" + p.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -