connect two sockets to a droid phone

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

Moderators: 99jonathan, roger, imaqine

connect two sockets to a droid phone

Postby guntram » Fri Dec 02, 2011 3:19 pm

Hi guys!

I am trying to figure out if there is a possibility to connect two sockets at a time from one nxt to a droid phone.

I try to do this to separate sending commands and receiving sensor data.

It seems like there is no chance connecting twice with "waitForConnection()". It also turned out that there is no chance to connect from the nxt to the phone via "connect()" (not even if it is only one socket)...
(btw. pairing the phone or the pc from the nxt's menu doesn't work)

Maybe it could be interesting that i get 2 different Socket objects on the droid side, but on the nxt side the second waitForConnection() throws a nullpointer Exception.
Only when i try to connect the second socket, it throws an IO Exception on the phone, as the device or resource is busy.

So is there a way to get two connections between nxt and android at the same time?

EDIT:

Additionally, i have to say that i can connect two sockets at the same time between two nxts!
guntram
New User
 
Posts: 11
Joined: Fri Dec 02, 2011 3:10 pm
Location: Cologne University of Applied Sciences, Germany

Re: connect two sockets to a droid phone

Postby Shawn » Sat Dec 03, 2011 2:11 am

guntram wrote:Hi guys!

I am trying to figure out if there is a possibility to connect two sockets at a time from one nxt to a droid phone.

I try to do this to separate sending commands and receiving sensor data.


Hi, not sure but why do you want to do that?

Are you using leJOS-Droid (in SVN)? If so seperate threads for reading/writing are created within NXTCommAndroid which it uses to connect.

PS. I moved your thread here since it doesn't really belong in FAQs.
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: connect two sockets to a droid phone

Postby guntram » Mon Dec 05, 2011 10:58 pm

Shawn wrote:Hi, not sure but why do you want to do that?

Are you using leJOS-Droid (in SVN)? If so seperate threads for reading/writing are created within NXTCommAndroid which it uses to connect.

PS. I moved your thread here since it doesn't really belong in FAQs.

Hi Shawn!
Thx for your reply :)

I don't think we are using your leJOS-Droid yet...We are working on an android framework concerning the master thesis of our head developer. This is being developed using NXTs, but should be able to be usable on other bot-systems too.

I guess, regarding your answer, that what i asked is not possible. In our implementation, we also realize separate threads for reading and writing the socket to the NXT, but it seems that there was a problem sometimes. So while sending commands to the brick, it seemed like there was a conflict between reading and writing. So for example there is some command and we receive a reply - and at the same time there should be a reply by querying some of the sensors... maybe somehow the data got mixed in some way. That is why i was assigned the task of creating two separate sockets for this issue.

I am new in the team and don't have that much experience with leJOS. And the other thing was, that creating two sockets between two NXTs was no problem at all. They send/receive data at the same time! :)

All in all, i think our way of implementation was not so well chosen, and that we have to rethink our structure...

So best thanx for your answer and thanx for moving this here!

regards,
guntram
guntram
New User
 
Posts: 11
Joined: Fri Dec 02, 2011 3:10 pm
Location: Cologne University of Applied Sciences, Germany

Re: connect two sockets to a droid phone

Postby guntram » Tue Dec 06, 2011 11:21 pm

as i was succeeding in dual-socketing two NXTs, i want to share some code. even though it is no more than the two samples combined...

the two NXTs who talk to each other are called "yoshi" and "luigi".

start luigi first...this example not always succeeds. this is caused by bad implementation of timing i guess ;)
Code: Select all
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.RemoteDevice;

import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;

public class Luigi {

 
  public static void main(String [] args)  throws Exception
  {
   
        LuigiReceive lr = new LuigiReceive();
        lr.start();
       

   
  }
 
 
 
}



class LuigiSend extends Thread {
 
  String name = "Yoshi";
 
  public void run() {
   
    LCD.drawString("Connecting...", 0, 0);
    LCD.refresh();
   
    RemoteDevice btrd = Bluetooth.getKnownDevice(name);
 
    if (btrd == null) {
      LCD.clear();
      LCD.drawString("No such device", 0, 0);
      LCD.refresh();
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.exit(1);
    }
   
    BTConnection btc = Bluetooth.connect(btrd);
   
    if (btc == null) {
      LCD.clear();
      LCD.drawString("Connect fail", 0, 0);
      LCD.refresh();
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.exit(1);
    }
   
    LCD.clear();
    LCD.drawString("Connected", 0, 0);
    LCD.refresh();
   
    InputStream dis = btc.openInputStream();
    OutputStream dos = btc.openOutputStream();
       
   
       
   
    for(int i=0;i<100;i++) {
      try {
        LCD.drawInt(i*30000, 8, 0, 2);
        LCD.refresh();
        dos.write(i*30000);
        dos.flush();     
      } catch (IOException ioe) {
        LCD.drawString("Write Exception", 0, 0);
        LCD.refresh();
      }
     
      try {
        LCD.drawInt(dis.read(),8, 0,3);
        LCD.refresh();
      } catch (IOException ioe) {
        LCD.drawString("Read Exception ", 0, 0);
        LCD.refresh();
      }
    }
   
    try {
      LCD.drawString("Closing...    ", 0, 0);
      LCD.refresh();
      dis.close();
      dos.close();
      btc.close();
    } catch (IOException ioe) {
      LCD.drawString("Close Exception", 0, 0);
      LCD.refresh();
    }
   
    LCD.clear();
    LCD.drawString("Finished",3, 4);
    LCD.refresh();
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
  }
 
}




class LuigiReceive extends Thread {
 
  String connected = "Connected";
    String waiting = "Waiting...";
    String closing = "Closing...";
 
    public void run(){
     
    while (true)
    {
      LCD.drawString(waiting,0,5);
      LCD.refresh();

          BTConnection btc = Bluetooth.waitForConnection();
         
      LCD.clear();
      LCD.drawString(connected,0,5);
      LCD.refresh(); 

      InputStream dis = btc.openInputStream();
      OutputStream dos = btc.openOutputStream();
     
      LuigiSend ls = new LuigiSend();
         
          ls.start();
     
      try {
       
        for(int i=0;i<100;i++) {
          int n = dis.read();
          LCD.drawInt(n,7,0,6);
          LCD.refresh();
          dos.write(-n);
          dos.flush();
        }
       
        dis.close();
        dos.close();
        Thread.sleep(100); // wait for data to drain
        LCD.clear();
        LCD.drawString(closing,0,5);
        LCD.refresh();
        btc.close();
        LCD.clear();
       
      } catch( Exception e ) {
       
      }
     
    }
   
    }
 
}

and the other one:
Code: Select all
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.RemoteDevice;

import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;

public class Yoshi {

 
  public static void main(String[] args) throws Exception {
   
    YoshiSend ys = new YoshiSend();
   
    ys.start();
   

   
  }
 
 
}

class YoshiSend extends Thread {
 
  String name = "Luigi";
 
  public void run() {
   
    LCD.drawString("Connecting...", 0, 0);
    LCD.refresh();
   
    RemoteDevice btrd = Bluetooth.getKnownDevice(name);
 
    if (btrd == null) {
      LCD.clear();
      LCD.drawString("No such device", 0, 0);
      LCD.refresh();
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.exit(1);
    }
   
    BTConnection btc = Bluetooth.connect(btrd);
   
YoshiReceive yr = new YoshiReceive();
   
    yr.start();
   
    if (btc == null) {
      LCD.clear();
      LCD.drawString("Connect fail", 0, 0);
      LCD.refresh();
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.exit(1);
    }
   
    LCD.clear();
    LCD.drawString("Connected", 0, 0);
    LCD.refresh();
   
    InputStream dis = btc.openInputStream();
    OutputStream dos = btc.openOutputStream();
       
   
   
   
    for(int i=0;i<100;i++) {
      try {
        LCD.drawInt(i*30000, 8, 0, 2);
        LCD.refresh();
        dos.write(i*30000);
        dos.flush();     
      } catch (IOException ioe) {
        LCD.drawString("Write Exception", 0, 0);
        LCD.refresh();
      }
     
      try {
        LCD.drawInt(dis.read(),8, 0,3);
        LCD.refresh();
      } catch (IOException ioe) {
        LCD.drawString("Read Exception ", 0, 0);
        LCD.refresh();
      }
    }
   
    try {
      LCD.drawString("Closing...    ", 0, 0);
      LCD.refresh();
      dis.close();
      dos.close();
      btc.close();
    } catch (IOException ioe) {
      LCD.drawString("Close Exception", 0, 0);
      LCD.refresh();
    }
   
    LCD.clear();
    LCD.drawString("Finished",3, 4);
    LCD.refresh();
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
  }
 
}



class YoshiReceive extends Thread {
 
  String connected = "Connected";
    String waiting = "Waiting...";
    String closing = "Closing...";
 
    public void run(){
     
    while (true)
    {
      LCD.drawString(waiting,0,5);
      LCD.refresh();

          BTConnection btc = Bluetooth.waitForConnection();
         
      LCD.clear();
      LCD.drawString(connected,0,5);
      LCD.refresh(); 

      InputStream dis = btc.openInputStream();
      OutputStream dos = btc.openOutputStream();
     
      try {
       
        for(int i=0;i<100;i++) {
          int n = dis.read();
          LCD.drawInt(n,7,0,6);
          LCD.refresh();
          dos.write(-n);
          dos.flush();
        }
       
        dis.close();
        dos.close();
        Thread.sleep(100); // wait for data to drain
        LCD.clear();
        LCD.drawString(closing,0,5);
        LCD.refresh();
        btc.close();
        LCD.clear();
       
      } catch( Exception e ) {
       
      }
     
    }
   
    }
 
}
of course, "getknowndevice()" assumes the devices are paired. i have no problems pairing NXTs, but i do have problems pairing an NXT to a pc/phone. this always causes an error on the NXTs display...
the concerning systems are a rooted nexus s and a pc with a usb bt stick...
guntram
New User
 
Posts: 11
Joined: Fri Dec 02, 2011 3:10 pm
Location: Cologne University of Applied Sciences, Germany

Re: connect two sockets to a droid phone

Postby guntram » Fri Dec 09, 2011 11:29 am

Hi Andy!

I just talked with my head developer, who has just finished his master thesis labeled "An Android Framework and Runtime System for modular
applications that could interface with heterogeneous robots", which is primarily being developed for NXTs, but should be able to deal with any kind of robot in the future.
Also present was our professor, who is the project leader, and we were arguing if we want to check out the behaviour of dual socketing between droid and the NXTs.

So for our research project it would be very interesting to have an eye on that, even if we decide to implement a better synchronization between droid and NXT on only one socket after we checked the dual socketing thing. I have read a lot of code and documentation and tried to dual socket for quite some time now, but i am not really sure why i cannot connect from the NXT to the phone. If you would have a little hint for me on connecting from the NXT to the phone/pc i would really appreciate it :) (or maybe you have some literature in mind you could tell me of...)

Because you say dualsocketing will cause a large overhead on the NXT, i assume you have already researched this. What exactly is causing this overhead? Is it using so much power that the NXT is going to be empty very fast? Or is it that the NXTs processor will be slowing down and starting to lag everything?

It would be quite an interesting task for us to find stuff out as we are researching, best would be if we could try it out ourselves :)

I would really appreciate any little hint you could give me!

thanx for your patience :) !
guntram
guntram
New User
 
Posts: 11
Joined: Fri Dec 02, 2011 3:10 pm
Location: Cologne University of Applied Sciences, Germany

Re: connect two sockets to a droid phone

Postby gloomyandy » Fri Dec 09, 2011 12:21 pm

I have no idea about the android side of things. I do know that the Bluetooth implementation of the NXT is very limited in terms of what it is able to connect to. So for instance as I have already said it will only connect to a device using the first (i.e. unit zero) RFCOMM device which in effect means that when connecting from the NXT you can only open a single connection to any other device (you can however open up to three connections to different devices). The problem with a PC is that unit zero is often already in use, or has some other service listening on it. Most of this information is available on the web (the NXT Bluetooth module is a Bluecore module from CSR, but this is a custom design for Lego)...

In terms of why having multiple connections from/to the NXT is a bad idea, well I wrote the code that supports this on the NXT so yes I have a pretty good idea why it is bad. Basically the NXt talks to the Bluetooth module what is in effect a serial device, plus an extra control line that can be used to switch the Bluetooth module . If only one connection is in use then things are very simple, open the connection and then send receive data over the usart. However if you have more than one device then you basically have to do switch the serial device between each connection, this involves switching the Bluetooth module into command mode, telling it to switch channels then switching back to data mode. All of this has a pretty big overhead and can get very complex...

If you want to understand more why not simply read the Bluetooth source code, like all of leJOS it is supplied to you...
http://lejos.svn.sourceforge.net/viewvc ... iew=markup

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4000
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: connect two sockets to a droid phone

Postby guntram » Fri Dec 09, 2011 2:15 pm

Thanx again for your answer.

I already have been reading the Bluetooth class for some days yet, so i think i will be reading it some days more. Some things there are just not so clear to me maybe that is why i don't succeed in what i am supposed to do.
If i just say this is not possible or not a good idea this is not a very scientific statement and it would be better if i would get it done and be able to show why it is a bad idea. I will spend some more time with that to see if i will be able to claim any success.
That we are going to implement things in a different way seems inevitable regarding what you all said about this, but there is not only the practical factor in our project, there is also the scientific view we are following.

I have already done some Android Bluetooth handling in the past but there never was a use-case where i had to do something like this now. And i don't like giving up because dealing with things makes one understand them better, even if one fails :)

So if i will succeed in any way about anything what might be of interest, i will document the efforts and share them.

if( efforts > 0 ) share( efforts );

;)
guntram
New User
 
Posts: 11
Joined: Fri Dec 02, 2011 3:10 pm
Location: Cologne University of Applied Sciences, Germany


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff