Android to NXT via bluetooth - readInt blocks forever

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: imaqine, 99jonathan, roger

New User
Posts: 1
Joined: Mon Jun 11, 2012 7:01 pm

Android to NXT via bluetooth - readInt blocks forever

Postby daveg » Wed Jun 20, 2012 6:45 pm


I've successfully got the sample projects BTSend and BTReceive working, and I'm trying to move the BTSend application to Android instead of the PC.

In SVN there is a sample LeJOS-Droid project, so I'm trying this and finding that although it connects to the NXT the data sent from Android to the NXT never arrives...

If I add some LCD.drawString calls on the NXT side before and after the call to dis.readInt() I can see that this method blocks forever.

On the Android side dos.writeInt() is called, and after this it calls dis.readInt() and this blocks forever...


The last output in the Android logcat window shows this:

06-20 17:53:16.559: E/LeJOSDroid NXJ log:(18825): Connected to NXT
06-20 17:53:16.559: D/ASOCKWRP(18825): asocket_write
06-20 17:53:16.559: I/BLZ20_WRAPPER(18825): blz20_wrp_poll: nfds 2, timeout -1 ms
06-20 17:53:16.559: D/BLZ20_WRAPPER(18825): blz20_wrp_poll: transp poll : (fd 52) returned r_ev [POLLOUT ] (0x4)
06-20 17:53:16.559: D/BLZ20_WRAPPER(18825): blz20_wrp_poll: return 1
06-20 17:53:16.569: D/BLZ20_WRAPPER(18825): blz20_wrp_write: wrote 6 bytes out of 6 on fd 52
06-20 17:53:16.569: D/ASOCKWRP(18825): asocket_read
06-20 17:53:16.569: I/BLZ20_WRAPPER(18825): blz20_wrp_poll: nfds 2, timeout -1 ms

I've seen other people posting example Android bluetooth to NXT code, that does not use the NXT* classes in LeJOS-Droid, using the native Bluetooth classes instead. I've tried code like this on the Android side too, and get the same results.

I've also found an Android application called nxtcontrolv2, available with source code from here:

When this connects to the example NXT program supplied with the source code, the Android side enters a loop sending a command "0" with argument of "0" using dos.writeInt(). While it is doing this the NXT side blocks, seemingly forever, calling dis.readInt(). This behavior is consistent with that of the sample programs supplied with LeJOS. However, if you pick up and tilt the phone it then starts sending values other that "0" to the NXT (move / speed / etc), at which point the NXT side crashes. I put try/catch calls in the NXT side and found that dis.readInt() was throwing an EOFException. The Android side still seemed to be connected to the NXT though and was continuing to send data to the NXT...

My phone is a HTC Desire, running Android 2.2.2. I have seen some threads on other sites saying that Bluetooth SPP is broken in some phones, possibly including mine - I'm not sure how accurate that is though as I can run the NXT Remote and MINDdroid applications on the same phone without any problem.

All my testing was done with LeJOS 0.9.1-beta2 on the NXT, and I'm running OpenSuse / Eclipse as my development platform.

I'm sure there is something real simple I'm missing here ?

User avatar
Advanced Member
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Android to NXT via bluetooth - readInt blocks forever

Postby Shawn » Thu Jun 21, 2012 11:34 am

Well all I can say is that MINDdroid, which you say works for your handset, doesn't have separate read write threads and it uses LCP (Lego Communication Protocol).

LeJOS-Droid does have dedicated read write threads so while read might block, the program as a whole should not. Also, it's not using LCP unless you specify that.

Personally, I'd try modifying NXTCommAndroid. You may need to look at to understand what some it is doing.

Otherwise you could try configuring the project to use It's not so difficult to do. I had to use that on my first HTC handsets which were supposed to have SPP but didn't. I don't know if the backport will fix things or not but it may be worth the shot.

The src for MINDdroid is online too and you may want to look at it for comparison. Since it uses LCP though, it's limited to sending a command and getting the response code back.

So anyway my conclusion (which may very well be wrong) is that the programs that work use LCP (I'm guessing the nxt-remote does) and the ones that don't are not working due to your handset's bluetooth implementation.

Return to “NXJ Software”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest