I have created a black jack playing program that uses BTsend and BTReceive to connect to a lego nxt which performs a card scan, and return the card value back to the program. At the moment, when ever i perform a card scan, my program initiates connection every time a scan needs to be performed and this causes a delay of about 3 seconds every time.
I have modified BTSend to look like this:
- Code: Select all
public class ReadCard {
public int card;
public VoiceCom B = new VoiceCom();
public int CardRead(Interface frame){
//public static void main(String[] args) {
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.printStackTrace();
}
}
);
// 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);
}
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
try {
System.out.println("Sending " + (1));
dos.writeInt(1);
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
}
try {
card= dis.readInt();
//System.out.println("Card = " + card);
} catch (IOException ioe) {
System.out.println("IO Exception reading bytes:");
System.out.println(ioe.getMessage());
}
try {
dis.close();
dos.close();
conn.close();
} catch (IOException ioe) {
System.out.println("IOException closing connection:");
System.out.println(ioe.getMessage());
}
try {
frame.clear();
frame.displayCard(card);
//B.PlaySound(card);
setCard(card);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return card;
}
Is there a way I could modify this class further so that a single connection initiated at the beginning of the game, and card scans can be requested multiple times before the connection is closed instead of opening and closing the connection every time a scan is required? I am hoping this would reduce the delay.
I hope that makes sense.
Thanks
Will
