Connecting LeJos and Android

I have been working on getting an android phone to connect to the Lego NXT Brick and, so far have encountered some interesting problems. Essentially, leJOS (the Lego programming extension to Java) provides Bluetooth functionality for connecting the robot to either a PC or another NXT. Additionally, Android provides an API for connecting to Bluetooth devices. The trick is to get these two different API’s to work together. This gives me an interesting chance to learn about how devices connect to each other and implement some low-level solutions in Java, which is typically a higher level language.

Some of the problems I have encountered so far have been hardware dependent. Android support for Bluetooth varies from phone to phone and HTC phones like the one I am using are notoriously uncooperative. I have, however, found a work around which gets some sample programs from Lego to run.

I have also had to deal with various communications protocols. Lego has its own protocol called the Lego Communications Protocol which provides lots of useful functionality. For example, motors and sensors can be directly controlled using a program running outside the robot. This is very efficient compared to other programs widely in use which communicate between the robot and the phone by dumping data into and out of streams. While this still uses LCP, it ignores the advantages of using such a communication protocol.

Basically, LCP acts in a way similar to other communication┬áprotocols whereby devices know who is going to send the next message to whom. This means there is less switching of Bluetooth nodes between sending and receiving modes (which takes time and energy). This does, however, have some disadvantages. Since the Lego device has a very limited amount of space, it often discards old (sometimes uncompleted) messages if too many come in too quickly. Unfortunately, there is no way to tell how many uncompleted messages are sitting on the device remotely, so it is sometimes difficult to know if instructions were completed. Meanwhile, the data stream method requires the use of polling, a procedure which constantly checks for new information. This is bad for lots of reasons, most notably because it requires both devices to be processing even while they are “inactive”. It also eats up resources while other processes are happening. This solution is much easier to implement on Android, however, since it does not require translating LPC codes to the phone. Currently, Lego has not moved all of the LCP functionality supported on the computer to Android.

Currently, I am working on moving LCP support to Android. Even though I have sample code which does this, it is poorly abstracted and often difficult to remove from other functionality of the program. Additionally, it seems that the file configurations are somewhat tricky. Debugging those programs shows that much of the code is called from internal leJOS libraries, so I need to make sure that my project incorporates the modified version of these libraries. However, it is good to know that such a task can be done and will ultimately result in me being able to control the robot remotely.

I have also decided to keep track of how to develop my application (and solve the problems I have encountered) on a wiki called “androidgoeslego” which I do not think is publicly viewable yet. I think it will be very helpful to anyone who attempts to continue developing Android-Lego applications in the future. Currently, there seem to be only two developers who have been active in sharing information about how to get these platforms to work together. While they participate in forums and publish sample code, they have done little to compile a “how-to” for developers and I hope that this wiki will fill that role.