Multiple bluetooth connections to NXT?

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Multiple bluetooth connections to NXT?

Postby kalell » Sun Feb 07, 2010 1:36 am

Hi all,
I've got 1 mobile phone connected to my NXT via bluetooth so far,
but i was wondering if its possible to have 2 different phones connected by bluetooth to a single NXT, possibly with threads?
kalell
New User
 
Posts: 8
Joined: Fri Sep 25, 2009 3:18 pm

Postby kalell » Thu Feb 11, 2010 4:52 pm

:cry:
Anyone have any idea at all?
kalell
New User
 
Posts: 8
Joined: Fri Sep 25, 2009 3:18 pm

Postby gloomyandy » Thu Feb 11, 2010 5:02 pm

Yes you can do this...

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

Postby kalell » Sun Feb 14, 2010 4:33 pm

After alittle research i've been hearing that the NXT can only recieve 1 bluetooth connection via the .waitForConnection() method at a time, is this true?

After reading gloomyanday's post ive tried setting up two different NXTConnection's using the waitForConnection() command one after the other but ive had no luck.

So recieving and handling 2 simultaneous bluetooth connections is not possible or am i doing something wrong?

Or do i need to setup 1 incomming and 1 outgoing bluetooth connection?
Could anyone point me in the right direction please :(
kalell
New User
 
Posts: 8
Joined: Fri Sep 25, 2009 3:18 pm

Postby gloomyandy » Sun Feb 14, 2010 7:22 pm

The NXT can have one inbound (via waitForConnect) and 3 outbound connections at the same time. Note however that having more than one connection open at once will result in much slower communications (because the NXT has to switch the Bluecore module between connections which can take up to 250ms per switch).

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

Similar problem?

Postby bandanner » Sun Feb 14, 2010 8:23 pm

I have a similar problem and hope you can maybe help me too...

So outbound would mean to connect TO another nxt?
for example if you get a device via

RemoteDevice btrd = Bluetooth.getKnownDevice(name);

and the connect to it with

Bluetooth.connect(btrd);

?

In this case i get a runtime-error while i try to wait for in inbound connection after i connected to the first device:
The first(outbound) connection is successful, and the nxt waits for an incoming connection. The incoming connection seems to success on the other device, but on my device which handles two connections it crashes totally.

Greetings

edit:

it looks like it was the same problem as in this thread:
http://lejos.sourceforge.net/forum/viewtopic.php?t=1920
Last edited by bandanner on Sun Feb 14, 2010 8:29 pm, edited 1 time in total.
bandanner
New User
 
Posts: 2
Joined: Sun Feb 14, 2010 7:33 pm

Postby gloomyandy » Sun Feb 14, 2010 8:26 pm

What error do you get? You may find it works better if you establish the inbound connection before you make the outbound one. I know this works fine...

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

Postby bandanner » Sun Feb 14, 2010 8:34 pm

unfortunately i don't have the brick here so i can tell more precisely tomorrow.

but as far as i remember it was

class 16
method 40

like in the topic i posted.

will try to connect the other way round tomorrow

thanks

edit:
ok the error was

Class: 16
Method: 78
PC: 7697

I tried to first establish the inbound connection and then the outbound one.
IT WORKS! :D

Thank you very much, Andy!
bandanner
New User
 
Posts: 2
Joined: Sun Feb 14, 2010 7:33 pm

Postby kalell » Wed Feb 17, 2010 6:54 pm

Well ive tried coding the NXT as the initiator for the bluetooth connection however im having trouble with connecting it to the specific MIDlet program.
The NXT connects to the mobile phone(as the phone flashes the bluetooth logo) but does not connect and react to the specific MIDlet program.

The java ME Midlet code for the bluetooth server program is as follows :

public class Server {

private LocalDevice local;
private myForm form;
private UUID uuid;
private String connectionString;
private StreamConnectionNotifier streamConnNotifier;
private StreamConnection con;
private final int SERIALID = 0x1101;
private DiscoveryAgent agent;

private DataOutputStream dos;
private DataInputStream dis;

public Server(myForm form)
{
this.form = form;
uuid = new UUID(1101);
connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";

}

public void startServer() throws IOException
{


LocalDevice localDevice = LocalDevice.getLocalDevice();
//localDevice.setDiscoverable(DiscoveryAgent.GIAC);
//agent = localDevice.getDiscoveryAgent();

form.append("Address: "+localDevice.getBluetoothAddress());
form.append("Name: "+localDevice.getFriendlyName());

form.append("Waiting for connection");
streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

con = streamConnNotifier.acceptAndOpen();
form.append("Connection done");

RemoteDevice dev = RemoteDevice.getRemoteDevice(con);

form.append("Remote device address: "+dev.getBluetoothAddress());

form.append("Remote device name: "+dev.getFriendlyName(true));

readData read = new readData(form,con);
read.start();
}
}

The code for the NXT is as follows.. where W995 is the phone name.

BTConnection btc;
LCD.drawString("Connecting...", 0, 2);

// RemoteDevice con2 = Bluetooth.getKnownDevice("W995");
btc = Bluetooth.connect("W995", NXTConnection.RAW);

DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();

LCD.drawString("DONE...", 0, 2);

HelloWorld start = new HelloWorld(btc, dis, dos);

start.runProgram();

Anyone have any idea why the connection occurs but not to the specific MIDlet program?
This is driving me nuts :shock:
kalell
New User
 
Posts: 8
Joined: Fri Sep 25, 2009 3:18 pm

Postby gloomyandy » Wed Feb 17, 2010 7:07 pm

The NXt can only connect to the first spp channel/id on the device. There is often already a program/virtusl device attached to this on phones and PCs (usually a virtual comm port). For this reason it is often easier to have the PC/Phone connect to the NXT...

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

Postby kalell » Thu Feb 18, 2010 5:56 pm

Thanks for the tip gloomyandy, although thats a real shame.
Originally i planned on using 1 phone as the master server recieving all incomming connections.

Howerver since thats not possible, i created a MIDlet to act as both a bluetooth reciever and initiator. Ive only just started programming with bluetooth on j2me but it seems wierd having the phone act as both a server and client? But i guess this is the wrong forum for that.
Thanks for the help.
kalell
New User
 
Posts: 8
Joined: Fri Sep 25, 2009 3:18 pm

Postby ggelan » Sun Mar 14, 2010 3:50 pm

Hi guys,

I'm having the same problem as described in this post.
Now I'm a bit confused about the term inbound/outbound,
does this have something to do with master/slave?

If more NXTs need to be connected via bluetooth,
is the master/slave relationship needed?
Do they all need to be manually paired first?

Can someone post some code for the inbound/outbound?
These terms are somehow confused to me : /

Thanks all in advance!
ggelan
New User
 
Posts: 5
Joined: Sun Mar 14, 2010 3:20 pm

Postby gloomyandy » Sun Mar 14, 2010 5:34 pm

Inbound are connections made to an NXT (so the NXt uses Bluetooth.waitForConnection), outbound connections are made from the NXT (so the NEXT uses Bluetooth.connect). You can have three outbound connections (connections initiated by the NXT), but only one inbound (connections to the NXT from some other device)...

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

Postby ggelan » Sun Mar 14, 2010 10:35 pm

Thanks lot Andy!
Now I'm getting clear with the inbound/outbound definitions :p

Can I see the NXT with the inbound connection as the master and the rest 3 outbound connections as the slaves?

After the whole connection set has been done can the outbound connections communicate with each other directly or must they go through the master first?

As I have understood the master NXT waits for the outbound connections from the other 3 NXTs, and they will be connected to the master in a sequential way.
Is this correct?
ggelan
New User
 
Posts: 5
Joined: Sun Mar 14, 2010 3:20 pm

Postby gloomyandy » Mon Mar 15, 2010 1:33 am

There is no master or slave here, there are simply connections. When you call Bluetooth.connect then the connection is created, once it has been created you can use it to read and write data to whatever you have connected to. If you have one nxt open three outbound connections to three other NXTs then there is no connection between the other three NXTs so if you want to talk between them you will have to go via the NXT that has connections to the others.

You should note though that if an NXT has multiple connections open then the speed of those connections is much slower than for a single connection (each send/receive will take several hundred milliseconds, instead of 30ms or so). For this reason you may be better off using a PC as you communications hub rather than an NXT.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4077
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Next

Return to NXJ Projects

Who is online

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

more stuff