0

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?

SwapnilP
  • 137
  • 2
  • 7

2 Answers2

2

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();
 }
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
0

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.

takecare
  • 1,684
  • 3
  • 21
  • 32