Пример #1
0
  /* DiscoveryListener methods: */
  public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    // System.err.println(btDevice.getFriendlyName(false) + " discovered.");
    /*
    System.err.println("Major = " + cod.getMajorDeviceClass());
    System.err.println("Minor = " + cod.getMinorDeviceClass());
    System.err.println("Service = " + cod.getServiceClasses());
    System.err.println("GPS_MAJOR = " + GPS_MAJOR);
    System.err.println("Authenticated? " + btDevice.isAuthenticated());
    */

    if ((cod.getMajorDeviceClass() & GPS_MAJOR) == GPS_MAJOR) {
      if (btDevice.isAuthenticated()) { // Check if paired.
        this.btDevice = btDevice;
        da.cancelInquiry(this);
      }
    }
  }
Пример #2
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);
  }