Пример #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) {

    }
  }
Пример #2
0
  public void run() {
    System.out.println("Connecting to " + name);
    conn = Bluetooth.connect(Bluetooth.getKnownDevice(name));
    conn.setIOMode(NXTConnection.PACKET);

    dis = conn.openDataInputStream();
    dos = conn.openDataOutputStream();
    System.out.println("NXT connected");

    // Request dimensions
    try {
      dos.writeByte(3);
      dos.flush();
    } catch (IOException e1) {
    }

    try {
      while (true) {
        byte type = dis.readByte();
        switch (type) {
          case 1:
            byte slot = dis.readByte();
            byte state = dis.readByte();
            states[slot] = state;
            break;

          case 3:
            slotCount = dis.readByte();
            stateCount = dis.readByte();
            break;

          case 127:
            EStop.thread.activate = true;
            break;
        }
      }
    } catch (IOException e) {
      Sound.buzz();
      System.out.println("Comm dropped to " + name);
    }
  }