Bluetooth problems.

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Bluetooth problems.

Postby ciborro » Sun Sep 16, 2012 10:41 am

Hello everybody,
Finally I found some time to play with my NXT set, and to learn some java.
I choice lejos as best solution for this.

Hearing a lot of informations of Curiosity rover, I decided to build my own room rover, instead of mars rover;)
The first issue is communication between NXT brick and my PC. Of course it must be wireless, so Bluetooth is the only option.
I paired NXT with my PC, everythink seems to be working, I can upload program over BT. But, when I checking paired devices on NXT, the list is empty - it's really strange.

Is it a normal behavior?

I'm not sure, but I suppose that it can be connected with my second problem.

I tried many times to send data between NXT and PC, and all my trial fails.
I enclose source code below.

NXT CODE:
Code: Select all
import java.io.DataInputStream;
import java.io.IOException;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;

public class Start {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      LCD.clear();
      LCD.drawString("Waiting...", 0, 0);
      BTConnection btc = Bluetooth.waitForConnection();
      LCD.drawString("Connected...", 0, 0);
      
      DataInputStream iStream = btc.openDataInputStream();
      if(iStream != null)
         {
         LCD.drawString("W4 Data...", 0, 0);
         
         int DataFromMC = 0;
         int counter = 1;
         
         while(DataFromMC != -1)
         {
            LCD.drawInt(DataFromMC, 0, counter);
            LCD.refresh();
            counter++;
            if(counter > 3) counter = 1;
            try {
               LCD.drawString("Before read", 0, 0);
               DataFromMC = iStream.readInt();
               LCD.drawString("After read", 0, 0);
            } catch (IOException ioe)
            {
               LCD.drawString("Read excepion", 0, 0);
            }
         }
         LCD.drawString("Disconnecting", 0, 0);
         
         try {
            iStream.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
      else
      {
         LCD.drawString("CCStream", 0, 0);
      }
      btc.close();
      LCD.drawString("Finishing;)...", 0, 0);
      while(Button.ENTER.isUp());
   }
}


PC CODE:
Code: Select all

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import lejos.pc.comm.NXTConnector;

public class Start {

   /**
    * @param args
    */
   
   
   public static void main(String[] args) {
      // TODO Temporary for test
      
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      NXTConnector conn = new NXTConnector();
      
      System.out.println("Connecting");
      
      if(conn.connectTo())
      {
         System.out.println("Output stream creating");
         DataOutputStream oStream = new DataOutputStream(conn.getOutputStream());
         System.out.println("Type integer (-1) for end");
         int x = 0;
         while(x != -1)
         {
            try {
               x = Integer.parseInt(br.readLine());
            } catch (NumberFormatException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            } catch (IOException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
            try {
               oStream.writeInt(12);
            } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }   
            //oStream.flush();
         }         
         try {
            conn.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}



Behavior:

After program runs, I see "Waiting".
After PC part runs, I see "Before read" and 0 i next line on NXT,
and,
Connecting
BlueCove version 2.1.0 on winsock
Output stream creating
Type integer (-1) for end
on PC.

When i type something on PC, next line appears and I can type next number, and so on.
Unfortunately on NXT i see always the same "Before read" and 0 in second line.

Do you have any ideas?

Some information's:

PC with Windows 7 32bit
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
lejos 0.9.1 (rev.6595)


Regards
Przemek.
ciborro
New User
 
Posts: 5
Joined: Tue Dec 20, 2011 11:00 am

Re: Bluetooth problems.

Postby gloomyandy » Sun Sep 16, 2012 4:04 pm

Hi,
Firstly you must flush the output stream on the PC, otherwise you will see noting for a long time. Secondly take care when displaying output data on the NXT LCD screen, so for instance it is very unlikely you will ever see the "After read" message as it will almost immediately be over written by the "Before read" message. You may want to consider using the remote console API via a USB connection to allow you to display debug trace data on the PC. This makes it much easier to work out what is going on. Also I assume that you have changed the data you are sending to be a fixed value as part of your debug process?. Finally if you are still struggling with this you should probably try the leJOS sample Bluetooth programs and make sure that they work. That way you can be sure that Bluetooth comms between your NXT and PC are working correctly.

Good luck

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

Re: Bluetooth problems.

Postby ciborro » Sun Sep 16, 2012 4:36 pm

Hi Andy,
Adding "oStream.flush()" resolved my problem:) Everything works like a charm!
I tried to add flush once, but something other was wrong.

Thank you a lot!!!

Regards.
Przemek.
ciborro
New User
 
Posts: 5
Joined: Tue Dec 20, 2011 11:00 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff