/** * @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 boolean connectTo(String name, String address, int protocol) { viewer.logMessage("Connecting to " + name + " " + address); con = new NXTConnector(); boolean res = con.connectTo(name, address, protocol); viewer.logMessage("Connect result " + res); if (!res) { viewer.logMessage("Connection failed "); return false; } os = con.getOutputStream(); dataIn = con.getDataIn(); if (dataIn == null) { viewer.logMessage("NULL input stream "); return false; } else { if (os == null) { viewer.logMessage("NULL output stream"); return false; } else { connected = true; } } name = con.getNXTInfo().name; address = con.getNXTInfo().deviceAddress; viewer.connectedTo(name, address); return true; }
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 void close() { try { if (con != null) con.close(); } catch (IOException ioe) { } ; }
public void close() { try { setM_connected(false); m_is.close(); m_os.close(); if (m_connecter != null) m_connecter.close(); } catch (IOException e) { e.printStackTrace(); System.err.println("Error while closing MessageFramework: " + e.getMessage()); } }
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; }