javamacosbluetoothnxtlejos-nxj

Connect to nxt with java bluetooth


I would like to connect to my mindstorms nxt robot over bluetooth with java. Also keep in mind that I have lejos installed. Lejos includes classes to connect with the robot. Thank you so much I have spent weeks trying to figure this out and would really appreciate your help!

I would like my mac to initiate the connection.

Thank you post a comment if you don't understand what I am asking. Thank you!

The code running on the mac:

//package
package bluetoothtest;


//imports
import lejos.nxt.LCD;
import lejos.pc.comm.*;
import lejos.pc.*;


//main class
public class test {
//main method
public static void main(String[] args) throws Exception
{
//establish a connection...
NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
NXTInfo nxtInfo = new NXTInfo(NXTCommFactory.BLUETOOTH, "NXT", "00:16:53:0B:9C:CA");
}
}

code on robot:

//package
package bluetoothrobottest;

//imports
import java.io.DataInputStream;
import java.io.DataOutputStream;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.NXTConnection;

//main class
public class test {
//main method
public static void main(String[] args)
{
//wait for connection...
System.out.println("waiting for bluetooth connection...");
NXTConnection connection = Bluetooth.waitForConnection();
System.out.println("connected...");
//create streams
DataInputStream dis = connection.openDataInputStream();
DataOutputStream dos = connection.openDataOutputStream();
}
}

When I put this line of code in it gives me an error saying that the bluecove library isn't available:

nxtComm.open(nxtInfo,NXTComm.PACKET);

Solution

  • If you look in the folder you get when you download lejos from http://www.lejos.org/nxj-downloads.php, you will see a .zip-file named "samples". Extract that and go to: "samples/pcsamples/src/org/lejos/pcsample/BTSend.java" for some test code for your pc and "samples/samples/src/org/lejos/sample/btreceive.java" for some test code for your NXT. Remember to pair the two devices before you run the code, otherwise nothing will happen (The nxt's default password is: 1234).

    From what I remember the bluecove-library, which is used for this kind of communication, are no longer supported by Mac OS. Which makes it impossible to use bluetooth between a newer Mac and a NXT... But you can try it anyway.

    USB might be an alternative, it works great.