NXT to NXT

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: 99jonathan, roger, imaqine

NXT to NXT

Postby michcoyl » Thu Oct 17, 2013 2:07 am

I am trying to connect 2 NXts together by Bluetooth and when I run the program to test the connection I get an error message "ERno such device."

When I begin, I start by pairing the two NXTs after they are paired I load run my BTReceive program, then I run the BTSend program.

The code for the receiver:

Code: Select all

import java.io.*;
import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;


public class BTReceive
{
   public static void main(String[] args) throws Exception
   {
      LCD.clear();
      LCD.drawString("Receiver wait...", 0, 0);
      LCD.refresh();

      try
      {
         BTConnection connection = Bluetooth.waitForConnection();
         if (connection == null)
            throw new IOException("Connect fail");

         LCD.drawString("Connected.", 1, 0);
         DataInputStream input = connection.openDataInputStream();
         DataOutputStream output = connection.openDataOutputStream();

         int answer1 = input.readInt();
         LCD.drawString("1st = " + answer1, 2, 0);
         int answer2 = input.readInt();
         LCD.drawString("2nd = " + answer2, 3, 0);

         output.writeInt(0);
         output.flush();
         LCD.drawString("Sent data", 4, 0);
                  
         input.close();
         output.close();
         connection.close();
         LCD.drawString("Bye ...", 5, 0);
      }
      catch(Exception ioe)
      {
         LCD.clear();
         LCD.drawString("ERROR", 0, 0);
         LCD.drawString(ioe.getMessage(), 2, 0);
         LCD.refresh();
      }

      Thread.sleep(4000);
   }
}




The code for the sender:

Code: Select all

import java.io.*;
import javax.bluetooth.RemoteDevice;
import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;


public class BTSend
{
   public static void main(String[] args) throws Exception
   {
      // Change this to the name of your receiver
      String name = "TWO";
      
      LCD.clear();
      LCD.drawString("Connecting...", 0, 0);
      LCD.refresh();
      try
      {
         RemoteDevice receiver = Bluetooth.getKnownDevice(name);
         if (receiver != null)
            throw new IOException("no such device");
         BTConnection connection = Bluetooth.connect(receiver);
         if (connection == null)
            throw new IOException("Connect fail");

         LCD.drawString("connected.", 1, 0);
         DataInputStream input = connection.openDataInputStream();
         DataOutputStream output = connection.openDataOutputStream();

         output.writeInt(42);
         output.writeInt(-42);
         output.flush();
         LCD.drawString("Sent data", 2, 0);
         
         LCD.drawString("Waiting ...", 3, 0);
         int answer = input.readInt();
         LCD.drawString("# = " + answer, 4, 0);
         
         input.close();
         output.close();
         connection.close();
         LCD.drawString("Bye ...", 5, 0);
      }
      catch(Exception ioe)
      {
         LCD.clear();
         LCD.drawString("ERROR", 0, 0);
         LCD.drawString(ioe.getMessage(), 2, 0);
         LCD.refresh();
      }
      Thread.sleep(4000);
   }
}




P.S. After I get the two NXts paired it says "Press any key" Is that any button on the NXT or back in Eclipse?

Mike
michcoyl
New User
 
Posts: 2
Joined: Tue Sep 17, 2013 9:47 pm

Re: NXT to NXT

Postby javareds » Mon Dec 02, 2013 2:20 pm

I got 2 nxt's to communicate modifing your code. I left a few things commented out that I didn't use, but this should work for you.

Code: Select all
package Bluetooth1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import lejos.nxt.LCD;
import lejos.nxt.comm.*;


// this is j's slave the receiver
//import lejos.nxt.comm.BTConnection;
//import lejos.nxt.comm.NXTCommLogListener;

public class Bluetooth1 {
    public static void main(String[] args) throws Exception {
         LCD.clear();
         LCD.drawString("Receiver wait...", 0, 0);
         LCD.refresh();
      try{

          BTConnection connection = Bluetooth.waitForConnection();
      
   
            if (connection == null)
               throw new IOException("Connect fail");
            
            LCD.drawString("Connected.", 1, 1);
            DataInputStream input = connection.openDataInputStream();
            //DataOutputStream output = connection.openDataOutputStream();

            int answer1 = input.readInt();
            LCD.drawString("1st = " + answer1, 2,2);
         //   int answer2 = input.readInt();
          //  LCD.drawString("2nd = " + answer2, 3, 0);

           // output.writeInt(0);
          //  output.flush();
         //   LCD.drawString("Sent data", 4, 0);
                    
            input.close();
          //  output.close();
            connection.close();
            LCD.drawString("Bye ...", 5,5);
            Thread.sleep(4000);
      }
      catch(Exception ioe)
         {
            LCD.clear();
            LCD.drawString("ERROR", 0, 0);
            LCD.drawString(ioe.getMessage(), 2, 0);
            LCD.refresh();
         }
      
   
    }
   

}

Code: Select all
package Bluetooth3;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import javax.bluetooth.RemoteDevice;

import lejos.nxt.LCD;
import lejos.nxt.comm.*;

//this is j the sender

//import lejos.nxt.comm.BTConnection;
//import lejos.nxt.comm.NXTCommLogListener;

public class Bluetooth3 {
    public static void main(String[] args) throws Exception {
         LCD.clear();
         LCD.drawString("Sender wait...", 0, 0);
         LCD.refresh();
         String name = "J's Slave";
        
        
        
      try{

          BTConnection connection = Bluetooth.connect(name, 0);
      
   
            if (connection == null)
               throw new IOException("Connect fail");
            
            LCD.drawString("Connected.", 1, 1);
           // DataInputStream input = connection.openDataInputStream();
            DataOutputStream output = connection.openDataOutputStream();
            output.writeInt(0);
            
           // int answer1 = input.readInt();
          //  LCD.drawString("1st = " + answer1, 2, 0);
           // int answer2 = input.readInt();
            //LCD.drawString("2nd = " + answer2, 3, 0);

            
            output.flush();
            LCD.drawString("Sent data", 4, 2);
                    
          //  input.close();
            output.close();
            connection.close();
            LCD.drawString("Bye ...", 5, 0);
            Thread.sleep(4000);
      }
      catch(Exception ioe)
         {
            LCD.clear();
            LCD.drawString("ERROR", 0, 0);
            LCD.drawString(ioe.getMessage(), 2, 0);
            LCD.refresh();
         }
      
   
    }
   

}
javareds
New User
 
Posts: 3
Joined: Mon Mar 18, 2013 1:15 pm

Re: NXT to NXT

Postby michcoyl » Sat Dec 07, 2013 5:34 pm

Hopefully this works out for me. Thanks.
michcoyl
New User
 
Posts: 2
Joined: Tue Sep 17, 2013 9:47 pm


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff