stop discovering peers wifi p2p

i'm trying to make a code where there's a phone who sharing its WIFI credentials wifi such as SSID and passwords thanks to WIFI P2P and in the other side there's a service which is listening for new peers. If the service found a peer and connect to him. The phone will send its WIFI credentials. I'm stucked at the point of the service discovers peers. Although The service discovers and found someone, he's still discovering instead of connecting and i don't know why. Actually i've precised to service when he discovers peers to connect clearly and it's not. The problem is from the service i'm sure because i've tried the same code on an activity with buttons and it's worked. There's the code of service: package com.sara.apps.wifiservice;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;



public class WiFiService extends Service {

    static final int MESSAGE_READ = 1;
    static WifiManager wifiManager;
    private static String TAG = "WiFiService";
    public WifiP2pManager.PeerListListener peerListListener = null;
    public WifiP2pManager.ConnectionInfoListener connectionInfoListener = null;
    List<WifiP2pDevice> peers = null;
    String[] devicenameArray;
    WifiP2pDevice[] deviceArray;
    private WifiP2pManager mManager = null;
    private WifiP2pManager.Channel mChannel = null;
    private BroadcastReceiver mReceiver = null;
    private IntentFilter mIntentFilter = null;
    private ServerSocket serverSocket = null;
    private Socket socket = null;
    private InputStream inputStream = null;
    private HandlerThread handlerThread = null;
    private HandlerThread handlerThreadServer = null;
    private final Binder binder=new LocalBinder();
    ServerClass serverClass;
    ClientClass clientClass;
    SendReceive sendReceive;

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        mIntentFilter = new IntentFilter();
        peers = new ArrayList<WifiP2pDevice>();

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);

        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

        mReceiver = new WifiDirectBroadcastReceiver(mManager, mChannel, this);
        registerReceiver(mReceiver, mIntentFilter);
        registerListeners();
            }
    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what)
            {
                case MESSAGE_READ:
                    byte[] readBuff= (byte[]) msg.obj;
                    String tempMsg=new String(readBuff,0,msg.arg1);
                    Log.i(TAG, "le message recu est" + tempMsg);

                    if(tempMsg!=null) {
                        String[] tempMsg2 = tempMsg.split(";");
                        Log.i(TAG, tempMsg2[0] + "  " + tempMsg2[1]);
                        WifiConnectionReceiver as= new WifiConnectionReceiver();
                        registerReceiver(as,new IntentFilter(WifiConnectionReceiver.getIntentFilterForWifiConnectionReceiver()));
                        try {
                            as.connectToWifi(tempMsg2[0],tempMsg2[1],WiFiService.this);
                            Log.i(TAG," Si le scan wifi marche ? : " );
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            return true;
        }
    });


    private void registerListeners() {

        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }


        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "onSuccess: ");

            }

            @Override
            public void onFailure(int i) {
                Log.d(TAG, "onFailure: ");

            }
        });


        peerListListener = new WifiP2pManager.PeerListListener() {
            @Override
            public void onPeersAvailable(WifiP2pDeviceList peerlist) {
                Log.d(TAG, "onPeersAvailable: ");

                peers.addAll(peerlist.getDeviceList());
                devicenameArray = new String[peerlist.getDeviceList().size()];
                deviceArray = new WifiP2pDevice[peerlist.getDeviceList().size()];
                int index = 0;
                for (WifiP2pDevice device : peerlist.getDeviceList()) {
                    devicenameArray[index] = device.deviceName;
                    deviceArray[index] = device;
                    index++;
                }
            if(peers.size()==0)
            {
                Log.i(TAG,"no device found ");
                return;
            }

                for (int i = 0; i < deviceArray.length; i++) {
                    Log.d(TAG, "onPeersAvailable: deviceArray[" + i + "]=" + peers.get(i));
                }

                WifiP2pDevice device = peers.get(0);
                final WifiP2pConfig config = new WifiP2pConfig();
                config.deviceAddress = device.deviceAddress;
                config.groupOwnerIntent=10;
                mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.i(TAG, "Connectivity OK");
                        connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
                            @Override
                            public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
                                final InetAddress groupowneradress = wifiP2pInfo.groupOwnerAddress;
                                if (wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner) {
                                    Log.d(TAG, "onConnectionInfoAvailable: Host");
                                    serverClass = new ServerClass();
                                    serverClass.start();
                                } else if (wifiP2pInfo.groupFormed)

                                {
                                    Log.d(TAG, "onConnectionInfoAvailable: Client");
                                    clientClass= new ClientClass(groupowneradress);
                                    clientClass.start();
                                }
                            }
                        };
                        Log.i(TAG, "The priority of the service is : " + config.groupOwnerIntent);
                    }

                    @Override
                    public void onFailure(int i) {
                        Log.i(TAG, "Connectivity KO");
                    }
                });


            }
        };




    }
    public class ServerClass extends Thread{
        Socket socket;
        ServerSocket serverSocket;

        @Override
        public void run(){
            try {

                serverSocket=new ServerSocket(8888);
                Log.i(TAG,"socket cree");
                socket=serverSocket.accept();
                Log.i(TAG,"socket utilisee");
                sendReceive=new SendReceive(socket);
                sendReceive.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class SendReceive extends Thread{
        private Socket socket;
        private InputStream inputStream;
        private OutputStream outputStream;

        public SendReceive(Socket skt)
        {
            socket=skt;
            try {
                inputStream=socket.getInputStream();
                outputStream=socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            byte[] buffer=new byte[1024];
            int bytes;

            while (socket!=null)
            {
                try {

                    bytes=inputStream.read(buffer);
                    if(bytes>0)
                    {
                        handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
                        Log.i(TAG,"Message va etre affiche");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void write(byte[] bytes)
        {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class ClientClass extends Thread {
        Socket socket;
        String hostAdd;

        public ClientClass(InetAddress hostAddress) {
            hostAdd = hostAddress.getHostAddress();
            socket = new Socket();
        }

        @Override
        public void run() {
            try {
                socket.connect(new InetSocketAddress(hostAdd, 8888), 500);
                sendReceive = new SendReceive(socket);
                sendReceive.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: "
        );
        try {
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class LocalBinder extends Binder {
        WiFiService getService() {
            return (WiFiService.this) ;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}

Thanks for your answers. Sara