Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AvilaGPSSerial;
- import java.util.Vector;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.location.Coordinates;
- import javax.microedition.location.LocationListener;
- import javax.microedition.location.LocationProvider;
- import javax.microedition.location.Criteria;
- import javax.microedition.location.Location;
- import javax.microedition.io.CommConnection;
- import javax.microedition.io.Connector;
- import javax.microedition.location.LocationException;
- import javax.microedition.location.QualifiedCoordinates;
- public class AvilaGPSSerial extends MIDlet implements CommandListener,LocationListener,Runnable {
- private final Command exitCmd = new Command("Exit", Command.EXIT, 1);
- private Command startPort = new Command("Start port", Command.SCREEN,1);
- private Command stopPort = new Command("Stop port", Command.SCREEN,1);
- private Command sendTestMessage = new Command("Send Test", Command.SCREEN,1);
- private Display display;
- private Form mainForm;
- private ChoiceGroup portChoice;
- private String currentPort;
- private byte[] msg;
- private LocationProvider provider;
- private Criteria criteria;
- private StringItem latitude;
- private StringItem longitude;
- private Thread gpsThread;
- private boolean sending = false;
- public AvilaGPSSerial() {
- }
- protected void destroyApp(boolean unconditional) {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- display = Display.getDisplay(this);
- mainForm = new Form("AvilaGPSSerial");
- mainForm.addCommand(exitCmd);
- mainForm.addCommand(startPort);
- mainForm.addCommand(sendTestMessage);
- mainForm.setCommandListener(this);
- latitude=new StringItem("Latitude:","N/A");
- longitude=new StringItem("Longitude:","N/A");
- mainForm.append(latitude);
- mainForm.append(longitude);
- fillPortChoice();
- msg = new byte[3];
- msg[1] = 123;
- display.setCurrent(mainForm);
- gpsThread = new Thread(this);
- gpsThread.start();
- }
- public void run() {
- try{
- criteria=new Criteria();
- criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
- provider = LocationProvider.getInstance(criteria);
- provider.setLocationListener(this, -1, -1, -1);
- }catch(Exception e) {
- Alert alert = new Alert("Error", "Could not retrieve location!", null, AlertType.ERROR);
- display.setCurrent(alert);
- }
- }
- public void locationUpdated(LocationProvider arg0, Location loc) {
- if(loc.isValid()) {
- QualifiedCoordinates c=loc.getQualifiedCoordinates();
- latitude.setText(String.valueOf(c.getLatitude()));
- longitude.setText(String.valueOf(c.getLongitude()));
- byte[] message = convertFloatCoordsToByteArray(c.getLatitude(),c.getLongitude());
- if(sending){
- try{
- writeBytesToSerialPort(msg,currentPort);
- } catch(IOException e){
- Alert alert = new Alert("Error", "Could not send location to serial port!", null, AlertType.ERROR);
- display.setCurrent(alert);
- }
- }
- }
- }
- public void providerStateChanged(LocationProvider provider, int newState) {
- }
- public void commandAction(Command c, Displayable d) {
- if (c == exitCmd) {
- destroyApp(false);
- notifyDestroyed();
- }
- else if(c == startPort){
- sending = true;
- int index = portChoice.getSelectedIndex();
- currentPort = portChoice.getString(index);
- mainForm.removeCommand(startPort);
- mainForm.addCommand(stopPort);
- }
- else if(c == stopPort){
- sending = false;
- int index = portChoice.getSelectedIndex();
- currentPort = portChoice.getString(index);
- mainForm.removeCommand(stopPort);
- mainForm.addCommand(startPort);
- }
- else if(c == sendTestMessage){
- byte[] msg = new byte[4];
- msg[1] = 1;
- msg[2] = 2;
- msg[3] = 120;
- try{
- writeBytesToSerialPort(msg,currentPort);
- } catch(IOException e){
- Alert alert = new Alert("Error", "Could not send location to serial port!", null, AlertType.ERROR);
- display.setCurrent(alert);
- }
- }
- }
- private String[] findPorts() {
- String portsString = System.getProperty("microedition.commports");
- Vector nodes = new Vector();
- String separator = ",";
- // Parse nodes into vector
- int index = portsString.indexOf(separator);
- while(index >= 0) {
- nodes.addElement( portsString.substring(0, index) );
- portsString = portsString.substring(index+separator.length());
- index = portsString.indexOf(separator);
- }
- // Get the last node
- nodes.addElement( portsString );
- // Create split string array
- String[] ports = new String[ nodes.size() ];
- if( nodes.size() > 0 ) {
- for(int i = 0; i < nodes.size(); i++){
- ports[i] = (String)nodes.elementAt(i);
- }
- }
- return ports;
- }
- private void writeBytesToSerialPort(byte[] message, String port) throws IOException {
- CommConnection device = (CommConnection) Connector.open(port);
- OutputStream os = device.openOutputStream();
- os.write(message);
- os.close();
- device.close();
- }
- private void fillPortChoice(){
- String[] ports = findPorts();
- portChoice = new ChoiceGroup("Port: ",ChoiceGroup.EXCLUSIVE,ports,null);
- mainForm.append(portChoice);
- }
- private byte[] convertFloatCoordsToByteArray(double latitude, double longitude){
- byte[] latArray = new byte[8];
- long lng = Double.doubleToLongBits(latitude);
- for(int i = 0; i < 8; i++){
- latArray[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
- }
- byte[] lonArray = new byte[8];
- lng = Double.doubleToLongBits(latitude);
- for(int i = 0; i < 8; i++){
- lonArray[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
- }
- byte[] result = new byte[17];
- for(int i = 1; i < latArray.length;++i)
- {
- result[i]= latArray[i];
- result[i+4]= lonArray[i];
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement