title: Multi connection via bluetooth in android
body: <p>Hy , We are working on the android multiplayer game via bluetooth.
  It is a multiplayer LUDO game in which 4 player connect to each other and play the game.</p>
  
  <p>We are stuck in connection with the 3rd and 4th player connection.</p>
  
  <pre><code>private OnMaxConnectionsReachedListener maxConnectionsListener = new OnMaxConnectionsReachedListener() {
      public void OnMaxConnectionsReached() {
  
      }
  };
  
  private OnIncomingConnectionListener connectedListener = new OnIncomingConnectionListener() {
      public void OnIncomingConnection(String device) {
          //rivalDevice = device;
          if(index &lt; 1000)
          {
              clients[index] = device;
              index++;
              Log.d("Khawar", "Khawar"+device);
              if(index == 1)
              {
                  //blue.setText(clients[0]);
                  mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]);
              }
              else if(index == 2)
              {
                  //blue.setText(clients[0]);
                  //red.setText(clients[1]);
                  mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]);
                  mConnection.sendMessage(clients[1], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]);
              }
              else if(index==3)
              {
                  //blue.setText(clients[0]);
                  //red.setText(clients[1]);
                  //yellow.setText(clients[2]);
                  mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
                  mConnection.sendMessage(clients[1], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
                  mConnection.sendMessage(clients[2], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
              }
          }
          //Toast.makeText(getBaseContext(), "Client found" + device , Toast.LENGTH_LONG).show();
        //  WindowManager w = getWindowManager();
        //  Display d = w.getDefaultDisplay();
         // int width = d.getWidth();
          //int height = d.getHeight();
      }
  };
  
  private OnConnectionLostListener disconnectedListener = new OnConnectionLostListener() {
      public void OnConnectionLost(String device) {
          class displayConnectionLostAlert implements Runnable {
              public void run() {
                  Builder connectionLostAlert = new Builder(self);
  
                  connectionLostAlert.setTitle("Connection lost");
                  connectionLostAlert
                          .setMessage("Your connection with the other player has been lost.");
  
                  connectionLostAlert.setPositiveButton("Ok", new OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                          finish();
                      }
                  });
                  connectionLostAlert.setCancelable(false);
                  try {
                  connectionLostAlert.show();
                  } catch (BadTokenException e){
                      Log.v("TAG", "Exception throws here");
                  }
              }
          }
          self.runOnUiThread(new displayConnectionLostAlert());
      }
  };
  
  private OnConnectionServiceReadyListener serviceReadyListener = new OnConnectionServiceReadyListener() {
      public void OnConnectionServiceReady() {
          if (mType == 0) {
              mConnection.startServer(1, connectedListener, maxConnectionsListener,
                      dataReceivedListener, disconnectedListener);
              self.setTitle("LudoOverAndroid: " + mConnection.getName() + "-" + mConnection.getAddress());
              server = mConnection.getAddress();
              green.setText(server);
              Log.v("1", server);
        //      Intent Game = new Intent(self, Board.class);
              // .putExtra("TYPE", 1);
          //     startActivity(Game);
  
          } else {
  
              self.setTitle("LudoOverAndroid: " + mConnection.getName() + "-" + mConnection.getAddress());
              Intent serverListIntent = new Intent(self, ServerListActivity.class);
              startActivityForResult(serverListIntent, SERVER_LIST_RESULT_CODE);
              clients[index] = mConnection.getAddress();
           //   Intent Game = new Intent(self, Board.class);
              // .putExtra("TYPE", 1);
           //    startActivity(Game);
  
          }
      }
  };
  </code></pre>
  
  <p>Above is the sample code where connection are established.
  But in the Connection Services class we have the following code</p>
  
  <pre><code>       public int connect(String srcApp, String device) throws RemoteException {
          if (mApp.length() &gt; 0) {
              Log.v("a", "connection service-&gt;connect-&gt;mApp.length");
              return Connection.FAILURE;
          }
          mApp = srcApp;
          BluetoothDevice myBtServer = mBtAdapter.getRemoteDevice(device);
          BluetoothSocket myBSock = null;
          Log.v("a", "Yaah aya hay aik baar nahe bar bar :");
          for (int i = 0; i &lt; Connection.MAX_SUPPORTED &amp;&amp; myBSock == null; i++) {
              for (int j = 0; j &lt; 3 &amp;&amp; myBSock == null; j++) {
                  myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
                  if (myBSock == null) {
                      try {
                          Log.v("a", "connection service-&gt;connect-&gt;myBSock==NULL thread sleep");
                          Thread.sleep(200);
                      } catch (InterruptedException e) {
                          Log.e(TAG, "InterruptedException in connect", e);
                      }
                  }
              }
          }
          if (myBSock == null) {
              Log.v("a", "connection service-&gt;connect-&gt;myBSock==NULLss");
              return Connection.FAILURE;
          }
          mBtSockets.put(device, myBSock);
          mBtDeviceAddresses.add(device);
          Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(device));
          mBtStreamWatcherThread.start();
          mBtStreamWatcherThreads.put(device, mBtStreamWatcherThread);
          return Connection.SUCCESS;
      }
  </code></pre>
  
  <p>The mobile when connect with the 3rd or 4th device it return myBSock==null.
  But if the codes work right it must return the address of the device and 
  should add mBtDeviceAddresses.add(device); into the server list.</p>
  
  <p>kindly help us to fix the problem.</p>
  
  <p>Thanks in advance</p>