BTSend/BTReceive, can i reduce the connection delay?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

BTSend/BTReceive, can i reduce the connection delay?

Postby treeface99 » Wed May 23, 2012 11:20 am

Hi all,

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
treeface99
Novice
 
Posts: 30
Joined: Thu Nov 24, 2011 10:07 am

Re: BTSend/BTReceive, can i reduce the connection delay?

Postby skoehler » Wed May 23, 2012 12:02 pm

Scan once, keep the connection open and reuse it instead of closing it. There's no reason to close after writing a single Int.
skoehler
leJOS Team Member
 
Posts: 1387
Joined: Thu Oct 30, 2008 4:54 pm

Re: BTSend/BTReceive, can i reduce the connection delay?

Postby treeface99 » Thu May 24, 2012 12:10 pm

thanks. I have been trying to do that but am receiving null values back from the robot. Do I need to create new data streams each time or create them once and leave them open.? thanks
treeface99
Novice
 
Posts: 30
Joined: Thu Nov 24, 2011 10:07 am

Re: BTSend/BTReceive, can i reduce the connection delay?

Postby skoehler » Thu May 24, 2012 5:36 pm

treeface99 wrote:Do I need to create new data streams each time or create them once and leave them open.? thanks

Create them once, leave them open and simply then reuse them.
skoehler
leJOS Team Member
 
Posts: 1387
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff