Exemple #1
0
  /**
   * @param args
   * @throws IOException
   * @throws Throwable
   */
  public static void main(String[] args) throws IOException, Throwable {
    // TODO Auto-generated method stub
    NXTConnector conn = new NXTConnector();

    conn.addLogListener(
        new NXTCommLogListener() {

          public void logEvent(String message) {
            System.out.println("BTSend Log.listener: " + message);
          }

          public void logEvent(Throwable throwable) {
            System.out.println("BTSend Log.listener - stack trace: " + throwable.getMessage());
          }
        });
    // Connect to any NXT over Bluetooth
    boolean connected = conn.connectTo("btspp://");

    if (!connected) {
      System.err.println("Failed to connect to any NXT");
      System.exit(1);
    }

    Connection c = new Connection(conn.getInputStream(), conn.getOutputStream());
    Thread.sleep(2000);
    c.send("{\"action\":\"GET\",\"resource\":\"/\"}\n");
    System.out.println(wrap(c.waitForResponse()));
  }
 public void run() {
   NXTConnector link;
   boolean connected = false;
   link = new NXTConnector();
   while (true) {
     connected = false;
     while (!connected) {
       //				connected = link.connectTo("btspp://001653180896");
       connected = link.connectTo("btspp://");
       if (connected) System.out.println("Conntected to NXT.");
       else {
         System.out.println("Failed to connect to NXT.");
         try {
           Thread.sleep(1500);
         } catch (InterruptedException e) {
           System.out.println("Interrupted: " + e);
         }
       }
     }
     DataOutputStream dos = new DataOutputStream(link.getOutputStream());
     DataInputStream dis = new DataInputStream(link.getInputStream());
     RobotRequestMessage newMsg = null;
     while (true) {
       try {
         while (newMsg == null) newMsg = comm.getRobotRequestMessage();
         if (newMsg != null) {
           dos.writeUTF(newMsg.getXML());
           System.out.println("Forwarding client message: \n" + newMsg.getXML());
         }
         dos.flush();
       } catch (IOException e) {
         System.out.println("Failed to send message:" + e);
       }
       try {
         System.out.println("Receiving");
         String newReply = dis.readUTF();
         System.out.println(newReply);
         comm.addRobotReplyMessage(parser.parse(newReply));
       } catch (IOException e) {
         System.out.println("Failed to read message:" + e);
         try {
           link.close();
         } catch (IOException e1) {
           // TODO Auto-generated catch block
           e1.printStackTrace();
         }
         break;
       }
       if (newMsg.automatic()) automatize(dos, dis);
       newMsg = null;
     }
   }
 }
  public boolean ConnectToNXT(NXTInfo info) {
    if (m_connected) return true;

    setM_connected(m_connecter.connectTo(info.name, info.deviceAddress, NXTCommFactory.BLUETOOTH));

    if (!m_connected) return false;

    m_is = m_connecter.getInputStream();
    m_os = m_connecter.getOutputStream();

    if (m_is == null || m_os == null) {
      setM_connected(false);
    }

    if (!m_connected) return false;

    try {
      m_reader.start(); // Start to listen for incoming messages
    } catch (IllegalThreadStateException e) {
      e.printStackTrace();
    }

    return m_connected;
  }