RemoteControl
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTConnector;
 
public class RemoteControl {
 
    public final int FORWARD = 0;
    public final int BACKWARD = 1;
    public final int STOP = 2;
    public final int LEFT = 3;
    public final int RIGHT = 4;
    public final int REPORT = 5;
    public final int DIE = 6;
    public final int ATTACK = 7;
    public final int PLAY = 8;
    public final int LAUGH = 9;
    public final int PSYCHO = 10;
 
    private boolean connected;
 
    /**
     * connects to NXT using Bluetooth
     * 
     * @param name of NXT
     * @param address bluetooth address
     */
    public boolean connect(String name, String address) {
 
        System.out.println(" connecting to " + name + " " + address);
        con = new NXTConnector();
        boolean res = con.connectTo(name, address, NXTCommFactory.BLUETOOTH);
        System.out.println(" connect result " + res);
        connected = res;
        if (!connected) {
            return connected;
        }
        OutputStream os = con.getOutputStream();
        InputStream is = con.getInputStream();
        try // handshake
        {
            byte[] hello = new byte[] { 'R', 'C', 'P' };
            os.write(hello);
            os.flush();
            byte[] reply = { 0, 0, 0 };
            is.read(reply);
 
            for (int i = 0; i < hello.length; i++) {
                connected = connected && reply[i] == hello[i];
            }
        } catch (IOException e) {
            System.out.println("Handshake failed - is RCPilot running?");
            connected = false;
            return false;
        }
        if (!connected) {
            System.out.println("handshake failed - is RCPilot running?");
            return false;
        }
        dataIn = con.getDataIn();
        dataOut = con.getDataOut();
        if (dataIn != null) {
            System.out.println(" data In  OK");
        } else {
            connected = false;
            System.out.println(" dataIn NULL");
            return connected;
        }
        return connected;
    }
 
    public void forward() {
        send(FORWARD);
    }
 
    public void stop() {
        send(STOP);
    }
 
    public void backward() {
        send(BACKWARD);
    }
 
    public void left() {
        send(LEFT);
    }
 
    public void right() {
        send(RIGHT);
    }
 
    public void quit() {
        send(DIE);
    }
 
    public void attack() {
        send(ATTACK);
    }
 
    public void playSound() {
        send(PLAY);
    }
 
    public void laugh() {
        send(LAUGH);
    }
 
    public void psycho() {
        send(PSYCHO);
    }
 
    /**
     * send data to the reomote Pilot
     * 
     * @param code
     * @param param
     */
    private void send(int code, String param) {
        if (!connected) {
            System.out.println("Abort sending (not connected).");
            return;
        } else {
 
            try {
                dataOut.writeInt(code);
                dataOut.flush();
            } catch (IOException e) {
                System.out.println("send problem " + e);
            }
//             wait for reply
//             try {
//             _isMoving = dataIn.readBoolean();
//             _distance = dataIn.readFloat();
//             _angle = dataIn.readFloat();
//             // System.out.println(" read " + _isMoving + " " + _distance +
//             " " +
//             // _angle);
//             } catch (IOException e) {
//             System.out.println("read error " + e);
//             }
        }
    }
 
    private void send(int code) {
        send(code, "");
    }
 
//    private boolean _isMoving;
//    private float _distance;
//    private float _angle;
 
    private DataInputStream dataIn;
    /**
     * used by send()
     */
    private DataOutputStream dataOut;
    /*
     * input field for robot name or address
     */
    private NXTConnector con;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License