Remote Controller Via Bluetooth

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

Moderators: 99jonathan, roger, imaqine

Remote Controller Via Bluetooth

Postby Hattusas » Tue Oct 23, 2007 5:57 pm

Hi lejo fans.
I am dealing with my robot car and having extremely joy out of it.

There is a problem I need to solve; hope you can help/guide me.

I have constructed an applet that is to send commands to my NXT.
NXt will receive this commands (which are integer lets say 1 for MotorA.forward(), 2 MotorA.backward(),3. MotorB.forward() etc.)

I start my NXT then my applet on PC. Bluetooth connection is established and I am successfully sending my "first" command to my robot car. But my others commands are not received by my NXT.

Can anyone help me where am I doing wrong ? Thanx so much.

Here are the code snippets of PC and NXT .

PC :

Code: Select all
public void baglan() throws Exception{
   BAGLANTI_SAGLANDI=startConnector(NXT_ADDRESS, false);
   ivjPanel.setEnabled(BAGLANTI_SAGLANDI);
      InputStream is = nxtComm.getInputStream();
      OutputStream os = nxtComm.getOutputStream();
      
   
}

BAGLANTI_SAGLANDI is a boolean variable that means connection is established.
startConnector(String , boolean) is nearly the same as the sample BTSend example.
Code: Select all
     public  boolean  startConnector(String NXT, boolean useUSB) throws Exception
     {
        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);

        }

       return true;
     }



THOSE METHODS ARE ONLY CALLED ONCE!

And here is the code that works on my NXT
Code: Select all
   public static void main(String [] args)  throws Exception
   {
      String connected = "Connected";
        String waiting = "Waiting";
       
          motorA = new Motor(MotorPort.A);
          motorB = new Motor(MotorPort.B);

      while (true)
      {
         LCD.clear();   
         LCD.drawString(waiting,0,0);
         LCD.refresh();
         
            btc = Bluetooth.waitForConnection();
          
         LCD.clear();
         LCD.drawString(connected,0,0);
         LCD.refresh();   
         if (is==null){
          is = btc.openInputStream();
          os = btc.openOutputStream();
         }
          dis = new DataInputStream(is);
          dos = new DataOutputStream(os);
         

            int ii = dis.readInt();
            LCD.drawInt(ii,3,0,1);
            LCD.refresh();            
            if (ii==KOMUT_GERI)
               motorA.backward();
            else if (ii==KOMUT_ILERI)
               motorA.forward();
            else if (ii==KOMUT_SAG)
               motorB.backward();
            else if (ii==KOMUT_SOL)
               motorB.forward();
            else{
               motorA.stop();
               motorB.stop();
            }   
         
         dis=null;
         dos=null;
         //btc.close();
         //LCD.clear();
         
      }
   }



Thank you for bothering me.
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am

Postby Hattusas » Tue Oct 23, 2007 6:22 pm

oh dear :)

if (btc==null )
btc = Bluetooth.waitForConnection();

it works better now :wink:
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am

Postby elpy » Wed Oct 24, 2007 10:50 am

I'm trying to do the same thing.. I can't get your code to work.. :/ Did you manage to get it working?
elpy
New User
 
Posts: 4
Joined: Wed Oct 17, 2007 10:34 am


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 3 guests

more stuff