title: wifip2pManager.discoverPeers() fails IN ANDROID 10 despite of using runtime permission and in Manifest too
body: <p>I was trying to run the wifidirectdemo app provided on wifip2p page of documentation but on android 10 its not running perfectly.I tried the solution mentioned on <a href="https://stackoverflow.com/questions/63340713/wifip2pmanager-discoverpeers-fails-in-android-10">WifiP2pManager.discoverPeers fails in android 10</a> ,but nothing helped.All other wifip2p functions are running normally only this is causing issue and returns 0 i.e. ERROR code.
  Thanks for any suggestion.</p>
  <p>Manifest.xml</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.deom&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;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot;/&gt;
  
      &lt;!-- Google Play filtering --&gt;
  
  
      &lt;uses-feature android:name=&quot;android.hardware.wifi.direct&quot; android:required=&quot;true&quot;/&gt;
  
      &lt;application android:theme=&quot;@android:style/Theme.Holo&quot; android:label=&quot;@string/app_name&quot; android:icon=&quot;@drawable/ic_launcher&quot;&gt;
  
  
      &lt;activity android:name=&quot;.WiFiDirectActivity&quot; android:label=&quot;@string/app_name&quot; android:launchMode=&quot;singleTask&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;provider
              android:name=&quot;androidx.core.content.FileProvider&quot;
              android:authorities=&quot;${applicationId}.fileprovider&quot;
              android:exported=&quot;false&quot;
              android:grantUriPermissions=&quot;true&quot;&gt;
              &lt;meta-data
                  android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
                  android:resource=&quot;@xml/file_paths&quot; /&gt;
          &lt;/provider&gt;
  
  
  
      &lt;!-- Used for transferring files after a successful connection --&gt;
  
  
      &lt;service android:name=&quot;.FileTransferService&quot; android:enabled=&quot;true&quot;/&gt;
  
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p>WifiDirectActivity.java</p>
  <pre><code>
  
  package com.example.deom;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.app.Activity;
  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.WifiP2pConfig;
  import android.net.wifi.p2p.WifiP2pDevice;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.net.wifi.p2p.WifiP2pManager.ActionListener;
  import android.net.wifi.p2p.WifiP2pManager.Channel;
  import android.net.wifi.p2p.WifiP2pManager.ChannelListener;
  import android.os.Build;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Toast;
  
  
  /**
   * An activity that uses WiFi Direct APIs to discover and connect with available
   * devices. WiFi Direct APIs are asynchronous and rely on callback mechanism
   * using interfaces to notify the application of operation success or failure.
   * The application should also register a BroadcastReceiver for notification of
   * WiFi state related events.
   */
  public class WiFiDirectActivity extends Activity implements ChannelListener, DeviceListFragment.DeviceActionListener {
  
      public static final String TAG = &quot;wifidirectdemo&quot;;
  
      private static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION = 1001;
  
      private WifiP2pManager manager;
      private boolean isWifiP2pEnabled = false;
      private boolean retryChannel = false;
  
      private final IntentFilter intentFilter = new IntentFilter();
      private Channel channel;
      private BroadcastReceiver receiver = null;
  
      /**
       * @param isWifiP2pEnabled the isWifiP2pEnabled to set
       */
      public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
          this.isWifiP2pEnabled = isWifiP2pEnabled;
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions,
              int[] grantResults) {
          switch (requestCode) {
          case PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION:
              if  (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                  Log.e(TAG, &quot;Fine location permission is not granted!&quot;);
                  finish();
              }
              break;
          }
      }
  
      private boolean initP2p() {
          // Device capability definition check
          if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) {
              Log.e(TAG, &quot;Wi-Fi Direct is not supported by this device.&quot;);
              return false;
          }
  
          // Hardware capability check
          WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
          if (wifiManager == null) {
              Log.e(TAG, &quot;Cannot get Wi-Fi system service.&quot;);
              return false;
          }
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
              if (!wifiManager.isP2pSupported()) {
                  Log.e(TAG, &quot;Wi-Fi Direct is not supported by the hardware or Wi-Fi is off.&quot;);
                  return false;
              }
          }
  
          manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
          if (manager == null) {
              Log.e(TAG, &quot;Cannot get Wi-Fi Direct system service.&quot;);
              return false;
          }
  
  
          channel = manager.initialize(this, getMainLooper(), null);
          if (channel == null) {
              Log.e(TAG, &quot;Cannot initialize Wi-Fi Direct.&quot;);
              return false;
          }
  
          return true;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          // add necessary intent values to be matched.
  
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
  
          if (!initP2p()) {
              finish();
          }
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M
                      &amp;&amp; checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                      != PackageManager.PERMISSION_GRANTED) {
              requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                      WiFiDirectActivity.PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION);
              // After this point you wait for callback in
              // onRequestPermissionsResult(int, String[], int[]) overridden method
          }
      }
  
      /** register the BroadcastReceiver with the intent values to be matched */
      @Override
      public void onResume() {
          super.onResume();
          receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
          registerReceiver(receiver, intentFilter);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          unregisterReceiver(receiver);
      }
  
      /**
       * Remove all peers and clear all fields. This is called on
       * BroadcastReceiver receiving a state change event.
       */
      public void resetData() {
          DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_list);
          DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          if (fragmentList != null) {
              fragmentList.clearPeers();
          }
          if (fragmentDetails != null) {
              fragmentDetails.resetViews();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.action_items, menu);
          return true;
      }
  
      /*
       * (non-Javadoc)
       * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
       */
      @SuppressLint(&quot;MissingPermission&quot;)
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.atn_direct_enable:
                  if (manager != null &amp;&amp; channel != null) {
  
                      // Since this is the system wireless settings activity, it's
                      // not going to send us a result. We will be notified by
                      // WiFiDeviceBroadcastReceiver instead.
  
                      startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                  } else {
                      Log.e(TAG, &quot;channel or manager is null&quot;);
                  }
  
                  return true;
  
              case R.id.atn_direct_discover:
                  if (!isWifiP2pEnabled) {
                      Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
                              Toast.LENGTH_SHORT).show();
                      return true;
                  }
                  final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
                          .findFragmentById(R.id.frag_list);
                  fragment.onInitiateDiscovery();
                  manager.discoverPeers(channel, new ActionListener() {
  
                      @Override
                      public void onSuccess() {
                          Toast.makeText(WiFiDirectActivity.this, &quot;Discovery Initiated&quot;,
                                  Toast.LENGTH_SHORT).show();
                      }
  
                      @Override
                      public void onFailure(int reasonCode) {
                          Toast.makeText(WiFiDirectActivity.this, &quot;Discovery Failed : &quot; + reasonCode,
                                  Toast.LENGTH_SHORT).show();
                      }
                  });
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      @Override
      public void showDetails(WifiP2pDevice device) {
          DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          fragment.showDetails(device);
  
      }
  
      @SuppressLint(&quot;MissingPermission&quot;)
      @Override
      public void connect(WifiP2pConfig config) {
          manager.connect(channel, config, new ActionListener() {
  
              @Override
              public void onSuccess() {
                  // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
              }
  
              @Override
              public void onFailure(int reason) {
                  Toast.makeText(WiFiDirectActivity.this, &quot;Connect failed. Retry.&quot;,
                          Toast.LENGTH_SHORT).show();
              }
          });
      }
  
      @Override
      public void disconnect() {
          final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          fragment.resetViews();
          manager.removeGroup(channel, new ActionListener() {
  
              @Override
              public void onFailure(int reasonCode) {
                  Log.d(TAG, &quot;Disconnect failed. Reason :&quot; + reasonCode);
  
              }
  
              @Override
              public void onSuccess() {
                  fragment.getView().setVisibility(View.GONE);
              }
  
          });
      }
  
      @Override
      public void onChannelDisconnected() {
          // we will try once more
          if (manager != null &amp;&amp; !retryChannel) {
              Toast.makeText(this, &quot;Channel lost. Trying again&quot;, Toast.LENGTH_LONG).show();
              resetData();
              retryChannel = true;
              manager.initialize(this, getMainLooper(), this);
          } else {
              Toast.makeText(this,
                      &quot;Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.&quot;,
                      Toast.LENGTH_LONG).show();
          }
      }
  
      @Override
      public void cancelDisconnect() {
  
          /*
           * A cancel abort request by user. Disconnect i.e. removeGroup if
           * already connected. Else, request WifiP2pManager to abort the ongoing
           * request
           */
          if (manager != null) {
              final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
                      .findFragmentById(R.id.frag_list);
              if (fragment.getDevice() == null
                      || fragment.getDevice().status == WifiP2pDevice.CONNECTED) {
                  disconnect();
              } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE
                      || fragment.getDevice().status == WifiP2pDevice.INVITED) {
  
                  manager.cancelConnect(channel, new ActionListener() {
  
                      @Override
                      public void onSuccess() {
                          Toast.makeText(WiFiDirectActivity.this, &quot;Aborting connection&quot;,
                                  Toast.LENGTH_SHORT).show();
                      }
  
                      @Override
                      public void onFailure(int reasonCode) {
                          Toast.makeText(WiFiDirectActivity.this,
                                  &quot;Connect abort request failed. Reason Code: &quot; + reasonCode,
                                  Toast.LENGTH_SHORT).show();
                      }
                  });
              }
          }
  
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          
      }
  }
  </code></pre>