Queuing Bluetooth Connection Request and accept then concurrently

1.2k Views Asked by At

In my Android application I can accept connection request sent from BT device(SPP profile). those BT devices sends connection request periodically and application accept it. But now my problem is, I can pair with multiple device but wants to communicate with paired devices periodically. so i want clarification on this front. If application communicate with one device and at same time another device sends connection request then can i accept this connection request through my App using BluetoothServerSocket? How?

2

There are 2 best solutions below

0
On BEST ANSWER

Bluetooth Server can server up to 7 different bluetooth clients, you need to create bluetooth server socket in a separate thread and every time a client connects , send that client to a new thread , and return to listening state. you can use the following pseudocode

BluetoothServerSocket serverSocket =  BluetoothAdapter.listenUsingRfcommWithServiceRecord();
while(running){
    BluetoothSocket client = serverSocket.accept(); //blocks untel a client is connected
    sendClientToHisThread(client);
}

private void sendClientToHisThread(final BluetoothSocket socket){
     Thread thread = new Thread(new Runnable(){
@Override
public void run(){
   // communicate with client
      socket.close();
 }
});
    thread.start();
 }
0
On

I think you can follow the line on the BluetoothChat example, having a thread listening for incoming connections but, in your case, as a connection is established you do not close the server socket.