Olá!
Estou tentando desenvolver uma aplicação JME que para obter a coordenada do usuário. Fiz um exemplo simples onde de tanto em tanto tempo a aplicação mostra as coordenadas para o usuário.
No simulador o sistema funciona Ok. Tentei instalar num Sony Ericsson C905. Não faz diferença o Criteria definido nem a quantia de tempo informada na função getLocation(), o resultado sempre é
[ERRO E1]: Location Timeout
Alguém já conseguiu testar uma aplicação que utiliza GPS em algum celular? Sabe informar se tem que habilitar alguma coisa, acrescentar alguma biblioteca?
O código é o seguinte:
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.StringItem;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GpsMinimo extends MIDlet implements CommandListener {
Display ds_telaDisplay;
Form fr_telaInicial;
Form fr_telaResultado;
private Command cm_sair;
private Command cm_iniciar;
private boolean cancelou;
public GpsMinimo() {
// Iniciando os comandos
cm_sair = new Command("Sair", Command.EXIT, 0);
cm_iniciar = new Command("Iniciar", Command.OK, 1);
// inicia os formulários
fr_telaInicial = new Form("LOG-GPS");
fr_telaResultado = new Form("RASTREANDO");
cancelou = false;
}
protected void startApp() throws MIDletStateChangeException {
ds_telaDisplay = Display.getDisplay(this);
Command cm_sair = new Command("Sair", Command.EXIT, 0);
// subtitulo
fr_telaInicial.append("Iniciaremos a busca\n");
// adiciona comando ao formulário
fr_telaInicial.addCommand(cm_sair);
fr_telaInicial.addCommand(cm_iniciar);
// gerenciador de comandos
fr_telaInicial.setCommandListener(this);
ds_telaDisplay.setCurrent(fr_telaInicial);
}
public void commandAction(Command cmd, Displayable disp) {
try{
if (cmd == cm_sair) {
System.out.println("C: Sair" );
cancelou = true;
destroyApp(false);
notifyDestroyed();
}else if (cmd == cm_iniciar) {
System.out.println("C: Iniciar" );
fr_telaResultado.append("Iniciando");
fr_telaResultado.addCommand(cm_sair);
fr_telaResultado.addCommand(cm_iniciar);
fr_telaResultado.setCommandListener(this);
ds_telaDisplay.setCurrent(fr_telaResultado);
new Thread(new Runnable(){
public void run(){
iniciaRastreamento();
}
}).start();
}
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
private void iniciaRastreamento(){
System.out.println(" Rastreando : ");
boolean error = false;
while(!cancelou){
try {
fr_telaResultado.deleteAll();
Criteria c = new Criteria();
c.setHorizontalAccuracy(1000);
c.setVerticalAccuracy(1000);
// c.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
// c.setCostAllowed(false);
LocationProvider lp = LocationProvider.getInstance(null);
if(lp == null ) {
fr_telaResultado.append("lp == null");
error = true;
}
Location loc = lp.getLocation(20);
if(loc == null ) {
fr_telaResultado.append("loc == null");
error = true;
}
QualifiedCoordinates qc = loc.getQualifiedCoordinates();
if(qc == null ) {
fr_telaResultado.append("qc == null");
error = true;
}
if(!error){
StringItem si_Longitude = new StringItem("Longitude: ", String.valueOf(qc.getLongitude()));
StringItem si_Latitude = new StringItem("Latitude: ", String.valueOf(qc.getLatitude()));
StringItem si_Altitude = new StringItem("Altitude: ", String.valueOf(qc.getAltitude()));
fr_telaResultado.setTitle("Resultado");
fr_telaResultado.append(si_Longitude);
fr_telaResultado.append(si_Latitude);
fr_telaResultado.append(si_Altitude);
}
fr_telaResultado.addCommand(cm_sair);
fr_telaResultado.setCommandListener(this);
} catch (LocationException e) {
fr_telaResultado.append("[ERRO E1]: " + e.getMessage());
ds_telaDisplay.setCurrent(fr_telaResultado);
} catch (InterruptedException e) {
fr_telaResultado.append("[ERRO E2]: " + e.getMessage());
ds_telaDisplay.setCurrent(fr_telaResultado);
} catch (Exception e) {
fr_telaResultado.append("[ERRO E3]: " + e.getMessage());
ds_telaDisplay.setCurrent(fr_telaResultado);
}
ds_telaDisplay.setCurrent(fr_telaResultado);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(error){
break;
}
}
}
}