What Is ICommand in leJOS NXJ and How Do You Send Direct Commands to an NXT?
ICommand is the leJOS NXJ PC-side interface for sending direct commands to an NXT brick from a host computer over Bluetooth or USB. Use it when you want the PC to control the brick in real time — start a motor, read a sensor, play a tone — without uploading and running a Java program on the NXT itself. If your goal is to run autonomous Java code on the brick, you want the NXJ firmware and the on-brick API instead; ICommand is for the PC-driven case.
Direct commands vs. the NXJ protocol
These are two different control paths, and mixing them up is the most common source of confusion:
| ICommand (direct commands) | NXJ protocol (uploaded programs) | |
|---|---|---|
| Where the logic runs | On the PC | On the NXT (TinyVM) |
| What you send | Individual command packets | A compiled .nxj program |
| Typical use | Teleoperation, testing, PC-side scripts | Autonomous robots |
| Requires NXJ firmware? | No — works against the standard firmware | Yes |
Direct commands are the same low-level command set the standard LEGO firmware understands. That means ICommand can talk to a brick that is still running the stock firmware, which is useful for quick experiments before you commit to flashing NXJ.
Opening a connection and creating a command object
The general shape of a PC-side session is: open a transport, wrap it in a command object, issue commands, then close.
- Pick a transport.
Bluetoothfor a paired brick,USBfor a cabled connection. Both implement the same NXT connection interface, so the rest of your code is identical. - Open the connection to the brick. For Bluetooth this means the brick must be paired with the PC first; for USB the brick must be connected and the leJOS USB driver available.
- Instantiate the command object from the open connection. This is the object that exposes the direct-command methods.
- Issue commands — motor, sensor, sound, and so on.
- Close the connection when done, so the port is released for the next session.
The exact class and constructor names live in the leJOS NXJ PC API documentation on the site; check the API section there for the current signatures rather than relying on remembered names, since the API has changed across releases.
Common direct commands
Through ICommand you can reach the standard NXT command groups:
- Motor control — set forward/reverse, set speed, brake, reset the rotation counter, and read the tachometer.
- Sensor reads — poll a sensor's current value or switch it into a different sensor mode.
- Sound — play a tone with a chosen frequency and duration.
- System and status — query battery level, firmware version, and brick status; some builds also expose reset and sleep.
Because these map onto the standard firmware command set, the values you read back (tachometer counts, raw sensor values) are the same ones the stock firmware reports.
Troubleshooting
- Connection fails immediately. For Bluetooth, confirm the brick is paired and powered on; for USB, confirm the cable and driver. A brick that is already in an NXJ program may not accept direct commands until it returns to the firmware's command loop.
- Commands appear to do nothing. Check that you are addressing the right motor port or sensor port, and that the sensor is in the mode your read expects.
- Works once, then fails. You probably did not close the previous connection; the port stays busy.
- API mismatch. If a method you expect is missing, you are likely on a different leJOS NXJ release than the documentation you are reading — the PC API has been revised over time.
For the current class list and method signatures, use the leJOS NXJ API pages linked from the site's NXJ section.