示例#1
0
  /** Main entry to the program. */
  public static void main(String[] args) {
    RemoteControl remote = new RemoteControl();

    RemoteDevice racecar = Bluetooth.getKnownDevice("Batmobile");

    if (racecar == null) {
      System.out.println("No Such device existed");
      System.exit(1);
    }
    BTConnection racecarConnection = Bluetooth.connect(racecar);

    if (racecarConnection == null) {
      System.out.println("Connection Failed");
      System.exit(1);
    } else {
      System.out.println("Connected to Racecar");
    }

    DataOutputStream dos = racecarConnection.openDataOutputStream();
    try {
      System.out.println("Sending controller signal");
      dos.writeInt(CONTROLLER_DEVICE);
      dos.flush();
      System.out.println("Sent Controller signal");
    } catch (Exception ex) {
      // Do nothing
    }
    remote.setRacecarConnection(racecarConnection);
    remote.setRacecarOutputStream(dos);

    // Waiting for flag to set us off here
    System.out.println("Waiting for Flag connection");
    NXTConnection flagConnection = Bluetooth.waitForConnection();
    System.out.println("Connected to flag");
    DataInputStream dis = flagConnection.openDataInputStream();
    try {
      int check = dis.readInt();
      if (check == FLAG_SIGNAL) {
        System.out.println("Recived flag signal");
        dos.writeInt(FLAG_SIGNAL);
        dos.flush();
        System.out.println("sent flag signal to racecar");
        dis.close();
        remote.run();
      } else {
        System.out.println("Did not recieve flag connection");
      }

    } catch (Exception e) {

    }
  }
  public Location getLocation(int timeout) throws LocationException, InterruptedException {
    /* TODO The timeout might play to the fact that it is still acquiring satellites?
     * I was wondering about that before. Maybe it makes sense to have timeout in SimpleGPS?
     * TODO: Solution! Keep asking for altitude until is positive? (longitude can be negative)
     * Or perhaps just until speed positive? (set those after)
     * TODO: -1 in timeout is supposed to represent the default timeout (GPSListener?)
     * TODO: I don't know if this is supposed to wait for the GPS to provide a new
     * coordinate data or if it is okay to pass the latest cached GPS coordinates.
     * Is the purpose of the timeout that it gets a new updated location that
     * is not the previously returned or cached one?
     */

    if (timeout == 0) throw new IllegalArgumentException("timeout cannot equal 0");

    // Timeout results in LocationException:
    long startTime = System.currentTimeMillis();

    // TODO: Perhaps initialize and test for NaN instead.
    while (gps.getLatitude() == 0 & gps.getLongitude() == 0) {
      if (timeout != -1 & System.currentTimeMillis() - startTime > (timeout * 1000))
        throw new LocationException("GPS timed out");
      Thread.sleep(100); /* NOTE: This might very occasionally cause an error because
			* Thread.yield() seems to cause sentence parsing to start too soon.
			* (try changing sleep() to yield() to see what happens)
			* Perhaps something needs to be synchronized? */
    }

    QualifiedCoordinates qc =
        new QualifiedCoordinates(
            gps.getLatitude(),
            gps.getLongitude(),
            gps.getAltitude(),
            (gps.getHDOP() * 6),
            (gps.getVDOP() * 6));
    Location loc =
        new Location(
            qc,
            gps.getSpeed(),
            gps.getCourse(),
            gps.getTimeStamp(),
            0,
            null); // TODO: Implement location method and extraInfo (0 and null for now)

    return loc;
  }
 /** method to disconnect from the server */
 public void disconnect() throws IOException {
   // if stream connection is present then
   if (streamConnection != null) {
     // close all streams
     dataOutputStream.close();
     dataInputStream.close();
     streamConnection.close();
   }
   // nullify all stream objects
   dataOutputStream = null;
   dataInputStream = null;
   streamConnection = null;
   // initiate garbage collector
   System.gc();
   // set the connected flag to false indicating no connection
   connected = false;
 }
示例#4
0
 public static void main(String[] args) throws Exception {
   LocalDevice device = LocalDevice.getLocalDevice();
   System.out.print(device.getFriendlyName() + " at ");
   System.out.print(device.getBluetoothAddress());
   System.exit(0);
 }