Advertisement
Guest User

Untitled

a guest
Mar 18th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 KB | None | 0 0
  1.  
  2. package AvilaGPSSerial;
  3.  
  4. import java.util.Vector;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import javax.microedition.midlet.*;
  8. import javax.microedition.lcdui.*;
  9. import javax.microedition.location.Coordinates;
  10. import javax.microedition.location.LocationListener;
  11. import javax.microedition.location.LocationProvider;
  12. import javax.microedition.location.Criteria;
  13. import javax.microedition.location.Location;
  14. import javax.microedition.io.CommConnection;
  15. import javax.microedition.io.Connector;
  16. import javax.microedition.location.LocationException;
  17. import javax.microedition.location.QualifiedCoordinates;
  18.  
  19. public class AvilaGPSSerial extends MIDlet implements CommandListener,LocationListener,Runnable {
  20.  
  21.     private final Command exitCmd = new Command("Exit", Command.EXIT, 1);
  22.     private Command startPort = new Command("Start port", Command.SCREEN,1);
  23.     private Command stopPort = new Command("Stop port", Command.SCREEN,1);
  24.     private Command sendTestMessage = new Command("Send Test", Command.SCREEN,1);
  25.     private Display display;
  26.     private Form mainForm;
  27.     private ChoiceGroup portChoice;
  28.     private String currentPort;
  29.     private byte[] msg;
  30.     private LocationProvider provider;
  31.     private Criteria criteria;
  32.     private StringItem latitude;
  33.     private StringItem longitude;
  34.     private Thread gpsThread;
  35.     private boolean sending = false;
  36.  
  37.  
  38.     public AvilaGPSSerial() {
  39.  
  40.     }
  41.  
  42.     protected void destroyApp(boolean unconditional) {
  43.  
  44.     }
  45.  
  46.     protected void pauseApp() {
  47.     }
  48.  
  49.     protected void startApp() throws MIDletStateChangeException {
  50.         display = Display.getDisplay(this);
  51.         mainForm = new Form("AvilaGPSSerial");
  52.         mainForm.addCommand(exitCmd);
  53.         mainForm.addCommand(startPort);
  54.         mainForm.addCommand(sendTestMessage);
  55.         mainForm.setCommandListener(this);
  56.  
  57.         latitude=new StringItem("Latitude:","N/A");
  58.         longitude=new StringItem("Longitude:","N/A");
  59.  
  60.         mainForm.append(latitude);
  61.         mainForm.append(longitude);
  62.  
  63.         fillPortChoice();
  64.  
  65.         msg = new byte[3];
  66.         msg[1] = 123;
  67.         display.setCurrent(mainForm);
  68.         gpsThread = new Thread(this);
  69.         gpsThread.start();
  70.     }
  71.  
  72.     public void run() {
  73.         try{
  74.             criteria=new Criteria();
  75.             criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
  76.             provider = LocationProvider.getInstance(criteria);
  77.             provider.setLocationListener(this, -1, -1, -1);
  78.         }catch(Exception e) {
  79.              Alert alert = new Alert("Error", "Could not retrieve location!", null, AlertType.ERROR);
  80.              display.setCurrent(alert);
  81.         }    
  82.     }
  83.  
  84.     public void locationUpdated(LocationProvider arg0, Location loc) {
  85.         if(loc.isValid()) {
  86.             QualifiedCoordinates c=loc.getQualifiedCoordinates();
  87.             latitude.setText(String.valueOf(c.getLatitude()));
  88.             longitude.setText(String.valueOf(c.getLongitude()));
  89.             byte[] message = convertFloatCoordsToByteArray(c.getLatitude(),c.getLongitude());
  90.             if(sending){
  91.                 try{
  92.                     writeBytesToSerialPort(msg,currentPort);
  93.                 } catch(IOException e){
  94.                     Alert alert = new Alert("Error", "Could not send location to serial port!", null, AlertType.ERROR);
  95.                     display.setCurrent(alert);
  96.                 }
  97.             }
  98.         }
  99.     }  
  100.  
  101.  
  102.     public void providerStateChanged(LocationProvider provider, int newState) {
  103.     }
  104.  
  105.     public void commandAction(Command c, Displayable d) {
  106.         if (c == exitCmd) {
  107.             destroyApp(false);
  108.             notifyDestroyed();
  109.         }
  110.         else if(c == startPort){
  111.             sending = true;
  112.             int index = portChoice.getSelectedIndex();
  113.             currentPort = portChoice.getString(index);
  114.             mainForm.removeCommand(startPort);
  115.             mainForm.addCommand(stopPort);
  116.         }
  117.         else if(c == stopPort){
  118.             sending = false;
  119.             int index = portChoice.getSelectedIndex();
  120.             currentPort = portChoice.getString(index);
  121.             mainForm.removeCommand(stopPort);
  122.             mainForm.addCommand(startPort);
  123.         }
  124.         else if(c == sendTestMessage){
  125.             byte[] msg = new byte[4];
  126.             msg[1] = 1;
  127.             msg[2] = 2;
  128.             msg[3] = 120;
  129.             try{
  130.                 writeBytesToSerialPort(msg,currentPort);
  131.             } catch(IOException e){
  132.                 Alert alert = new Alert("Error", "Could not send location to serial port!", null, AlertType.ERROR);
  133.                 display.setCurrent(alert);
  134.             }
  135.         }
  136.     }
  137.  
  138.     private String[] findPorts() {
  139.         String portsString = System.getProperty("microedition.commports");
  140.        
  141.         Vector nodes = new Vector();
  142.         String separator = ",";
  143.             // Parse nodes into vector
  144.  
  145.         int index = portsString.indexOf(separator);
  146.         while(index >= 0) {
  147.             nodes.addElement( portsString.substring(0, index) );
  148.             portsString = portsString.substring(index+separator.length());
  149.             index = portsString.indexOf(separator);
  150.         }
  151.             // Get the last node
  152.         nodes.addElement( portsString );
  153.  
  154.              // Create split string array
  155.         String[] ports = new String[ nodes.size() ];
  156.         if( nodes.size() > 0 ) {
  157.             for(int i = 0; i < nodes.size(); i++){
  158.                 ports[i] = (String)nodes.elementAt(i);
  159.             }
  160.         }
  161.         return ports;
  162.     }
  163.  
  164.     private void writeBytesToSerialPort(byte[] message, String port) throws IOException {
  165.         CommConnection device = (CommConnection) Connector.open(port);
  166.         OutputStream os = device.openOutputStream();
  167.         os.write(message);
  168.         os.close();
  169.         device.close();
  170.     }
  171.  
  172.     private void fillPortChoice(){
  173.         String[] ports = findPorts();
  174.         portChoice = new ChoiceGroup("Port: ",ChoiceGroup.EXCLUSIVE,ports,null);
  175.         mainForm.append(portChoice);
  176.     }
  177.  
  178.     private byte[] convertFloatCoordsToByteArray(double latitude, double longitude){
  179.  
  180.         byte[] latArray = new byte[8];
  181.         long lng = Double.doubleToLongBits(latitude);
  182.         for(int i = 0; i < 8; i++){
  183.              latArray[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
  184.         }
  185.  
  186.         byte[] lonArray = new byte[8];
  187.         lng = Double.doubleToLongBits(latitude);
  188.         for(int i = 0; i < 8; i++){
  189.              lonArray[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
  190.         }
  191.  
  192.         byte[] result = new byte[17];
  193.         for(int i = 1; i < latArray.length;++i)
  194.         {
  195.             result[i]= latArray[i];
  196.             result[i+4]= lonArray[i];
  197.         }
  198.         return result;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement