This project presents a leJOS class to interface with the NXTBee, along with some sample Java code in leJOS showing how to send and receive data. I’ve written extensively about the Dexter Industries NXTBee sensor before. The NXTBee provides long-range serial wireless communication for the Lego Mindstorms NXT.
In my previous project I developed a RobotC type-packing library to send and receive basic data types between NXTs and PCs using the NXTBee.
The NXTBee class wraps the serial interface to the NXTBee with high-level Java primitives. The class contains a thread that runs in the background to send and receive data to and from the NXTBee. The thread uses two circular byte buffers (one for sending, one for receiving data) to hold data until the caller is ready to process it. All of this detail is hidden behind a Java Input/Output Stream interface, which is the default Java interface for reading and writing data values.
The NXTBee class also provides a direct command control method that turns off the data reading thread and allows you to re-configure the NXTBee. For example, you can use this method to change the baud rate of the NXTBee, change the device address and so on.
The JavaDoc methods for the class is shown below.
You can download the class file, and I’ve also included three test programs to send and receive data, and to adjust the baud rate of the NXTBee. These classes are also in the latest SVN snapshot for leJOS and will be part of the next official release of leJOS.
- NXTBee.java – the core NXTBee driver class in the lejos.nxt.addon package.
- CircularByteBuffer.java – a circular byte buffer implementation from the Ostermiller java utilities.
- NXTBeeBaud.java – allows you to change the baud rate of the NXTBee sensor.
- NXTBeeSend.java – sends basic data over the NXTBee, with no error detection or checksum. You will need to build a type-packing library on top of this class.
- NXTBeeReceive.java – receive basic types sent by NXTBeeSend (or from another XBee device).