how to manage inbound and outbound BT connections?

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

Moderators: 99jonathan, roger, imaqine

how to manage inbound and outbound BT connections?

Postby Rickz2020 » Thu Feb 02, 2012 1:54 pm

I am encountering some strange behavior and I think it is because when one connection is set up, it blocks another device from connecting.

I'm using:
Android 2.2.6 Gingerbread
leJOS 0.9.0
HOLOX BT-541 (gps device)
Windows 7 x64

What I am doing is this:

Connecting to a remote BT GPS device:
Code: Select all
//Try find the GPS device. Returns true if found and false if not...
    public boolean BTDeviceDetected() {
        boolean GPSDetected = false;
        try {
           
            RemoteDevice rd = Bluetooth.getKnownDevice(GPS_Device_Name);

            if (rd != null) {
                GPSDevice = rd;
                GPSDetected = true;
            } else {
                GPSDetected = false;
            }
        } catch (Exception ex) {
        }
        return GPSDetected;
    }

 //Attempt to connect to the GPS device. Returns true for success and false for a fail.
    public boolean ConnectGPS() {
        try {
            Bluetooth.addDevice(GPSDevice);
            btGPS = Bluetooth.connect(GPSDevice.getDeviceAddr(), NXTConnection.RAW, pin);           
            if (btGPS == null) {
                result = false;//No connection
            } else {
                result = true;//Connection Successful
                //Tried using openInputStream too. What's the difference?
                in = btGPS.openDataInputStream();
                gps = new GPS(in);
            }
        } catch (Exception ex) {
        }
        return result;
    }



Next, once the BT GPS is connected, I attempt to have an Android phone connect:
Code: Select all
 public boolean connect() {
       
        boolean success = false;
        while (!success) {
            btc = Bluetooth.waitForConnection(0, NXTConnection.RAW);
             DIS = btc.openDataInputStream();
             DOS = btc.openDataOutputStream();
            if (btc != null) {
                success = true;
                connected = true;
            }
        }
        return success;
    }


The BT GPS in an outbound connection and the Android to NXT connection is an inbound one. For some reason It's really difficult to connect via android. The connection attempt made by the Android fails about 9 out of 10 times, although, sometimes I do manage to connect and everything works.

If I attempt to have the Android connect first and then the BT GPS, upon connecting to the BT GPS, a nullPointerException is thrown. It does connect to the BT GPS.

I thought that maybe I could put one of the connections to "sleep" and so tried using:
Code: Select all
//called before we attempt to await for the Android to connect
BTConnection.closeStream();


and...

Code: Select all
//called when the android has connected
BTConnection.openStream();


... but still not working.

Another thing I've tried is having the Android connect and then only later on once some data has been sent via Android to the NXT, connect the BT GPS - this seems to work.

I'm not sure, but is it possible that the streams are getting mixed up?

Does anyone have any advice or a solution?

Thanks,

Rich.

PS: I've got the dGPS sensor from Dexter Industries, but it takes ages to connect to a satellite and is impossible to connect a satellite when there are clouds in the sky, which from me living in London, is impossible to avoid. Anyone else have the same problem with the dGPS sensor?
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: how to manage inbound and outbound BT connections?

Postby Rickz2020 » Thu Feb 02, 2012 10:54 pm

Hmm ok, it seems that the NXT is trying to write data to the DataOutputStream for both the Android and the BT GPS device.

If I connect the Android via BT and then connect the BT GPS later on, they both work, although slow-ish, as I think Andy mentioned they would.
I've noticed that if while both Android and BT GPS are connected, I turn off the BT GPS device, I get the exception thrown in the .writeInt() method - which would obviously only be used to write data to the Android.

I'm assuming now that I need a way to keep the connections separate. Anyone have any ideas?
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: how to manage inbound and outbound BT connections?

Postby gloomyandy » Thu Feb 02, 2012 11:23 pm

I doubt very much if it is writing to the wrong stream. What is much more likely is that when you turn off the GPS the loss of connection causes the NXT Bluecore Bluetooth module to crash resulting in leJOS resetting the device and so also closing the connection to the Android device. The Bluetooth module used in the NXT is rather fragile especially when using multiple connections... So why are you turning off the GPS device? Also did you ever track down what was causing the null pointer exception? Oh and looking at your code posted below it will throw a null pointer exception if ever waitForConnection returns null...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3898
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: how to manage inbound and outbound BT connections?

Postby Rickz2020 » Fri Feb 03, 2012 1:56 pm

I turned off the BT GPS device because with my robot a possible scenario is the BT GPS running out of battery and I wanted to catch any exceptions that may result in such a scenario.

As for the NPE, iv'e got no idea why it's being thrown. It only happens if I connect the phone first and then the BT GPS. I wish I could use this dGPS sensor from Dexter Industries but it's useless :( Clear skies and still no signal. My BT GPS device picks up signal in under 2 minutes...

I don't really know what I should do now. I need the GPS as it's a core feature of the robot. I would prefer to use the BT GPS as it's quicker, but it's causing so many problems and slows the rate of data transmission.

Is it possible to "suspend" a BT connection?

If I could do that then I could connect the Android and BT GPS, put the BT GPS to sleep, and continue about controlling the robot until I need the BT GPS. Then, I could "suspend" the Android BT connection while the robot uses the BT GPS connection to read coordinates and navigate to it's target. Then finally, I could again "suspend" the BT GPS connection and reactivate the Android BT connection.

Perhaps if I have a small check like:

Code: Select all
public double GetLatitude() {
       while(pause){
       //Wait until pause is changed to false before continuing to read
       }
       lat = gps.getLatitude();
       return lat;
    }


Does this seem like a possible solution? Is there a better way to "suspend" the connection? Am I dreaming?
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: how to manage inbound and outbound BT connections?

Postby gloomyandy » Fri Feb 03, 2012 3:00 pm

Hi,
At the end of the day this is a Lego based robot we are talking about, I really wouldn't worry too much about batteries going flat etc. As to managing your connections yes you can take a look at the BTConnection setActiveMode method this lets you control when we look at each open connection. AM_DISABLE will disable the connection. However the problem you have is that the GPS device is probably sending data all of the time. I'm not sure how this will react if you are not reading the data...

I always recommend that with the NXT you make use of send/ack style protocols in which you send a command and then wait for a reply as these have much better flow control characteristics, they may not be as efficient but they do tend to be much more robust.

What is the project you are working on? Is it just for your own interest or is it part of some course?

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

Re: how to manage inbound and outbound BT connections?

Postby Rickz2020 » Fri Feb 03, 2012 8:14 pm

Hi,

I've just tried that btGPS.setActiveMode(BTConnection.AM_DISABLE); and am still having problems connecting the Android after connecting the BT GPS. It appears that the NXT BT connection is being blocked. It's virtually impossible to connect the Android once the BT GPS has connected and I just don't know why :(

I tried using the btGPS.setActiveMode(BTConnection.AM_DISABLE); after connecting the Android and then connecting the BT GPS. It works great. Puts the BT GPS right to sleep, but how would I wake it up? .AM_ALWAYS?

I also get this darn NPE but it seems to be random... Also, occastionally, like 1/100 times, I will get a message on the NXT display: "SimpleGPS.run() error: ArrayIndextOutOfBounds" What could cause this?

I always recommend that with the NXT you make use of send/ack style protocols in which you send a command and then wait for a reply as these have much better flow control characteristics, they may not be as efficient but they do tend to be much more robust.


Wouldn't that slow down my communications significantly?

The project I am working on is a fully controllable, self navigating robot. The robot has two modes: Autonomous and Freestyle. In freestyle mode, it is much like an RC car, with the exception that it sends back sensor data to the Android and from the IR sensor readings, draws a simple 2D "radar". In Automatic mode, the user will be requested to turn on the BT GPS and then be prompted to connect to it. Once connected, the user will be asked to either set destination coordinates by typing them into the Android, or by selecting from a list of predefined locations. Once the robot has received the coordinates, it will ask the user if he/she is ready for the robot to start it's journey. The Communications between the Android and the NXT will then be suspended and the communications between the BT GPS device and the NXT will be started.

Once the robot has navigated to it destination, it will disable the BT GPS device communications and attempt to make contact with the Android so that a mission report can be displayed.

As you can see, the GPS is critical here :(

This project is part of my final year project - studying software engineering. Two programs (NXT and Android) and a 10,000 word report due by May 2. I'd like to have the programming done by the end of March at the latest so that I can focus on my exams.

So, you guys will probably see me here pretty often :D

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: how to manage inbound and outbound BT connections?

Postby gloomyandy » Fri Feb 03, 2012 10:03 pm

Hi,
as someone that has been doing this stuff for about 35 years a few of bits of advice...
1. It is often best to do things the way that work, rather than trying to do things the "right way", unless that is you have a lot of time on your hands
2. If you don't understand how something works read the source (luckily we give you it all).
3. Someone else has probably already hit the same problem you are hitting... So in this case look about 3 posting down in this very forum to find another user having problems with the gps class...

Good luck with your project, but remember it is your project...

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

Re: how to manage inbound and outbound BT connections?

Postby Rickz2020 » Mon Feb 06, 2012 9:15 pm

That's a loooooong time :D

It is often best to do things the way that work, rather than trying to do things the "right way", unless that is you have a lot of time on your hands

I agree!

If you don't understand how something works read the source (luckily we give you it all).

I do try but sometimes have to fight the urge to rather watch Stargate SG-1 repeats :(

Someone else has probably already hit the same problem you are hitting... So in this case look about 3 posting down in this very forum to find another user having problems with the gps class...

Thanks, I had been watching that thread but was apprehensive to mess-about flashing the brick (I have problems - the PC doesn't recognize the brick - with my USB ports) and it seemed like he was having an issue with the date.

Anyway, I did after you mentioned it, give it a try but unfortunately it it didn't make any noticeable difference. I've decided what I am going to do though... I'm just going to do what I mentioned earlier and suspend the BT GPS connection while the connection to the Android is needed.

Good luck with your project, but remember it is your project...

Thanks, and I know - I appreciate the help I get here and would never ask anyone to do my project or any part of it. The whole point of this project is to demonstrate my ability and to learn and so all I would like is the occasional guidance and hints. Although I do understand that you guys/girls do this for free and so I expect nothing and thank you sincerely for any help provided.

Regards,

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff