Can't Receive My Data I Send (Bluetooth Problem)

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

Moderators: 99jonathan, roger, imaqine

Can't Receive My Data I Send (Bluetooth Problem)

Postby Hattusas » Wed Oct 17, 2007 7:02 pm

Hello.
I am trying to understand the bluetooth infrastructure of lejos API.

I have two classes; one for my PC (That is the sample program of Lejo )
the other one is running on NXT..

I want to send data to NXT from my PC.

Here is the class working on my PC.
Code: Select all
import java.io.*;
   

import lejos.pc.comm.*;

   /**
    * conneccts to a NXT using either Bluetooth or USB  and builds  input and output data streams.
    *@author  Roger Glassey  22/08/2007
    */
   
public class Connector
{
   private boolean _usb = false;
      DataInputStream dataIn;
      DataOutputStream dataOut;
      InputStream is;
      OutputStream os;
      NXTComm nxtComm;
 /**
  *
  * @param NXT can be the friendly name of the NXT or a 16 character address
  * @param useUSB
  * @return true if connection was made
  */     
      public  boolean  startConnector(String NXT, boolean useUSB)   
      {
         NXTInfo[] nxtInfo ;
         _usb = useUSB;
         if(_usb)
         {
            nxtComm = new NXTCommLibnxt();
            System.out.println("searching");
            nxtInfo = nxtComm.search(null, NXTCommFactory.USB);       
            if (nxtInfo.length == 0)
            {
               System.out.println("No NXT Found");
               return false;
            }
            nxtComm.open(nxtInfo[0]);
            System.out.println(" Opened "+nxtInfo[0].name);
         }
         else
         {
            nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
            if(NXT == null || NXT == " ")
            {
               System.out.println("search for all");
               nxtInfo = nxtComm.search(NXT, NXTCommFactory.BLUETOOTH);
            }
            else if(NXT.length()<8)
            {
               System.out.println("search for " +NXT);
               nxtInfo = nxtComm.search(NXT,  NXTCommFactory.BLUETOOTH);

            }
            else
            {     
               nxtInfo = new NXTInfo[1];
               nxtInfo[0] = new NXTInfo("unknown ",NXT);// NXT is actually address
            }
            if (nxtInfo.length == 0)
            {
               System.out.println("No NXT Found:  is BT adatper on? is NXT on? ");
               System.exit(1);
            }
            System.out.println("Connecting to " + nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress);
            boolean opened = nxtComm.open(nxtInfo[0]);       
            if (!opened) {
               System.out.println("Failed to open " +  nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress);
               System.exit(1);
            }     
            System.out.println("Connected to " + nxtInfo[0].name);
         }
         is = nxtComm.getInputStream();
         dataIn = new DataInputStream( nxtComm.getInputStream());
         os = nxtComm.getOutputStream();
         dataOut = new DataOutputStream(os);
        return true;
      }
      /**
       * @return the InputStream for this connection;
       */
      public InputStream getInputStream(){return is;}
     
      /**
       * @return the DataInputStream for this connection;
       */
      public  DataInputStream getDataIn(){return dataIn;}
     
      /**
       * @return the OutputSteram for this connection;
       */
      public OutputStream getOutputStream(){return os;}
     
   /**
    * @return the DataOutputStream for this connection
    */
      public  DataOutputStream getDataOut() {return dataOut;} 


      public static void main(String[] args)throws Exception
      {
         Connector con = new Connector();
         con.startConnector("NXT",false);
       
         
       
          while (true){
             con.dataOut.write(new Double(Math.random()*5).intValue());
             //con.os.write();
             Thread.sleep(1000);
             con.dataOut.flush();
          }
      }
     
   }





and this is the class on my NXT

Code: Select all
import java.io.DataInputStream;


import lejos.nxt.*;
import lejos.nxt.comm.*;
public class MyMaster {

   /**
    * @param args
    */
   private static BTConnection conn = null;
   private static DataInputStream dis = null;
   
   public static void main(String[] args) throws Exception {
      // TODO Auto-generated method stub
       while (true){
          if (conn==null){
            conn = Bluetooth.waitForConnection();
            if (conn!=null){
               /*Baglanti Ok!*/
                 Sound.beep();                 
                 dis = new DataInputStream(conn.openInputStream());
            }
          }
        

          
          if (dis!=null){
       
          LCD.drawInt(dis.readInt(), 4, 2 );
          Thread.sleep(1000);
          LCD.refresh();
          }
       }
   }

}


When i run those 2 class simultaneously the NXT beeps stating that BluetoothConnection is OK...
But there is no print in LCD :(
When I change the DataInputStreams to InputStreams I get peculiar data printing on LCD.
Can anyone guide what am I doing wrong?

Also how can we use the NXTSocket ? If you could send me information or code sample it will be great.Thanks a lot.
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am

Postby roger » Thu Oct 18, 2007 3:39 am

Maybe the problem is that you are writing a double to the outputstream on the PC and reading an Int from the inputstream in the NXT.
Roger
roger
Moderator
 
Posts: 363
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Postby elpy » Fri Oct 19, 2007 5:55 am

I need to do the same thing. Tried your code.. on the PC side I can't run the program but got this error:

Code: Select all
Exception in thread "main" java.lang.NoClassDefFoundError: javax/bluetooth/DiscoveryListener
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
   at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
   at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
   at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
   at lejos.pc.comm.NXTCommFactory.createNXTComm(NXTCommFactory.java:17)
   at Connector.startConnector(Connector.java:42)
   at Connector.main(Connector.java:102)


Where can I find that library? Other Java programs using the javax library are working so I don't understand what the problem is.. :/
elpy
New User
 
Posts: 4
Joined: Wed Oct 17, 2007 10:34 am

Postby Hattusas » Sat Oct 20, 2007 10:52 am

You should add bluecove.jar as classpath.
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider], Yahoo [Bot] and 2 guests

more stuff