/** 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) { } }
// Main thread public void run() { String stringBuffer = ""; byte[] byteBuffer = new byte[1024]; connection = Bluetooth.waitForConnection(); connected = true; is = connection.openDataInputStream(); os = connection.openDataOutputStream(); terminateFlag = false; while (!terminateFlag) { // read into byte[] buffer int bytesRead; try { bytesRead = is.read(byteBuffer); } catch (IOException e) { bytesRead = 0; } if (bytesRead > 0) { // transfer from byte[] into StringBuffer stringBuffer += new String(byteBuffer, 0, bytesRead); // check for } // if found, this suggests that we just finished receiving a full message int endChar = stringBuffer.indexOf("}"); if (endChar != -1) { // check for matching { int startChar = stringBuffer.indexOf("{"); if (startChar != -1 && startChar < endChar) { // parse the message and add it to the queue Message messageRead = new Message(stringBuffer.substring(startChar, endChar + 1)); messageQueue.push(messageRead); Message ack = new Message(messageRead.getSeqNum()); ack.pairs.add(new String[] {"ack", null}); sendMessage(ack); } // clean command up to } off stringBuffer if (endChar == stringBuffer.length() - 1) { stringBuffer = ""; } else { stringBuffer = stringBuffer.substring(endChar + 1); } } } } // disconnect try { is.close(); os.close(); } catch (IOException e) { // TODO: not fail silently } connection.close(); connected = false; }
public static void main(String[] args) throws Exception { String connected = "Connected"; String waiting = "Waiting..."; String closing = "Closing..."; while (true) { LCD.drawString(waiting, 0, 0); LCD.refresh(); // BTConnection btc = Bluetooth.waitForConnection(); BTConnection btc = Bluetooth.waitForConnection(0, NXTConnection.RAW); LCD.clear(); LCD.drawString(connected, 0, 0); LCD.refresh(); DataInputStream dis = btc.openDataInputStream(); DataOutputStream dos = btc.openDataOutputStream(); for (int i = 0; i < 100; i++) { int n = dis.readInt(); LCD.drawInt(n, 7, 0, 1); LCD.refresh(); dos.writeInt(-n); dos.flush(); } dis.close(); dos.close(); Thread.sleep(100); // wait for data to drain LCD.clear(); LCD.drawString(closing, 0, 0); LCD.refresh(); btc.close(); LCD.clear(); } }