Running LejosDroid.connect on another thread problem.

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

Moderators: 99jonathan, roger, imaqine

Running LejosDroid.connect on another thread problem.

Postby armakapo » Mon Nov 14, 2011 7:08 pm

Hi, I've been building up a remote controller for lego NXT that runs on Android.
I got it working already, but now that I'm trying to improve it, I'm facing a problem. I want to run the LejosDroid.connect() method on a separate thread (not on the UI) so the buttons don't lag while it's trying to connect (1-3 seconds). Now the thing is, that whenever I put the method on another thread, it just crashes when I attempt to connect. I know you can't draw on the UI while in another thread (you have to do some stuff to do that) but regarding bluetooth, I didn't find any restriction.

I'd appreciate any kind of help on this problem, here is the simple code (remember, it works when not running on a separate thread):

Code: Select all
public void handleConnectButton(View v) {
        new Thread(new Runnable() {
          public void run() {
             conn = LeJOSDroid.connect(CONN_TYPE.LEGO_LCP);
          }
        }).start();

}
       
armakapo
New User
 
Posts: 8
Joined: Mon Nov 14, 2011 6:57 pm

Re: Running LejosDroid.connect on another thread problem.

Postby skoehler » Mon Nov 14, 2011 7:12 pm

I would assume, that the program does not just crash. Instead, you will probably get an exception. Which one is it? What does the exception say?

I suspect, that the leJOSDroid code takes care of obtaining the permission to use bluetooth, as well as establishing the bluetooth connection itself. I assume, that the first part must be done form the UI thread while the latter can be done from any other thread. But I haven't taken a look at the leJOSDroid class myself yet. The exception you're getting might be more conclusive!
skoehler
leJOS Team Member
 
Posts: 1390
Joined: Thu Oct 30, 2008 4:54 pm

Re: Running LejosDroid.connect on another thread problem.

Postby armakapo » Mon Nov 14, 2011 7:22 pm

Thanks you for your quick answer!
I don't know how to find out what exception it throws, as I can't test the program on the android emulator (because it doesn't have bluetooth) and honestly I know pretty much nothing about Android. All I get is the dialog saying the program stopped working unexpectedly.
Is there any way I can find what exception it's throwing? Also I'll start looking at LejosDroid code, maybe I find something interesting that could help.
armakapo
New User
 
Posts: 8
Joined: Mon Nov 14, 2011 6:57 pm

Re: Running LejosDroid.connect on another thread problem.

Postby skoehler » Mon Nov 14, 2011 7:59 pm

armakapo wrote:Thanks you for your quick answer!
I don't know how to find out what exception it throws, as I can't test the program on the android emulator (because it doesn't have bluetooth) and honestly I know pretty much nothing about Android. All I get is the dialog saying the program stopped working unexpectedly.
Is there any way I can find what exception it's throwing? Also I'll start looking at LejosDroid code, maybe I find something interesting that could help.


But you know Java, right?
Put the LeJOSDroid.connect(CONN_TYPE.LEGO_LCP) in a try catch block. Catch Throwable (not Exception, just to be safe), call the printStackTrace method of the Throable object and pass a StringWriter as a parameter. Then display the String in the UI.

Also try to google whether you can enable some sort of Debug mode, or whether there is a log file. It would be very strange, if Android would throw this valuable information (why the application crashed) away.
skoehler
leJOS Team Member
 
Posts: 1390
Joined: Thu Oct 30, 2008 4:54 pm

Re: Running LejosDroid.connect on another thread problem.

Postby armakapo » Mon Nov 14, 2011 8:16 pm

I'll try this out on wednesday when I'm back at university. Honestly I'm new to Java too but I've been learning quite a lot working with leJOS. As soon as I try this out, I'll post here again. I really appreciate all your help, thanks for the tips!
armakapo
New User
 
Posts: 8
Joined: Mon Nov 14, 2011 6:57 pm

Re: Running LejosDroid.connect on another thread problem.

Postby Shawn » Mon Nov 14, 2011 10:46 pm

Have a look a the samples. Also, see the tutorial http://lejos.sourceforge.net/nxt/nxj/tutorial/Android/Android.htm. You should use adb to log what's happening.
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Running LejosDroid.connect on another thread problem.

Postby armakapo » Wed Nov 16, 2011 8:00 pm

Thanks skoehler for the tips, I've catched the exception and it was because I wasn't calling Looper.prepare() and loop() on the new thread created. Now it's running but still getting another kind of problem, althought the original one was solved. After I solve it I'll post here again. Thanks for the support!
armakapo
New User
 
Posts: 8
Joined: Mon Nov 14, 2011 6:57 pm

Re: Running LejosDroid.connect on another thread problem.

Postby armakapo » Thu Nov 17, 2011 1:14 pm

Ok solved my issue. Here's the working code:

Code: Select all
public void handleConnectButton(View v) {
      if(connected){
         setTextOnUIThread(mMessage,"Already connected");
         return;
      }
      setTextOnUIThread(mMessage,"Connecting...");
      new Thread(new Runnable() {
          public void run() {
             try{
                Looper.prepare();
                conn = LeJOSDroid.connect(CONN_TYPE.LEGO_LCP);
                if(conn != null){
                   connected = true;
                   NXTCommand.getSingleton().setNXTComm(conn.getNXTComm());
                   setTextOnUIThread(mMessage,"Connection successful");
                    setTextOnUIThread(mStatus,"CONNECTED");
                }
                else{
                   connected = false;
                    setTextOnUIThread(mMessage,"NXT not found");
                    setTextOnUIThread(mStatus,"DISCONNECTED");
                }
                Looper.loop();
                Looper.myLooper().quit();
             }
             catch(final Exception e){
                runOnUiThread(new Runnable() {
                     public void run() {
                        setContentView(R.layout.error);
                        TextView tv = (TextView)findViewById(R.id.errorText);
                        tv.setText(stack2string(e));
                     }
                   });
             }
          }
        }).start();

   }


Thanks again for your help.
armakapo
New User
 
Posts: 8
Joined: Mon Nov 14, 2011 6:57 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff