Bluetooth connection problem

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

Moderators: 99jonathan, roger, imaqine

Bluetooth connection problem

Postby courage » Thu Oct 27, 2011 5:12 pm

Hi there!

I have no problem with USB connection but have with Bluetooth with the same code.
I tried out my bluetooth connection with nxjcontrol.bat and it worked prefectly.
So there is no hardware/pairing problem.

here is my code:

Code: Select all

    private static NXTInfo[] nxts ;
    private static NXTConnector conn = new NXTConnector();
    private static NXTComm nxtCon;
    private static NXTCommand nxtCommand;

    /*  ...............   */

try{
            nxtCon = NXTCommFactory.createNXTComm(NXTCommFactory.ALL_PROTOCOLS);
        }catch(Exception e){
         System.out.println("Error: createNXTComm");
        }

        //nxts = conn.search("NXT","0016530EE4AB", NXTCommFactory.ALL_PROTOCOLS);
       
        nxts = conn.search("", null, NXTCommFactory.ALL_PROTOCOLS);
           
        if (nxts.length == 0) {
            System.out.println("No NXTS found!");

        } else {
            System.out.println("NXTS found!");
        }



I tried both search functions but the results were the same.
They worked perfectly via USB, but did not via Bluetooth.

I'm using Netbeans with GUI, so I cannot use nxjpcc.

I think the createNXTComm is that causes the problem but I'm not certain.

Any ideas?
courage
New User
 
Posts: 4
Joined: Thu Oct 27, 2011 4:54 pm

Re: Bluetooth connection problem

Postby gloomyandy » Thu Oct 27, 2011 5:42 pm

If all you want to do is to connect, why use the search options at all just connect directly using one of the connectTo calls from this class...
http://lejos.sourceforge.net/nxt/pc/api ... ector.html

Other than that you may want to find and delete any comms cache files (nxj.cache) on your system...

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

Re: Bluetooth connection problem

Postby courage » Wed Nov 02, 2011 7:32 pm

Hi Andy!

If I use the conn.connectTo("NXT","0016530EE4AB", NXTCommFactory.ALL_PROTOCOLS) or any combination of connectTo parameters, it throws java.lang.NullPointerException.

The only way it works is nxts = conn.search("", null, NXTCommFactory.ALL_PROTOCOLS). (via USB of course).
If I change ALL_PROTOCOLS to BLUETOOTH it won't find the NXT. :(

I've tried to delete the cache.nxj files. I've tried to replace the pccomm.jar with a new one but nothing happened.

Got any clue what's going on?

Thanks also in advance.
courage
New User
 
Posts: 4
Joined: Thu Oct 27, 2011 4:54 pm

Re: Bluetooth connection problem

Postby gloomyandy » Wed Nov 02, 2011 11:00 pm

What is the stack trace when you get the NullPointerException. What version of leJOS are you using? What platform are you running on?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4240
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth connection problem

Postby courage » Fri Nov 04, 2011 8:57 pm

I'm using 0.9.0. I have tried out on win xp 32 bit and win 7 64 bit.
What is a stack trace?

Here is my code:

Code: Select all
package desktopapplication4;



import lejos.pc.comm.*;
import lejos.nxt.*;
import lejos.nxt.remote.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import lejos.pc.comm.NXTConnector.*;
import java.lang.Object.*;
import lejos.nxt.Motor.*;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class DesktopApplication4 extends SingleFrameApplication {

    private static NXTInfo[] nxts ;
    private static NXTConnector conn = new NXTConnector();
    private static NXTComm nxtCon;
    private static NXTCommand nxtCommand;
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new DesktopApplication4View(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of DesktopApplication4
     */
    public static DesktopApplication4 getApplication() {
        return Application.getInstance(DesktopApplication4.class);
    }

   
    public static void ctrll()
    {
        /*try{
            nxtCon = NXTCommFactory.createNXTComm(NXTCommFactory.ALL_PROTOCOLS);
        }catch(Exception e){
         System.out.println("Error: createNXTComm");
        }*/


        try{
            nxts = conn.search("NXT","0016530EE4AB", NXTCommFactory.ALL_PROTOCOLS);
        }catch(Exception e){
         System.out.println("Error: search by name and address");
        }
       
       
        /*try{
            conn.connectTo("NXT","0016530EE4AB", NXTCommFactory.ALL_PROTOCOLS);
        }catch(Exception e){
         System.out.println("Error: connect by name and address");
        }*/
       
        try{
            conn.connectTo("","", NXTCommFactory.ALL_PROTOCOLS);
        }catch(Exception e){
         System.out.println("Error: connect to anything that is possible");
        }
       
        nxtCommand = NXTCommand.getSingleton();
        nxtCommand.setNXTComm(nxtCon);
       
        String deviceName = "" ;

        try{
            deviceName = nxtCommand.getFriendlyName() ;
        }catch(Exception e){

        }

        System.out.println("Device name: " + deviceName);
        Sound.playTone(440, 1000);
        try {
            if (nxtCommand == null)    return;
            nxtCommand.setOutputState(0, (byte) 2, 0, 0, 0, 0, 0);
            //nxtCommand.setOutputState(1, (byte) 2, 0, 0, 0, 0, 0);
            nxtCommand.setOutputState(2, (byte) 2, 0, 0, 0, 0, 0);

            //tachos[0].setText("      " + nxtCommand.getTachoCount(0));
            //tachos[1].setText("      " + nxtCommand.getTachoCount(1));
            //tachos[2].setText("      " + nxtCommand.getTachoCount(2));
        } catch (IOException ioe) {
            //showMessage("IOException while stopping motors");
        }
    }
   
    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(DesktopApplication4.class, args);
        ctrll();
    }
}


I've tried to move the motors and play a sound, but I just got this error message:

Code: Select all
run:
BlueCove version 2.1.0 on winsock

Device name:
Exception in thread "main" java.lang.NullPointerException
   at lejos.nxt.remote.NXTCommand.sendRequest(NXTCommand.java:65)
   at lejos.nxt.remote.NXTCommand.playTone(NXTCommand.java:740)
   at lejos.nxt.Sound.playTone(Sound.java:23)
   at desktopapplication4.DesktopApplication4.ctrll(DesktopApplication4.java:121)
   at desktopapplication4.DesktopApplication4.main(DesktopApplication4.java:141)
BlueCove stack shutdown completed
courage
New User
 
Posts: 4
Joined: Thu Oct 27, 2011 4:54 pm

Re: Bluetooth connection problem

Postby courage » Fri Nov 04, 2011 9:09 pm

Oh and I'm using Java JDK 1.7 platform.
I included bluecove.jar, jtools.jar, pccomm.jar, pctools.jar libs to my project.
courage
New User
 
Posts: 4
Joined: Thu Oct 27, 2011 4:54 pm

Re: Bluetooth connection problem

Postby gloomyandy » Sun Nov 13, 2011 5:00 pm

You need to take a step back. Try testing things out using the leJOS sample code then make things more complex slowly from there. Try running the TachoCount sample supplied in the PcSamples directory as part of the leJOS install. It should get you started on connecting to the NXT and using the remote API....

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


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff