title: Can't find nearby WiFi- Direct devices showing "No Device Found!"
body: <p>I am trying to Discover two devices each other using WifiDirect. Devices are running Android version 8.0 and 9.0 respectively. But not able to succeed.</p>
  <p>First time I click on Discover button on both devices both waits for 2-3 seconds and shows No device found Toast.
  Second time I try to discover on both within a second both show up the No device found Toast.</p>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>package com.example.prototypewfp2p;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  
  import android.Manifest;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.content.pm.PackageManager;
  import android.net.wifi.WifiManager;
  import android.net.wifi.p2p.WifiP2pDevice;
  import android.net.wifi.p2p.WifiP2pDeviceList;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Looper;
  import android.util.Log;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.security.Permission;
  import java.util.ArrayList;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
  
  
      Button btnOnOff, btnDiscover, btnSend;
      ListView listView;
      TextView read_msg_box, ConnectoinStatus;
      EditText writeMsg;
      WifiManager wifiManager;
      WifiP2pManager mMangaer;
      WifiP2pManager.Channel mChannel;
      BroadcastReceiver mReceiver;
      IntentFilter mIntentFilter;
  
      List&lt;WifiP2pDevice&gt; peers = new ArrayList&lt;WifiP2pDevice&gt;();
      String[] deviceNameArray;
      WifiP2pDevice[] deviceArray;
  
      //  Permissions Related
      public String[] PermissionsList = {Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE
              , Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.ACCESS_FINE_LOCATION};
      public int permsRequestCode = 200;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  //      Permissions
  
          for (int i = 0; i &lt; PermissionsList.length; i++) {
              if (ContextCompat.checkSelfPermission(MainActivity.this, PermissionsList[i]) == PackageManager.PERMISSION_DENIED) {
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
  
                      requestPermissions(PermissionsList, permsRequestCode);
  
                  }
              } else {
                  Toast.makeText(MainActivity.this, &quot;Permissions Granted Already!&quot;, Toast.LENGTH_SHORT).show();
              }
          }
  
  
          //Calling Functions Declared below
          initialWork();
          exqListener();
      }
  
      //Permission (Override stuff)
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  
          switch (permsRequestCode) {
              case 200:
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[0] == PackageManager.PERMISSION_GRANTED){
                      Toast.makeText(this,&quot;Permission[1] granted!&quot;,Toast.LENGTH_LONG);
                  }
                  else{Toast.makeText(this,&quot;Permission[1] Denied!&quot;,Toast.LENGTH_LONG);}
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[2] == PackageManager.PERMISSION_GRANTED){Log.i(&quot;myTag&quot;,&quot;Permission 3 granted&quot;);}
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[3] == PackageManager.PERMISSION_GRANTED){Log.i(&quot;myTag&quot;,&quot;Permission 4 granted&quot;);}
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[4] == PackageManager.PERMISSION_GRANTED){Log.i(&quot;myTag&quot;,&quot;Permission 5 granted&quot;);}
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[5] == PackageManager.PERMISSION_GRANTED){Log.i(&quot;myTag&quot;,&quot;Permission 6 granted&quot;);}
                  if(grantResults.length &gt; 0 &amp;&amp;  grantResults[1] == PackageManager.PERMISSION_GRANTED){Log.i(&quot;myTag&quot;,&quot;Permission 2 granted&quot;);}
  
  //                boolean first = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  //                boolean second = grantResults[1] == PackageManager.PERMISSION_GRANTED;
  //                boolean third = grantResults[2] == PackageManager.PERMISSION_GRANTED;
  //                boolean fourth = grantResults[3] == PackageManager.PERMISSION_GRANTED;
  //                boolean fifth = grantResults[4] == PackageManager.PERMISSION_GRANTED;
  //                boolean sixth = grantResults[5] == PackageManager.PERMISSION_GRANTED;
                  break;
          }
      }
  
  
      private void exqListener() {
          btnOnOff.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (wifiManager.isWifiEnabled()) {
                      wifiManager.setWifiEnabled(false);
  //                    Toast.makeText(MainActivity.this,&quot;Wifi is turned OFF&quot;,Toast.LENGTH_LONG).show();
                  } else {
                      wifiManager.setWifiEnabled(true);
  //                    Toast.makeText(MainActivity.this,&quot;Wifi is turned ON&quot;,Toast.LENGTH_LONG).show();
                  }
  
              }
          });
  
          btnDiscover.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                          requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
                      }
                      return;
                  }
                  mMangaer.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                      @Override
                      public void onSuccess() {
                          ConnectoinStatus.setText(&quot;Discovery Started!&quot;);
                      }
  
                      @Override
                      public void onFailure(int reason) {
                          ConnectoinStatus.setText(&quot;Could not start Discovery&quot;);
                      }
                  });
              }
          });
      }
  
  
      private void initialWork() {
          btnOnOff = (Button) findViewById(R.id.onOff);
          btnDiscover = (Button) findViewById(R.id.discover);
          btnSend = (Button) findViewById(R.id.sendButton);
          listView = (ListView) findViewById(R.id.peerListView);
          read_msg_box = (TextView) findViewById(R.id.readMsg);
          ConnectoinStatus = (TextView) findViewById(R.id.connectionStatus);
          writeMsg = (EditText) findViewById(R.id.writeMsg);
  
          wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
          mMangaer = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
          mChannel = mMangaer.initialize(this, getMainLooper(),null);
          mReceiver = new WiFiDirectBroadcastReciever(mMangaer,mChannel,this);
          mIntentFilter = new IntentFilter();
  
          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);
      }
  
      WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
          @Override
          public void onPeersAvailable(WifiP2pDeviceList peerList) {
              if(!peerList.getDeviceList().equals(peers) ){
                  peers.clear();
                  peers.addAll(peerList.getDeviceList());
  
                  deviceNameArray =  new String[peerList.getDeviceList().size()];     //Size of deviceNameArray initialized
                  deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];   //Size of deviceArray initialized
                  int index = 0;
  
                  //assigning device name and device to 'deviceNameArray' and 'deviceArray'
                  for (WifiP2pDevice device: peerList.getDeviceList()){
                      deviceNameArray[index] = device.deviceName;
                      deviceArray[index] = device;
                      index++;
                  }
  
                  ArrayAdapter&lt;String&gt; adapter= new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_list_item_1,deviceNameArray);
                  listView.setAdapter(adapter);
              }
              if(peers.size()==0){
                  Toast.makeText(MainActivity.this,&quot;No Device Found!&quot;,Toast.LENGTH_LONG).show();
                  return;
              }
  
          }
      };
  
      @Override
      protected void onResume() {
          super.onResume();
          registerReceiver(mReceiver,mIntentFilter);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          unregisterReceiver(mReceiver);
      }
  }
  </code></pre>
  <p><strong>WiFiDirectBroadcastReciever.java</strong></p>
  <pre><code>  package com.example.prototypewfp2p;
  
  import android.Manifest;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.net.wifi.WifiManager;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.widget.Toast;
  
  import androidx.core.app.ActivityCompat;
  
  import static android.media.audiofx.Visualizer.STATE_ENABLED;
  
  public class WiFiDirectBroadcastReciever extends BroadcastReceiver {
  
      private WifiP2pManager mMangaer;
      private WifiP2pManager.Channel mChannel;
      private MainActivity mActivity;
  
      public WiFiDirectBroadcastReciever(WifiP2pManager mMangaer, WifiP2pManager.Channel mChannel, MainActivity mActivity) {
  
          this.mMangaer = mMangaer;
          this.mActivity = mActivity;
          this.mChannel = mChannel;
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          String action = intent.getAction();
  
          if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
              int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
  
              if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                  Toast.makeText(context, &quot;Wifi is On&quot;, Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(context, &quot;Wifi is Off&quot;, Toast.LENGTH_SHORT).show();
              }
          } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
              if (mMangaer != null) {
  
                  if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      // TODO: Consider calling
                      //    ActivityCompat#requestPermissions
                      // here to request the missing permissions, and then overriding
                      //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                      //                                          int[] grantResults)
                      // to handle the case where the user grants the permission. See the documentation
                      // for ActivityCompat#requestPermissions for more details.
  
                      Toast.makeText(context,&quot;Acces Location (BroadcastRec) Not granted&quot;,Toast.LENGTH_SHORT).show();
                  }
                  mMangaer.requestPeers(mChannel, mActivity.peerListListener);
              }
          }
          else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
  
          }
          else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
  
          }
  
  
      }
  }
  </code></pre>
  <p><strong>activitymain.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/onOff&quot;
          android:layout_width=&quot;100dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_alignParentTop=&quot;true&quot;
          android:layout_marginStart=&quot;24dp&quot;
          android:layout_marginTop=&quot;47dp&quot;
          android:text=&quot;Wifi On/Off&quot;
          android:textSize=&quot;13dp&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/discover&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignBaseline=&quot;@+id/onOff&quot;
          android:layout_alignBottom=&quot;@+id/onOff&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:layout_marginBottom=&quot;3dp&quot;
          android:layout_marginLeft=&quot;0dp&quot;
          android:text=&quot;discover&quot; /&gt;
  
      &lt;ListView
          android:id=&quot;@+id/peerListView&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;200dp&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_below=&quot;@+id/onOff&quot;
          android:layout_marginTop=&quot;25dp&quot;
          android:background=&quot;@android:color/holo_orange_light&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/readMsg&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentEnd=&quot;true&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_below=&quot;@+id/peerListView&quot;
          android:layout_marginTop=&quot;31dp&quot;
          android:text=&quot;Message&quot;
          android:textAlignment=&quot;center&quot;
          android:textSize=&quot;20sp&quot;
          android:textStyle=&quot;italic&quot; /&gt;
  
      &lt;EditText
          android:id=&quot;@+id/writeMsg&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentBottom=&quot;true&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:ems=&quot;10&quot;
          android:inputType=&quot;textPersonName&quot;
          android:layout_toStartOf=&quot;@+id/sendButton&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/sendButton&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentBottom=&quot;true&quot;
          android:layout_alignParentEnd=&quot;true&quot;
          android:text=&quot;Send&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/connectionStatus&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:layout_alignParentTop=&quot;true&quot;
          android:layout_marginTop=&quot;15dp&quot;
          android:text=&quot;Connection Status&quot;
          android:textAlignment=&quot;center&quot;
          android:textColor=&quot;@android:color/holo_blue_dark&quot;
          android:textSize=&quot;18sp&quot;
          android:textStyle=&quot;italic&quot; /&gt;
  
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>AndroidManifest.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.prototypewfp2p&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.CHANGE_WIFI_STATE&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.CHANGE_NETWORK_STATE&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.Prototypewfp2p&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>