Hello
I'm working for a project and I have created a client-server bluetooth application. I start up the server application and then connect the client to it.
The server needs to validate little information from the client and then the client will drop the communication.
Then it should be ready for another client to connect to it... so when the communication drops I start the connection again by calling acceptAndOpen(xxx) one more time... and so on
Here's the problem, I'm able to connect the client twice, then the server application freezes...
Here's some of the code I'm using:
public void startConnection(){
try {
ld = LocalDevice.getLocalDevice();
ld.setDiscoverable(DiscoveryAgent.GIAC);
url = "btspp://localhost:"+uuid.toString()+";name="+ld.getFriendlyName();
scn=(StreamConnectionNotifier)Connector.open(url);
open();
} catch (BluetoothStateException ex) {
} catch (IOException ex){
}
}
public void open(){
try{
sc=scn.acceptAndOpen();
dis=sc.openDataInputStream();
dos=sc.openDataOutputStream();
recibir();
} catch (BluetoothStateException ex) {
ex.printStackTrace();
} catch (IOException ex){
ex.printStackTrace();
}
ublic void closeConnections(){
try {
if(dos!=null)dos.close();
if(dis!=null)dis.close();
if (sc!=null)sc.close
} catch (IOException ex) {}
open();
}
public void receive() {
Thread t = new Thread() {
public void run() {
String mensajeRecibido = null;
try {
mensajeRecibido = dis.readUTF();
//do something with the message
receive();
} catch (IOException e) {
closeConnections();
}
}
};
t.start();
}

Reply With Quote

