Bluetooth connection time between nxt and pc

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

Moderators: 99jonathan, roger, imaqine

Bluetooth connection time between nxt and pc

Postby Mariam » Thu Jan 12, 2012 8:52 am

Hi, I want to establish a bluetooth connection between nxt and java program ,
the problem I am facing is time it takes for connection , when I run the program multiple times, sometimes it takes only 2 seconds to connect, other times it takes up to 25 seconds.
I don't know where is the problem, and is there any factors or properties I should consider in the program to speed up the connection time ?

I am using leJOS 0.9 , and I running under windows 7, I am using netbeans jdk 1.7.0

Here is my sample code :

nxt program :
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.net.*;

public class program extends BaseClass {

public static void main(String[] args) throws Exception {
BTConnection conn = Bluetooth.waitForConnection();

Beep();// function in the BaseClass
for ( int count0 =0 ;count0< 10 ;count0++)
{
  Backward();// function in the BaseClass
   }
}
}


pc program :
Code: Select all
import lejos.pc.comm.*;
import java.io.*;
public static void main(String [] args)
   { 
       Process p ;
      try{
        p=  Runtime.getRuntime().exec("cmd.exe /c start nxj -r program");
       
       Thread.sleep(15000);
        System.out.println("trying to connect");
      NXTConnector connt = new NXTConnector();
      boolean connected = connt.connectTo("btspp://");
     if(connected)System.out.println("Connected");
     else
         System.out.println("Disconnected");
       }
       catch (Exception ex)
       {}
   }
Mariam
New User
 
Posts: 11
Joined: Tue Nov 29, 2011 8:27 am

Re: Bluetooth connection time between nxt and pc

Postby gloomyandy » Thu Jan 12, 2012 10:37 am

Hi,
One of the problems is that you are specifying connect to any NXT (by using the btspp:// syntax. This means that the PC may need to perform a search request to locate the NXT. I suspect that the difference in time is caused by the stack sometimes using cached results for the search. You will get a faster connect time if you connect to a named NXT rather than any. The fastest connection mode is if you specify the actual Bluetooth address rather than the name of the NXT, but this may not be so convenient, maybe you should try all there and see what times you get for each. Please report back the results you get...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth connection time between nxt and pc

Postby Mariam » Fri Jan 13, 2012 6:59 am

Thank you , I tried to connect using the name and using the address the result is the same in both cases. Sometimes it takes only two seconds, and other times up to 22 seconds

Using the name I wrote :
Code: Select all
  Process p ;
      try{
        p=  Runtime.getRuntime().exec("cmd.exe /c start nxj -r program");
        Thread.sleep(15000);
        System.out.println("trying to connect");
         NXTConnector connt = new NXTConnector();
      boolean connected = connt.connectTo("btspp://NXT");
        System.out.println("connected");
      }
      catch (Exception ex){}


and for connecting using the address I wrote :

Code: Select all
 Process p ;
      try{
       NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
       NXTInfo nxtInfo = new NXTInfo(2,"NXT","0016530438DE");
        p=  Runtime.getRuntime().exec("cmd.exe /c start nxj -r program");
        Thread.sleep(15000);
        System.out.println("trying to connect");
        nxtComm.open(nxtInfo);
        System.out.println("connected");
      }
      catch (Exception ex){}
         

I got the nxt info first using

Code: Select all
  NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      NXTInfo[] nxtInfo = nxtComm.search("NXT",NXTCommFactory.BLUETOOTH);

Is there a problem in the code ?

Thanks in advance
Mariam
New User
 
Posts: 11
Joined: Tue Nov 29, 2011 8:27 am

Re: Bluetooth connection time between nxt and pc

Postby gloomyandy » Fri Jan 13, 2012 8:40 am

Hi,
There is no need to use NXTInfo when using a Bluetooth address you can supply it instead of the NXT name as part of the argument to the connectTo method. So you should be able to use btspp://0016530438DE. Other than that the code looks OK. How long does nxjborowse take to connect to the NXT when you use Bluetooth? Can you work out what the pattern is as to when the connection takes a long time, versus when it takes a short time. Have you tried restarting the NXT each time (i.e. power it off and back on) before you connect does this make any difference? Does it make any difference to the connect time if you run several connections one after the other, versus say leaving a long gap (so is the Bluetooth device on your PC being powered down and what you are seeing is the time needed to power the device up)?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth connection time between nxt and pc

Postby Mariam » Sat Jan 14, 2012 5:26 pm

Hi,

nxjborowse takes always around 3 seconds, either my program takes 2 or 20 seconds to connect.

. Have you tried restarting the NXT each time (i.e. power it off and back on) before you connect does this make any difference? Does it make any difference to the connect time if you run several connections one after the other, versus say leaving a long gap

I tried this but it didn't make any difference, still in sometime takes short time, other times take long time.
When I run it for several times it can take short times for several runs , then start taking long time then short time again , etc. There is no specific pattern.
I can't figure out a pattern. I tested it for about 100 times now.

Thanks for your help.
Mariam
New User
 
Posts: 11
Joined: Tue Nov 29, 2011 8:27 am

Re: Bluetooth connection time between nxt and pc

Postby gloomyandy » Sat Jan 14, 2012 5:36 pm

Hi,
I assume that when you are testing with nxjbrowse you are using Bluetooth and not USB? If so then the obvious thing to do is to look at the source code for that and try using the same connection code in your program. Are you sure it is opening the connection that is taking the time? Perhaps you could modify your program so that it opens the connection before doing the other things (that way you can be sure that it is this that is taking the time.... Also are you running your code from the command line? If not try doing that (i.e. try and run your code in exactly the same way as you run nxjbrowse)...

Good luck

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth connection time between nxt and pc

Postby Mariam » Sun Jan 15, 2012 6:53 am

Hi,

Thanks. Finally I could figure out a pattern.

I put BTConnection conn = Bluetooth.waitForConnection(); as the first statement in nxt program
When I run the program from the brick , then the pc program only connects to it. It takes only two seconds
Code: Select all
public static void main(String [] args)
   {
      try{
   
             NXTInfo nxtInfo = new NXTInfo(2,"NXT","0016530438DE");
             NXTConnector connt = new NXTConnector();
             System.out.println("trying to connect");
            connt.connectTo(nxtInfo, NXTComm.LCP);
            System.out.println("connected");
}
catch(Exception ex){}
        }


But when I run the program from the command line first (either inside the application or not), it takes usually long time .
Code: Select all
  public static void main(String [] args)
   {
       Process p ;
      try{
   
             NXTInfo nxtInfo = new NXTInfo(2,"NXT","0016530438DE");
             NXTConnector connt = new NXTConnector();
      p=  Runtime.getRuntime().exec("cmd.exe /c start nxj -r program");
   
     Thread.sleep(15000);
        System.out.println("trying to connect");
         connt.connectTo(nxtInfo, NXTComm.LCP);
        System.out.println("connected");
      }
      catch (Exception ex){}
   }
   }


Does this make any sens ?
After the command line finish running the program it writes
"Bluecove stack shutdown completed"

Does this shutdown affects the next command which trys to open the connection again ?
so as you said above
gloomyandy wrote: (so is the Bluetooth device on your PC being powered down and what you are seeing is the time needed to power the device up)?

Is there a way to solve this ?
I don;t know if this make sense or not but I am doing is
1- generating the nxt program from the application
2- Download and run it
3- The nxt program waits for bluetooth connection
4- The pc application connects to the nxt program
and then I start receiving and sending data

so is there a way after downloading and running the program the bluetooth connection isnot closed so I can use it in my nxt program ?

Thanks again for your help :)
Mariam
New User
 
Posts: 11
Joined: Tue Nov 29, 2011 8:27 am

Re: Bluetooth connection time between nxt and pc

Postby gloomyandy » Sun Jan 15, 2012 12:17 pm

Is the program being downloaded via Bluetooth? It looks like you have a delay in your program to wait for the download to complete. Are you sure that this is long enough in all cases? If you increase that delay does that help?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth connection time between nxt and pc

Postby Mariam » Wed Jan 18, 2012 9:56 am

Hi,

Yes the program is being downloaded via Bluetooth, the download and run process doesn't exceed 7 seconds. I tried to increase the wait to 20 seconds but it doesn't help.
I don't understand why this happen. But this shows that the problem isn't in the Bluetooth connection .
My work around solution now is that I don't download the program to the NXT , and I run it remotely from my program.

Thanks alot for your time and help.
Mariam
Mariam
New User
 
Posts: 11
Joined: Tue Nov 29, 2011 8:27 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 6 guests

cron
more stuff