connection failed during socket connection bluetooth android
<p>i am doing on Bluetooth application......when i am making connection from 1st device to second its getting connected while again i am making connection again by reopen application from 2nd device to 1st its not conected and showing toast mesaage "Unable to connect" as mentioned in android developer Bluetooth chat application..so what should i do for this means how to make connection from also second device to first..while both device contains same source code application so whats the problem i am getting stuck and wonder why this problem occurs?</p>

<p>my code is below:</p>

<pre><code>  private void connectionFailed() {
    setState(STATE_LISTEN);

    // Send a failure message back to the Activity
    Message msg = mHandler.obtainMessage(FaceCardList.MESSAGE_TOAST);
    Bundle bundle = new Bundle();
    bundle.putString(FaceCardList.TOAST, "Unable to connect device");
    msg.setData(bundle);
    mHandler.sendMessage(msg);
    }


    @Override
    public void run() {
        //Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            System.out.println("before connection");
            mmSocket.connect();
            System.out.println("after connection ");

        } catch (Exception e) {
            System.out.println("in connection failed :-"+e.getMessage());
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                //Log.e(TAG,"unable to close() socket during connection failure",                                    e2);
            }
            // Start the service over to restart listening mode

            try {
                BluetoothChatService.this.start();  
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
                System.out.println("in connection failed start bluetooth chat service :-"+e2.getMessage());
            }

            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }








     @Override
  public void onActivityResult(int requestCode, int resultCode,
        final Intent data) {

        Log.d("OnActivityResult", "onActivityResult " + resultCode);

    switch (requestCode) {
    case REQUEST_CONNECT_DEVICE:

        // When DeviceListActivity returns with a device to connect
        if (resultCode == Activity.RESULT_OK) {
            // sendMessage("dfaserfawerwr");
            FaceCardList.requestConnection = true;
            FaceCardList.deviceSelected = false;

            address = data.getExtras().getString(
                    DeviceListActivity.EXTRA_DEVICE_ADDRESS);


            Log.d("REQUEST_CONNECT_DEVICE", "REQUEST_CONNECT_DEVICE" + resultCode);


            device = FaceCardList.mBluetoothAdapter
                    .getRemoteDevice(address);

            // Attempt to connect to the device
            System.out.println("1111");
            FaceCardList.mChatService.connect(device);
            // Get the device MAC address
        }
        break;
</code></pre>
