File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes [Bluetooth] Client and server at the same time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "[Bluetooth] Client and server at the same time?" Watch "[Bluetooth] Client and server at the same time?" New topic
Author

[Bluetooth] Client and server at the same time?

Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
Hello all,

I have a strange bug in my midlet. Each of the mobile phone must be client and server at the same time (via a Bluetooth connection). The problem is that sometimes (almost one out of ten tries), the server acts strangely. The server thread hangs on :

(this is normal) but when a client tries to connect with :

the server hangs for 5 seconds (the mobile phone is totally frozen for 5 seconds), then the client throws an IOException whose message is "Connection rejected". The server doesn't throw an exception, it still waits for a client to connect.

I have tried lots of things, but this problem still exists. As each mobile acts as a client and as a server at the same time, there could be the problem, but I have read a lot of Bluetooth documentation and I can't find where.

May someone point me in the right direction to debug this ? Any idea is welcome !

Thank you very much !

Thibault
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well first I would ask, do you have the Server running in a seperate thread? It should be in its own thread.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
Hello Mark and thank you for your reply.

The answer is yes, the server runs in its own thread, as well as the client. To this point, my guess is that between the time the server was discovered thanks to the device / service discovery and the time the client tried to connect, something goes wrong on the server connection. What could cause this malfunction ? Is there some things that can not run at the same time on the same mobile ? (e.g. a device discovery on the client part of the application and the connection of a client on the server part)
Thanks !
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Nah, that shouldn't be a problem. Maybe the device went out of range between the discovery time and the start of the Connection. Just a guess.

Mark
Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
My problem is that it acts like this on the nokia emulator ...

I haven't tried it on real device yet, but it would probably do the same ...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Just out of curiosity, how does it work through the Wireless Toolkit with their defualt emulator?

Mark
Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
Nice move Mark ! it works fine in the Sun's WTK. I can't achieve why it doesn't in the Nokia emulator though !
Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
Uh oh my mistake ... after some time I get the same IO Exception with the message "connection is refused". I thought the client which was running a service discovery in the background could prevent the server to work correctly, so I have tried to stop the server when a discovery was launched, but it produced no change. Also I made the cyclical discoveries occur after a random time on each phone, but it does not seem to improve.
Thibault Revidon
Greenhorn

Joined: Aug 26, 2005
Posts: 6
Hello again !

Ok, I have cut my code to make it only 6 KB.

Finally, here is the problem : when I try to connect two clients to one server, both clients hang, and then I get the "connection rejected" error. It works with only one clients and several servers, so I guess my devices/services discovery is ok. I have tested how much clients my server can have (thanks to the LocalDevice.getProperty() method) : 7 on the Sun emulator, 5 on the Nokia emulator (Nokia 6230).

My guess is that the server send the same connection string to all clients until someone connects, so they try to connect to the same channel ! What I would like to know is what is the correct implementation of the run method of the server to make it accept several clients ?

Here is the code of the server's run() method :



My Server class implements the Runnable interface. The constructor starts a Thread which contains my Server object. Note : "running" (boolean) is always true in this example.

What is wrong with this implementation ?
Thanks !

Thibault
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

One thing to know is that you are creating a scatternet, and they can have one master and up to 7 clients/slaves. There is a way too extend that through multiple scatternets with one client acting as the master to the other scatternet and passing the other slaves communications in the other scatternet to this scatternet.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Bluetooth] Client and server at the same time?
 
Similar Threads
Connecting a mobile phone to PC via Bluetooth
bluetooth connection is established for 1st request for second request it is not
Bluetooth connection not working on US Nokia phones from J2ME Midlet (MIDP 2.0)
Bluetooth Question
unable to reopen the StreamConnection, for first request working fine but for second it is not