I have a similar problem. I have now got the following behaviors:
- driveforward (importance 0)
- hitobject (importance 1)
- waitforconnection (importance 2)
The first two work completely and now I'm to the point that I want to let the robot constantly listen to a connection being made. I have the following code:
- Code: Select all
public class WaitForConnection implements Behavior {
private boolean suppressed = false;
private BTConnection btc = null;
public WaitForConnection(){
btc = null;
}
public void suppress() {
suppressed = true;
}
public boolean takeControl() {
btc = Bluetooth.waitForConnection();
return (btc != null);
}
public void action() {
suppressed = false;
LCD.clear();
LCD.drawString("connected", 0, 0);
LCD.refresh();
DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();
LCD.drawString("opened", 0, 1);
try {
char n = dis.readChar();
LCD.drawChar(n, 0, 2);
if (n == 'H') {
LCD.drawString("accepted", 0, 3);
dos.writeChar('A');
dos.flush();
ShakeHands();
}
while (!suppressed && !takeControl()) {
Thread.yield();
}
dos.writeChar('E');
LCD.drawString("ended", 0, 5);
dos.flush();
dis.close();
dos.close();
LCD.clear();
LCD.drawString("closing", 0, 0);
LCD.refresh();
btc.close();
LCD.clear();
} catch (IOException ioe) {
LCD.drawString(ioe.getMessage(), 0, 0);
}
}
private static void ShakeHands() {
LCD.clear();
LCD.drawString("Shake hands", 0, 4);
Sound.beepSequenceUp();
//STUB
}
}
If I run this, the robot doesn't do anything anymore and is only listening for a connection to be made. I suppose that the waitForConnection() method is always returning something that is not null, but what method or what instance variable can I use to check whether a connection is being made? I have checked the API's for Bluetooth, BTConnection and NXTConnection, but there are so many variables with minimum explanation that I don't see which one to use.