NXT-Spy – First successful connection between the phone and the robot

After a lot of work and research on how to send and receive data over Bluetooth on Android and LeJOS, I finally managed to send a “Hello robot” message from the phone to the robot.

The main problem was that the LeJOS’s PC API is not compatible with Android (it is based on Bluecove). To solve this problem, I took a look at the few examples available on Android’s website to understand how to initiate a Bluetooth connection with an other Bluetooth equipment [source]. But event if these examples helped me to start coding my Android application, it also lead me to a lot of problems as the examples only involved Android to Android communications so it did not work with my situation.

So now I had the phone and the NXT brick ready to communicate with both Bluetooth socket initiated and ready but, for a really mysterious reason, they could not communicate. I finally found the answer on Google [source] : the problem was that my UUID was not right (only adapted for Android to Android communication).

Now that I made the connection between the two devices, it was time to send data… An easy task I thought, but actually not at all : To begin, I decided to send an integer. So the android sent a random number to its output stream, I told the NXT brick to read and display it and the brick just stopped, throwing an exception on the DataInputStream.readInt() function. After several hours of little tweaks on both devices, it happened that I just could not manage to send my number.

So, in a final desperate burst of motivation, I finally tried to send a string and it worked !

To conclude I still don’t know why a string sent with DataOutputStream.writeInt and received with DataInputStream readInt is throwing exceptions but with a string sent with DataOutputStream.writeUTF and received with DataInputStream.read function, it works perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *