Beispiel #1
0
  // start server
  public void startServer() throws IOException {

    // Create a UUID for SPP
    UUID uuid = new UUID(Config.uuid, true);
    System.out.println(uuid.toString());
    // Create the servicve url
    String connectionString = "btspp://localhost:" + uuid + ";name=Bluetooth SPP Server";

    // open server url
    StreamConnectionNotifier streamConnNotifier =
        (StreamConnectionNotifier) Connector.open(connectionString);

    // Wait for client connection
    System.out.println("\nServer Started. Waiting for clients to connect...");
    while (isRunning) {
      connection = streamConnNotifier.acceptAndOpen();
      System.out.println("Connection opened");
      RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
      System.out.println("Remote device address: " + dev.getBluetoothAddress());
      System.out.println("Remote device name: " + dev.getFriendlyName(true));

      inStream = connection.openInputStream();
      outStream = connection.openOutputStream();

      if ((dev.getFriendlyName(true) == Config.pairedDevice) || true) {
        new Receiver().start();
        new Sender().start();
      }
    }
    streamConnNotifier.close();
  }
  /* 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);
      }
    }
  }
  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);
  }