Best way to check if bluetooth is still connected?

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

Moderators: 99jonathan, roger, imaqine

Best way to check if bluetooth is still connected?

Postby Rickz2020 » Tue Jan 31, 2012 3:17 pm

What is the best way to determine when connection is lost? My NXT is connected to my Android via bluetooth and I'm looking for a good way to determine when the connection is lost. Eg: The user of the Android disables the bluetooth or perhaps walks out of range.

I thought that when the NXT tries to send data and fails, I could use that as a sign that the connection has been lost but I'm just not having any luck. I've also tried Bluetooth.getOpenPort(); but this seems to return 0 (even though the connection is still running).

What about Bluetooth.getStatus();? What values does it return?

Anyone have any hints on the best way to do this?
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Best way to check if bluetooth is still connected?

Postby gloomyandy » Tue Jan 31, 2012 3:22 pm

If the NXT thinks the connection has gone then sending data will fail. Note however that the Bluetooth stack will try retransmitting and various other things before finally giving up on the connection, so it may take some time before the dropped connection is noticed. Possibly the most reliable way is to use a simple heartbeat message that gets sent from one system to the other and is then echoed back, if you don't see the echo (or the request), for some period of time then something bad is happening with the connection...

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

Re: Best way to check if bluetooth is still connected?

Postby Rickz2020 » Tue Jan 31, 2012 3:27 pm

Hmm Ok, so there should be an exception thrown when sending data fails, right? If that is correct then I can have a thread running in the background to monitor a boolean and when the exception is thrown, change the value of the boolean.

Thanks

I'll give this a try - I didn't realize that it would take time to give up on the connection.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Best way to check if bluetooth is still connected?

Postby gloomyandy » Tue Jan 31, 2012 4:01 pm

Hi,
There may not be an exception thrown it depends which classes you are using to send the data (and obviously you have to be actually sending data to even trigger this). The lowest level APIs do not throw an exception they return an error count (these APIs are in BluetoothConnection), but I suspect you are using one of the stream API which will throw an exception...

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

Re: Best way to check if bluetooth is still connected?

Postby Rickz2020 » Tue Jan 31, 2012 4:06 pm

I don't seem to be getting an exception :(

I am testing using System.exit(0); So, if an exception is thrown it just restarts the NXT... but this is not happening.

I'm using the standard java.io.DataInputStream and DataOutputStream and the API says there should be an exception thrown... Odd.

I have a thread constantly sending sensor data so there should be an exception there when sending fails but there is none. :shock:
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Best way to check if bluetooth is still connected?

Postby gloomyandy » Tue Jan 31, 2012 5:45 pm

What happens if you use the lower level write operations in BluetoothConnection? Just modify your code to use these to send a series of bytes. Do you get an error then? What happens if you are trying to read data (using a blocking read), do you get an error then.

What are you using to test this all with? It would be best if you use a very small test program that way we can probably take a look at things. I just don't have the time to write the test code for this right now...

Also what version of things are you running and what is it that you are connecting to?

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

Re: Best way to check if bluetooth is still connected?

Postby Rickz2020 » Thu Feb 02, 2012 2:36 pm

What happens if you use the lower level write operations in BluetoothConnection? Just modify your code to use these to send a series of bytes. Do you get an error then? What happens if you are trying to read data (using a blocking read), do you get an error then.


Not tried these yet, but will ASAP.

I've noticed I do get the exception thrown on the Android side but that's all.

I'm using leJOS 0.9.0
Android 2.2.6 gingerbread
and Windows 7 x64.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Best way to check if bluetooth is still connected?

Postby tarquinn81 » Fri May 17, 2013 5:52 pm

Is there a function that checks if a bluetooth connection has been made?
tarquinn81
New User
 
Posts: 1
Joined: Fri May 17, 2013 5:49 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff