BTcontrolledCar.java
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
 
/**
 * Receive data from a PC, a phone, 
 * or another Bluetooth device.
 * 
 * Waits for a Bluetooth connection, receives two integers
 * interpreted as power and duration for a command to a
 * differential driven car. The resulting tacho counter
 * value is send to the initiator of the connection.
 * 
 * Based on Lawrie Griffiths BTSend
 * 
 * @author Ole Caprani
 * @version 26-2-12
 */
public class BTcontrolledCar {
 
    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();
 
            LCD.clear();
            LCD.drawString(connected,0,0);
            LCD.refresh();    
 
            DataInputStream dis = btc.openDataInputStream();
            DataOutputStream dos = btc.openDataOutputStream();
 
            while ( ! Button.ESCAPE.isPressed() )
            {
                int power = dis.readInt();
                LCD.drawInt(power,7,0,1);
                LCD.refresh();
                int dur = dis.readInt();
                LCD.drawInt(dur,7,0,2);
                LCD.refresh();
                Car.forward(power, power);
                Thread.sleep(dur);
                Car.stop();
                dos.writeInt(Car.counter());
                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();
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License