title: wifi p2p peers changed intent not triggered
body: <p>I am working on a wifi p2p file sharing application. But I am not able to discover the peers</p>
  
  <p>Below is the code for my activity</p>
  
  <pre><code>    package com.neeraj8le.majorproject.activity;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.IntentFilter;
  import android.net.wifi.p2p.WifiP2pDevice;
  import android.net.wifi.p2p.WifiP2pDeviceList;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import com.neeraj8le.majorproject.R;
  import com.neeraj8le.majorproject.WiFiDirectBroadcastReceiver;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class SelectPeerActivity extends AppCompatActivity{
  
      WifiP2pManager mManager;
      WifiP2pManager.Channel mChannel;
      BroadcastReceiver mReceiver;
      IntentFilter mIntentFilter;
      ArrayAdapter&lt;String&gt; wifiP2pArrayAdapter;
      ListView listView;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_select_peer);
  
          listView = findViewById(R.id.list_view);
  
          wifiP2pArrayAdapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1);
          listView.setAdapter(wifiP2pArrayAdapter);
  
          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);
  
          mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
          mChannel = mManager.initialize(this, getMainLooper(), null);
          mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
  
  
  
          search();
  
  
      }
  
      public void search()
      {
          mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
              @Override
              public void onSuccess() {
                  Toast.makeText(SelectPeerActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
  
              }
  
              @Override
              public void onFailure(int reasonCode) {
                  Toast.makeText(SelectPeerActivity.this, "FAILURE", Toast.LENGTH_SHORT).show();
              }
          });
  
      }
  
      public void displayPeers(WifiP2pDeviceList peerList)
      {
          wifiP2pArrayAdapter.clear();
  
          for(WifiP2pDevice peer : peerList.getDeviceList())
              wifiP2pArrayAdapter.add(peer.deviceName + "\n" + peer.deviceAddress);
  
      }
  
  
      /* register the broadcast receiver with the intent values to be matched */
      @Override
      protected void onResume() {
          super.onResume();
          registerReceiver(mReceiver, mIntentFilter);
      }
  
      /* unregister the broadcast receiver */
      @Override
      protected void onPause() {
          super.onPause();
          unregisterReceiver(mReceiver);
      }
  
  }
  </code></pre>
  
  <p>And here is the code for my broadcastreceiver</p>
  
  <pre><code>package com.neeraj8le.majorproject;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.net.NetworkInfo;
  import android.net.wifi.p2p.WifiP2pConfig;
  import android.net.wifi.p2p.WifiP2pDevice;
  import android.net.wifi.p2p.WifiP2pDeviceList;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.util.Log;
  import android.widget.Toast;
  
  import com.neeraj8le.majorproject.activity.SelectPeerActivity;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
  
      private static final String TAG = ":::::::::::::::::::::";
      private WifiP2pManager mManager;
      private WifiP2pManager.Channel mChannel;
      private SelectPeerActivity mActivity;
      List&lt;WifiP2pDevice&gt; mPeers;
      List&lt;WifiP2pConfig&gt; mConfigs;
  
      public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, SelectPeerActivity activity) {
          super();
          this.mManager = manager;
          this.mChannel = channel;
          this.mActivity = activity;
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
  
          if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {    // Check to see if Wi-Fi is enabled and notify appropriate activity
              int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
              if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                  Toast.makeText(mActivity, "Wifi Direct Enabled", Toast.LENGTH_SHORT).show();
                  // Wifi P2P is enabled
              } else {
                  Toast.makeText(mActivity, "Wifi Direct Disabled", Toast.LENGTH_SHORT).show();
                  // Wi-Fi P2P is not enabled
              }
  
          }
          else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
          {
              // Call WifiP2pManager.requestPeers() to get a list of current peers
              // request available peers from the wifi p2p manager. This is an
              // asynchronous call and the calling activity is notified with a
              // callback on PeerListListener.onPeersAvailable()
              mPeers = new ArrayList&lt;&gt;();
              mConfigs = new ArrayList&lt;&gt;();
  
              if (mManager != null) {
  
                  mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
                              @Override
                              public void onPeersAvailable(WifiP2pDeviceList peerList) {
                                  mPeers.clear();
                                  mPeers.addAll(peerList.getDeviceList());
                                  mActivity.displayPeers(peerList);
  
                                  for (int i = 0; i &lt; peerList.getDeviceList().size(); i++) {
                                      WifiP2pConfig config = new WifiP2pConfig();
                                      config.deviceAddress = mPeers.get(i).deviceAddress;
                                      mConfigs.add(config);
                                  }
                              }
  
                          });
  
              }
  
              if (mPeers.size() == 0) {
                  Log.d("::::::::::::::::::::::", "No devices found");
                  return;
              }
  
          }
          else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
              Log.d(TAG, "wifi p2p connection changed action");
              NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
              Log.d(TAG,"network info available");
              // Respond to new connection or disconnections
          } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
              // Respond to this device's wifi state
              Log.d(TAG, "wifi p2p this device changed action");
  //            notifyDeviceUpdate();
              WifiP2pDevice thisDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
              Log.d(TAG,"this device acquired");
          }
      }
  }
  </code></pre>
  
  <p>I have gotten most of the code from <a href="https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html" rel="nofollow noreferrer">here</a></p>
  
  <p>I debugged the app and all 3 intent filters were triggered except for</p>
  
  <p>WIFI_P2P_PEERS_CHANGED_ACTION  </p>
  
  <p>I tried keeping another android device near my phone to see if it detects peers and one time id did trigger the WIFI_P2P_PEERS_CHANGED_ACTION intent.. but then it said that the peerlist size is 0 and hence I am unable to find peer.</p>
  
  <p>Any help with the issue is greatly appreciated.</p>