请帮助我更正J2ME代码 [英] Please help me to correct th J2ME code
本文介绍了请帮助我更正J2ME代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经开发了一个j2me应用程序,它可以从内部gps读取gps坐标,并且我想将此坐标通过蓝牙发送到pc.here我创建了服务,但无法成功传输
代码
i have developed a j2me application it reads gps cordinates from the internal gps and i want to send this cordinates over bluetooth to a pc.here i have created a service but i cant sucessfully transfer the
code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
import java.rmi.Remote;
import javax.microedition.midlet.*;
/**
* @author r
*/
public class GPS extends MIDlet implements CommandListener {
Command Exit = new Command("Refresh",Command.EXIT,0);
Command send=new Command("send",Command.OK,1) ;
Form f = new Form("GPS ");
String msg;
StreamConnectionNotifier notifier;
StreamConnection conn;
LocalDevice localDevice;
ServiceRecord serviceRecord;
InputStream input;
OutputStream output;
private static final UUID BTGPSSERVER =
new UUID(0x1101);
private boolean isInit;
public void startApp() {
try
{
boolean isBTReady = false;
// create/get a local device
localDevice = LocalDevice.getLocalDevice();
// set we are discoverable
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
// Some implementations always return false, even if
// setDiscoverable successful
// throw new IOException("Can't set discoverable mode...");
}
StringBuffer serverUrl = new StringBuffer("btspp://");
// indicate this is a server
serverUrl.append("localhost").append(':');
// add the UUID to identify this service
serverUrl.append(BTGPSSERVER.toString());
// add the name for our service
serverUrl.append(";name=GPS SERVICE");
// request all of the client not to be authorized
// some devices fail on authorize=true
serverUrl.append(";authorize=false");
// create notifier now
notifier = (StreamConnectionNotifier)Connector.open(serverUrl.toString());
// and remember the service record for the later updates
serviceRecord = localDevice.getRecord(notifier);
// and remember the service record for the later updates
serviceRecord = localDevice.getRecord(notifier);
f.append("Mining gps data \n");
f.addCommand(Exit);
f.addCommand(send);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
Display.getDisplay(this).setCurrent(f);
try {
Criteria c=new Criteria();
c.setHorizontalAccuracy(1000);
c.setVerticalAccuracy(1000);
c.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
LocationProvider lp=LocationProvider.getInstance(c);
Location loc=lp.getLocation(60);
QualifiedCoordinates qc=loc.getQualifiedCoordinates();
f.append("Alt: "+qc.getAltitude());
f.append("Lat: "+qc.getLatitude());
f.append("Long: "+qc.getLongitude());
msg="GPGGA"+qc.getAltitude();
msg=msg + "$";
msg=msg+qc.getLatitude();
msg=msg+qc.getLongitude();
output = conn.openOutputStream();
output.write(msg.length()); // length is 1 byte
output.write(msg.getBytes());
output.close();
} catch(Exception e) {
f.append("Alt : 0000\n");
f.append("Lat : 0000\n" );
f.append("Long: 0000\n");
f.append("Your GPS device has this error "+e);
}
}catch(Exception e)
{
f.append("error "+e);
}
}
public void resumeMidlet()
{
}
public void pauseApp()
{this.pauseApp();
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == Exit) {
startApp();
}
if(c==send)
{
f.append("System under Construction");
}
}
}
推荐答案
"; msg = msg + qc.getLatitude(); msg = msg + qc.getLongitude(); 输出= conn.openOutputStream(); output.write(msg.length()); // 长度为1个字节 output.write(msg.getBytes()); output.close(); } 捕获(异常e){ f.append(" ); f.append(" ); f.append(" ); f.append(" + e); } } 捕获(异常e) { f.append(" + e); } } 公共 无效 resumeMidlet() { } 公共 无效 pauseApp() {此 .pauseApp(); } 公共 无效 destroyApp(布尔值无条件){ } 公共 无效 commandAction(命令c,Displayable s){ 如果(c ==退出){ startApp(); } 如果(c ==发送) { f.append(" ); } } }
"; msg=msg+qc.getLatitude(); msg=msg+qc.getLongitude(); output = conn.openOutputStream(); output.write(msg.length()); // length is 1 byte output.write(msg.getBytes()); output.close(); } catch(Exception e) { f.append("Alt : 0000\n"); f.append("Lat : 0000\n" ); f.append("Long: 0000\n"); f.append("Your GPS device has this error "+e); } }catch(Exception e) { f.append("error "+e); } } public void resumeMidlet() { } public void pauseApp() {this.pauseApp(); } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == Exit) { startApp(); } if(c==send) { f.append("System under Construction"); } } }
这篇关于请帮助我更正J2ME代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文