Пример #1
0
  protected BTGPSLocationProvider() throws LocationException {

    // TODO: Move this to searchConnect method?
    // TODO: The problem here is that it searches every time. Slow. Need to try Properties?
    // TODO: BIG ONE: Should only connect to GPS that isPaired() (from menu). Will
    // allow some degree of control over which GPS is connects to in classroom.
    try {
      da = LocalDevice.getLocalDevice().getDiscoveryAgent();
      da.startInquiry(DiscoveryAgent.GIAC, this);
    } catch (BluetoothStateException e) {
      throw new LocationException(e.getMessage());
    }

    while (!doneInq) {
      Thread.yield();
    }

    if (btDevice == null) throw new LocationException("No device found");

    String address = btDevice.getBluetoothAddress();
    String btaddy = "btspp://" + address;

    try {
      StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(btaddy);

      if (scn == null) throw new LocationException("Bad BT address");
      StreamConnection c = scn.acceptAndOpen();

      /* This problem below occurred one time for my Holux GPS. The solution was to
       * remove the device from the Bluetooth menu, then find and pair again.
       */
      if (c == null) throw new LocationException("Failed. Try pairing at menu again");
      InputStream in = c.openInputStream();

      if (in != null) {
        gps = new SimpleGPS(in);
        // c.close(); // TODO: Clean up when done. HOW TO HANDLE IN LOCATION?
      }
    } catch (IOException e) {
      throw new LocationException(e.getMessage());
    }
    // Add itself to SimpleGPS as listener
    SimpleGPS.addListener(this);
  }
Пример #2
0
  public void setLocationListener(
      LocationListener listener, int interval, int timeout, int maxAge) {

    // * Stop all previous listener threads *
    listenerRunning = false;
    if (listyThread != null) {
      while (listyThread.isAlive()) {
        Thread.yield();
      } // End old thread
      listyThread = null; // Discard the listener thread instance
    }

    // * Remove any listeners from GPSListener *
    if (listener == null) {
      // Remove current listener from SimpleGPS
      SimpleGPS.removeListener(gpsl);
      gpsl = null;
      return; // No listener provided, so return now so it dosn't make a new one
    }

    // * Inner classes need final variables *
    final int to = timeout;
    final LocationListener l = listener;
    final LocationProvider lp = this;
    final int delay = interval * 1000; // Oddly interval is in seconds, and not float

    // Make new thread here and start it if interval > 0, else if -1
    // then use the GPSListener interface.
    if (interval > 0) { // Notify according to interval by user
      listyThread =
          new Thread() {
            public void run() {
              while (listenerRunning) {
                try {
                  // TODO: Probably only notify if location changed? Need to compare to old.
                  // TODO: Make helper method since this is used below too.
                  l.locationUpdated(lp, lp.getLocation(to));
                  Thread.sleep(delay);
                } catch (LocationException e) {
                  // TODO Auto-generated catch block
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                }
              }
            }
          };
      listyThread.setDaemon(true); // so JVM exits if thread is still running
      listenerRunning = true;
      listyThread.start();
    } else if (interval < 0) { // If interval is -1, use default update interval
      // In our case, update as soon as new coordinates are available from GPS (via GPSListener)
      // TODO: Alternate method: Use GPSListener for ProximityListener and this.
      gpsl =
          new GPSListener() {
            public void sentenceReceived(NMEASentence sen) {
              // Check if GGASentence. Means that new location info is ready
              if (sen.getHeader().equals(GGASentence.HEADER)) {
                try {
                  // TODO: Probably only notify if location changed? Need to compare to old.
                  l.locationUpdated(lp, lp.getLocation(to));
                } catch (LocationException e) {
                  // TODO Auto-generated catch block
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                }
              }
            }
          };
      SimpleGPS.addListener(gpsl);
    }

    // TODO: Need to implement LocationListener.providerStateChanged()
  }