SENDING MESSAGE

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

SENDING MESSAGE

Postby sociocoop » Tue Jan 19, 2010 5:46 pm

Hello everybody. i'm new in lego mindstorm lejos programming. does anyone have a script for sending messages to mindstorm via bluetooth?
sociocoop
New User
 
Posts: 1
Joined: Tue Jan 19, 2010 5:45 pm

Postby burti » Tue Jan 19, 2010 11:58 pm

have a look to the sample code:
BTSend for the PC:
Code: Select all
import lejos.pc.comm.*;
import java.io.*;

/**
 * This is a PC sample. It connects to the NXT, and then
 * sends an integer and waits for a reply, 100 times.
 *
 * Compile this program with javac (not nxjc), and run it
 * with java.
 *
 * You need pccomm.jar and bluecove.jar on the CLASSPATH.
 * On Linux, you will also need bluecove-gpl.jar on the CLASSPATH.
 *
 * Run the program by:
 *
 *   java BTSend
 *
 * Your NXT should be running a sample such as BTReceive or
 * SignalTest. Run the NXT program first until it is
 * waiting for a connection, and then run the PC program.
 *
 * @author Lawrie Griffiths
 *
 */
public class BTSend {   
   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();
            
      for(int i=0;i<100;i++) {
         try {
            System.out.println("Sending " + (i*30000));
            dos.writeInt((i*30000));
            dos.flush();         
            
         } catch (IOException ioe) {
            System.out.println("IO Exception writing bytes:");
            System.out.println(ioe.getMessage());
            break;
         }
         
         try {
            System.out.println("Received " + dis.readInt());
         } catch (IOException ioe) {
            System.out.println("IO Exception reading bytes:");
            System.out.println(ioe.getMessage());
            break;
         }
      }
      
      try {
         dis.close();
         dos.close();
         conn.close();
      } catch (IOException ioe) {
         System.out.println("IOException closing connection:");
         System.out.println(ioe.getMessage());
      }
   }
}

BTReceive for the nxt:
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

/**
 * Receive data from another NXT, a PC, a phone,
 * or another bluetooth device.
 *
 * Waits for a connection, receives an int and returns
 * its negative as a reply, 100 times, and then closes
 * the connection, and waits for a new one.
 *
 * @author Lawrie Griffiths
 *
 */
public class BTReceive {

   public static void main(String [] args)  throws Exception
   {
      String connected = "Connected";
        String waiting = "Waiting...";
        String closing = "Closing...";
       
      while (true)
      {
         LCD.drawString(waiting,0,0);
         LCD.refresh();

           BTConnection btc = Bluetooth.waitForConnection();
          
         LCD.clear();
         LCD.drawString(connected,0,0);
         LCD.refresh();   

         DataInputStream dis = btc.openDataInputStream();
         DataOutputStream dos = btc.openDataOutputStream();
         
         for(int i=0;i<100;i++) {
            int n = dis.readInt();
            LCD.drawInt(n,7,0,1);
            LCD.refresh();
            dos.writeInt(-n);
            dos.flush();
         }
         
         dis.close();
         dos.close();
         Thread.sleep(100); // wait for data to drain
         LCD.clear();
         LCD.drawString(closing,0,0);
         LCD.refresh();
         btc.close();
         LCD.clear();
      }
   }
}
burti
Novice
 
Posts: 61
Joined: Thu Jun 25, 2009 11:41 pm

Postby Fieral » Sat Jan 23, 2010 1:26 am

hi
I used your code and it says:

Code: Select all
BTSend Log.listener: Failed to load Bluetooth comms driver: Cannot load Bluetooth driver
BTSend Log.listener: Failed to connect to any NXT
Failed to connect to any NXT


how can i solve this?
Fieral
New User
 
Posts: 3
Joined: Sat Jan 23, 2010 1:25 am

Postby Fieral » Sat Jan 23, 2010 1:41 am

or:
Code: Select all
Exception in thread "main" java.lang.ClassCastException: javax.microedition.io.Connector$1 cannot be cast to javax.microedition.io.StreamConnection
   at lejos.pc.comm.NXTCommBluecove.open(NXTCommBluecove.java:120)
   at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:229)
   at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:328)
   at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:337)
   at BTSend.main(BTSend.java:50)


when i use this code:
Code: Select all
 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
            NXTInfo[] nxtList = nxtComm.search("Fieral", NXTCommFactory.BLUETOOTH);
            System.out.println(nxtList.length + " device(s) found");
            for (int i = 0; i < nxtList.length; i++) {
                System.out.println("Device found: ");
                System.out.println("Name: " + nxtList[i].name);
                System.out.println("Address: " + nxtList[i].deviceAddress);
            }
            NXTInfo nxtInfo = new NXTInfo(nxtList[0]);
            nxtComm.open(nxtInfo);
...
...


it says:

Code: Select all
Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: lejos.nxt.NXT.getUserPages()I
   at lejos.nxt.NXT.getUserPages(Native Method)
   at lejos.nxt.Flash.<clinit>(Flash.java:18)
   at lejos.nxt.SystemSettings.<clinit>(SystemSettings.java:30)
   at lejos.nxt.comm.NXTCommDevice.loadSettings(NXTCommDevice.java:167)
   at lejos.nxt.comm.NXTCommDevice.<clinit>(NXTCommDevice.java:20)
   at javax.bluetooth.DiscoveryAgent$1.run(DiscoveryAgent.java:194)


any ideas how can i try to solve it?
Fieral
New User
 
Posts: 3
Joined: Sat Jan 23, 2010 1:25 am

Postby Fieral » Sat Jan 23, 2010 2:01 am

solved by using
bluecove.jar
bluecove-gpl.jar

on client side!
hoooorraayyy! :)
Fieral
New User
 
Posts: 3
Joined: Sat Jan 23, 2010 1:25 am

Postby omarrr » Sat Apr 24, 2010 12:41 pm

where did you download the bluecove.jar file and which version?
omarrr
New User
 
Posts: 2
Joined: Sat Apr 24, 2010 12:40 pm

Postby Shawn » Sat Apr 24, 2010 12:43 pm

it should be in your distribution

/lejos_nxj/3rdparty/lib
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby omarrr » Sat Apr 24, 2010 1:04 pm

thanks another question is nxt is giving java exception error which is

Java Exception :
Class : 65
Method: 117
PC : 8871

and i changed the
boolean connected = conn.connectTo("NXT"); after this it started working however, it gives the error listed above.
omarrr
New User
 
Posts: 2
Joined: Sat Apr 24, 2010 12:40 pm

Postby gloomyandy » Sat Apr 24, 2010 4:43 pm

This thread has notes on understanding the leJOS exception codes that may help you to work out what is going on...
viewtopic.php?t=1215&highlight=exception

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

Postby DoNe Marcelus » Mon Jun 21, 2010 5:36 pm

So how did you finally solved the problem of the

java.lang.ClassCastException: javax.microedition.io.Connector$1 cannot be cast to javax.microedition.io.StreamConnection

??????????
DoNe Marcelus
New User
 
Posts: 10
Joined: Mon Jun 21, 2010 4:59 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff