You Are Here:

Community: Developer Discussion Boards

#1 Old how to maintain connection of the gps? - 2007-10-24, 15:19

Join Date: Sep 2007
Posts: 20
hardc0d3r
Offline
Registered User
i have these simple midlet that records the coordinates in a text file. when i save the file, the gps connection terminates.. how do i maintain the connection even after it saves?

Code:
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.*;
 
public class Test extends MIDlet implements CommandListener, LocationListener {
    
    private Form form;
    private Command save;
    private Command exit;
    private FileHandler fh;
    private Thread saveThread;
    
    private String coords = "";
    
    public Test() {
    }
 
    protected void startApp() throws MIDletStateChangeException {
        buildGUI();
        getDisplay().setCurrent(form);
        connectToGPS();
        fh = new FileHandler(getDisplay());
    }
 
    protected void pauseApp() {
    }
 
    protected void destroyApp(boolean b) {
    }
    
    public void buildGUI() {
        form = new Form("Coordinates");
        
        save = new Command("Save", Command.ITEM, 1);
        exit = new Command("Exit", Command.EXIT, 1);
        
        form.addCommand(save);
        form.addCommand(exit);
        form.setCommandListener(this);
    }
    
    public void connectToGPS() {
        try {
            Criteria cr = new Criteria();
            cr.setHorizontalAccuracy(500);
 
            LocationProvider lp = LocationProvider.getInstance(cr);	
            lp.setLocationListener(this, -1, -1, -1);
            form.append("Connecting to satellite...\n");
        }
        catch (Exception e) {
            Alert alert = new Alert("Error");
            alert.setString(e.toString());
            alert.setTimeout(3000);
        }
    }
    
    public Display getDisplay() {
        return Display.getDisplay(this);
    }
 
    public void commandAction(Command cmd, Displayable disp) {
        if(cmd == save) {
            try {
                fh.setCoords(coords);
                form.deleteAll();
                coords = "";
                saveThread = new Thread(fh);
                saveThread.start();
            } catch (Exception ex) {
                Alert alert = new Alert("ERROR");
                alert.setString(ex.toString());
                alert.setTimeout(3000);
                getDisplay().setCurrent(alert, form);
            }
        }
        else if(cmd == exit) {
            destroyApp(true);
            notifyDestroyed();
        }
    }
 
    public void locationUpdated(LocationProvider locationProvider, Location location) {
        if(location.isValid()) {
            try {
                Coordinates c = location.getQualifiedCoordinates();
                if (c != null) {
                    String lat = c.getLatitude() + "";
                    String lon = c.getLongitude() + "";
                    coords = coords + lat + " | " + lon + "\n";
                    form.append(lat + " | " + lon + "\n");
                }
                else {
                }
 
            }
            catch (Exception e) {
                Alert alert = new Alert("ERROR");
                alert.setString(e.toString());
                alert.setTimeout(3000);
                getDisplay().setCurrent(alert, form);
            }
        }
    }
 
    public void providerStateChanged(LocationProvider locationProvider, int i) {
    }
}
 
class FileHandler implements Runnable {
    
    private static String mFileRoot = "file:///E:/tracks/";
    private String coords;
    private Display display;
    private String fileName;
    
    public FileHandler(Display display) {
        this.display = display;
        fileName = "track.txt";
    }
    
    public void save() throws IOException {
        FileConnection fc = null;
        DataOutputStream dos = null;
 
        try {
            // If exists already, first delete file, a little clumsy.
            StringBuffer fileURL = new StringBuffer(mFileRoot + fileName);
            fc = (FileConnection) Connector.open( fileURL.toString(), Connector.READ_WRITE);
            if (fc.exists()) {
                fc.delete();
                fc.close();
                fc = (FileConnection) Connector.open( fileURL.toString(),
                Connector.READ_WRITE);
            }
            fc.create();
            dos = new DataOutputStream(fc.openOutputStream());
            
            dos.write(coords.getBytes());
        }
        catch(Exception e) {
            Alert alert = new Alert("SAVE");
            alert.setString(e.toString());
            alert.setTimeout(3000);
            display.setCurrent(alert);
        }
        finally {
            if (dos != null) {
                dos.flush();
                dos.close();
                Alert alert = new Alert("SAVE");
                alert.setString("Saving Successfull");
                alert.setType(AlertType.INFO);
                alert.setTimeout(3000);
                display.setCurrent(alert);
            }
            if (fc != null) fc.close();
        }
    }
    
    public void setCoords(String s) {
        coords = s;
    }
 
    public void run() {
        try {
            save();
        }
        catch (IOException ex) {
        }
    }
}
and in setLocationListener(LocationListener listener, int interval, int timeout, int maxAge), sorry but i do not understand what interval, timeout and maxAge is..
Reply With Quote

#2 Old Re: how to maintain connection of the gps? - 2007-10-25, 09:47

Join Date: Apr 2007
Posts: 1,758
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
about locatiolIstener, this is an Interface u have to implement in the class definition...
the callback function of this interface

public void locationUpdated(LocationProvider locationProvider, final Location location)

will be called every specified interval in the setLocationListener(LocationListener listener, int interval, int timeout, int maxAge)...
So if u place interval 5 the locationUpdated will be called every 5 secs with a new Location... the timeout I actually never understood either but the maxAge is how old a Location may be before it gest discarded as a too old (and invalid) Location...
anyways the description of methods, parameters, callabacks is all in the API-documentation...
Reply With Quote

#3 Old Re: how to maintain connection of the gps? - 2007-10-28, 21:43

Join Date: Sep 2007
Posts: 20
hardc0d3r
Offline
Registered User
thank you sir for clearing that up! anyone knows the answer to my other question?
Reply With Quote

#4 Old Re: how to maintain connection of the gps? - 2007-10-29, 15:05

Join Date: Sep 2007
Posts: 20
hardc0d3r
Offline
Registered User
anyone? another question.. how do i know if the gps is connected?
Reply With Quote

#5 Old Re: how to maintain connection of the gps? - 2007-10-31, 09:37

Join Date: Apr 2007
Posts: 1,758
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
even though what ur asking is a pretty basic question I never under stood it either on how to know if GPS is connected...
Do u want to know if a GPS-device has been found or if the GPS-device has a fix ?
Reply With Quote

#6 Old Re: how to maintain connection of the gps? - 2008-03-03, 07:27

Join Date: Sep 2007
Posts: 20
hardc0d3r
Offline
Registered User
i am still having this problem.. can anyone help me solve it?
new question: what is i do not use the locationlsitener and get the location in a thread? is this ok?

edit:
Tiger79 - if the GPS-device has a fix
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
SPP Connection to GPS receiver - MTU problem pz1974 Bluetooth Technology 3 2006-01-19 17:02
Security Exception on a Bluetooth SPP connection between a Nokia 6230 and a GPS xavoton Bluetooth Technology 6 2005-12-19 20:12
SPP Connection to GPS receiver - MTU problem pz1974 Mobile Java Networking & Messaging & Security 0 2005-12-01 10:11
GPS - Bluetooth connection problem can_bal99 Symbian Networking & Messaging 2 2005-08-22 14:11
maintain socket connection Rx-lee Mobile Java Networking & Messaging & Security 0 2005-03-31 15:50

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia