title: Permission violation - getConfiguredNetworks not allowed for uid=10293, reason=java.lang.SecurityException: UID 10293 has no location permission
body: <p>I am working on app which works for sharing files whenever I connect to it through scanning it does not connect on target sdk version 29 and above but works fine on target sdk version 28 This happen only on android 10. Please help me. It has been working fine on all android 9 and later version but not working on android 10 version. What should i do please suggest me i am suffering from it. Please help i will be very thankful.</p>
  <pre><code> This is my manifest file
  </code></pre>
  <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;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      package=&quot;com.Anand.ShareMe&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_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.FOREGROUND_SERVICE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_PRIVILEGED_PHONE_STATE&quot;
          tools:ignore=&quot;ProtectedPermissions&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.REQUEST_INSTALL_PACKAGES&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.SYSTEM_ALERT_WINDOW&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;
          tools:ignore=&quot;ScopedStorage&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_SETTINGS&quot;
          tools:ignore=&quot;ProtectedPermissions&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.VIBRATE&quot; /&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.NETWORK_SETTINGS&quot;
          tools:ignore=&quot;ProtectedPermissions&quot; /&gt;
  
      &lt;application
          android:name=&quot;com.Anand.ShareMe.base.App&quot;
          android:allowBackup=&quot;true&quot;
          android:appComponentFactory=&quot;@string/app_name&quot;
          android:fullBackupContent=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.TBShare&quot;
          tools:ignore=&quot;GoogleAppIndexingWarning&quot;
          tools:replace=&quot;android:appComponentFactory&quot;&gt;
  
          &lt;meta-data
              android:name=&quot;com.google.android.gms.version&quot;
              android:value=&quot;@integer/google_play_services_version&quot; /&gt;
          &lt;meta-data
              android:name=&quot;com.google.android.gms.ads.APPLICATION_ID&quot;
              android:value=&quot;@string/admob_app_id&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.MainActivity&quot;
              android:label=&quot;@string/app_name&quot;
              android:launchMode=&quot;singleTask&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar&quot;&gt;
  
          &lt;/activity&gt;
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ShareActivity&quot;
              android:label=&quot;@string/app_name&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;genonbeta.intent.action.TBShare_SEND_TEXT&quot; /&gt;
                  &lt;action android:name=&quot;android.intent.action.SEND&quot; /&gt;
                  &lt;action android:name=&quot;android.intent.action.SEND_MULTIPLE&quot; /&gt;
                  &lt;action android:name=&quot;genonbeta.intent.action.TBShare_SEND&quot; /&gt;
                  &lt;action android:name=&quot;genonbeta.intent.action.TBShare_SEND_MULTIPLE&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
                  &lt;data android:mimeType=&quot;*/*&quot; /&gt;
              &lt;/intent-filter&gt;
  
  
          &lt;/activity&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.AddDevicesToTransferActivity&quot;
              android:label=&quot;@string/text_addDevicesToTransfer&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.FileExplorerActivity&quot;
              android:label=&quot;@string/text_fileExplorer&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ConnectionManagerActivity&quot;
              android:label=&quot;@string/text_connectDevices&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ContentSharingActivity&quot;
              android:label=&quot;@string/text_send&quot;
              android:launchMode=&quot;singleTask&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.FilePickerActivity&quot;
              android:label=&quot;@string/text_fileExplorer&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ViewTransferActivity&quot;
              android:label=&quot;@string/text_transactionViewer&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar.StaticStatusBar&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
  
                  &lt;data android:scheme=&quot;file&quot; /&gt;
                  &lt;data android:scheme=&quot;content&quot; /&gt;
                  &lt;data android:mimeType=&quot;*/*&quot; /&gt;
  
                  &lt;data android:host=&quot;*&quot; /&gt;
                  &lt;data android:pathPattern=&quot;.*\\.tshare&quot; /&gt;
                  &lt;data android:pathPattern=&quot;.*\\..*\\.tshare&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ManageDevicesActivity&quot;
              android:label=&quot;@string/text_manageDevices&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar&quot;/&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.SearchActivity&quot;
              android:label=&quot;@string/butn_search&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.TextEditorActivity&quot;
              android:label=&quot;@string/text_textEditor&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.ChangeStoragePathActivity&quot;
              android:theme=&quot;@style/Base.Theme.AppCompat.Dialog&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;com.genonbeta.intent.action.UPDATE_STORAGE_PATH&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.BarcodeScannerActivity&quot;
              android:label=&quot;@string/text_scanQrCode&quot;
              android:theme=&quot;@style/Theme.TBShare.BarcodeScannerActivity&quot; /&gt;
  
          &lt;activity
              android:name=&quot;com.Anand.ShareMe.activity.SplashActivity&quot;
              android:label=&quot;@string/app_name&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar&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;activity android:name=&quot;com.Anand.ShareMe.activity.WebShareActivity&quot;
              android:label=&quot;Web Share&quot;
              android:theme=&quot;@style/Theme.TBShare.NoActionBar&quot;/&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/provider_paths&quot; /&gt;
          &lt;/provider&gt;
  
          &lt;receiver
              android:name=&quot;com.Anand.ShareMe.receiver.NetworkStatusReceiver&quot;
              android:process=&quot;:transfer&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.net.wifi.WIFI_STATE_CHANGED&quot; /&gt;
                  &lt;action android:name=&quot;android.net.wifi.WIFI_AP_STATE_CHANGED&quot; /&gt;
                  &lt;action android:name=&quot;android.net.wifi.STATE_CHANGE&quot; /&gt;
                  &lt;action android:name=&quot;android.net.wifi.p2p.CONNECTION_STATE_CHANGE&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          &lt;receiver
              android:name=&quot;com.Anand.ShareMe.receiver.DialogEventReceiver&quot;
              android:process=&quot;:transfer&quot; /&gt;
  
          &lt;service
              android:name=&quot;com.Anand.ShareMe.service.CommunicationService&quot;
              android:enabled=&quot;true&quot;
              android:label=&quot;@string/text_communicationService&quot;
              android:process=&quot;:transfer&quot; /&gt;
  
          &lt;service
              android:name=&quot;com.Anand.ShareMe.service.DeviceChooserService&quot;
              android:label=&quot;@string/text_chooserTargetService&quot;
              android:permission=&quot;android.permission.BIND_CHOOSER_TARGET_SERVICE&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.service.chooser.ChooserTargetService&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;service android:name=&quot;com.Anand.ShareMe.service.DeviceScannerService&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;genonbeta.intent.action.SCAN_DEVICES&quot; /&gt;
                  &lt;action android:name=&quot;genonbeta.intent.action.ADD_IP&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
  
          &lt;service
              android:name=&quot;com.Anand.ShareMe.service.WorkerService&quot;
              android:label=&quot;@string/text_workerService&quot; /&gt;
  
          &lt;service
              android:name=&quot;com.Anand.ShareMe.service.CommunicationToggleTile&quot;
              android:icon=&quot;@mipmap/ic_launcher&quot;
              android:label=&quot;@string/text_shareFiles&quot;
              android:permission=&quot;android.permission.BIND_QUICK_SETTINGS_TILE&quot;
              android:process=&quot;:transfer&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.service.quicksettings.action.QS_TILE&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
      &lt;/application&gt;
  
  
  &lt;/manifest&gt;
  </code></pre>
  <p>This is my ConnectionUtill code</p>
  <pre><code>package com.Anand.ShareMe.util;
  
  import android.Manifest;
  import android.content.Context;
  import android.content.pm.PackageManager;
  import android.location.LocationManager;
  import android.net.ConnectivityManager;
  import android.net.DhcpInfo;
  import android.net.NetworkInfo;
  import android.net.wifi.WifiConfiguration;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  import android.os.Build;
  import android.util.Log;
  
  import androidx.annotation.WorkerThread;
  import androidx.core.content.ContextCompat;
  
  import com.Anand.ShareMe.base.AppConfig;
  import com.Anand.ShareMe.adapter.NetworkDeviceListAdapter;
  import com.genonbeta.android.framework.util.Interrupter;
  
  import java.util.List;
  
  public class ConnectionUtils
  {
      public static final String TAG = ConnectionUtils.class.getSimpleName();
  
      private Context mContext;
      private WifiManager mWifiManager;
      private HotspotUtils mHotspotUtils;
      private LocationManager mLocationManager;
      private ConnectivityManager mConnectivityManager;
  
      ConnectionUtils(Context context) {
          mContext = context;
  
          mWifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
          mLocationManager = (LocationManager) getContext().getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
          mHotspotUtils = HotspotUtils.getInstance(getContext());
          mConnectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
      }
  
      public static ConnectionUtils getInstance(Context context) {
          return new ConnectionUtils(context);
      }
  
      public static String getCleanNetworkName(String networkName) {
          if (networkName == null)
              return &quot;&quot;;
  
          return networkName.replace(&quot;\&quot;&quot;, &quot;&quot;);
      }
  
      public boolean canAccessLocation() {
          return hasLocationPermission(getContext()) &amp;&amp; isLocationServiceEnabled();
      }
  
      public boolean canReadScanResults() {
          return getWifiManager().isWifiEnabled() &amp;&amp; (Build.VERSION.SDK_INT &lt; 23 || canAccessLocation());
      }
  
      public boolean disableCurrentNetwork() {
          // TODO: Networks added by other applications will possibly reconnect even if we disconnect them
          // This is because we are only allowed to manipulate the connections that we added.
          // And if it is the case, then the return value of disableNetwork will be false.
          return isConnectedToAnyNetwork()
                  &amp;&amp; getWifiManager().disconnect()
                  &amp;&amp; getWifiManager().disableNetwork(getWifiManager().getConnectionInfo().getNetworkId());
      }
  
      @WorkerThread
      public String establishHotspotConnection(final Interrupter interrupter,
                                               final NetworkDeviceListAdapter.HotspotNetwork hotspotNetwork,
                                               final ConnectionCallback connectionCallback) {
          final int pingTimeout = 1000; // ms
          final long startTime = System.currentTimeMillis();
  
          String remoteAddress = null;
          boolean connectionToggled = false;
          boolean secondAttempt = false;
          boolean thirdAttempt = false;
  
          while (true) {
              int passedTime = (int) (System.currentTimeMillis() - startTime);
  
              // retry code will be here.
              if (passedTime &gt;= 10000 &amp;&amp; !secondAttempt) {
                  secondAttempt = true;
                  disableCurrentNetwork();
                  connectionToggled = false;
              }
  
              if (passedTime &gt;= 20000 &amp;&amp; !thirdAttempt) {
                  thirdAttempt = true;
                  disableCurrentNetwork();
                  connectionToggled = false;
              }
  
              if (!getWifiManager().isWifiEnabled()) {
                  Log.d(TAG, &quot;establishHotspotConnection(): Wifi is off. Making a request to turn it on&quot;);
  
                  if (!getWifiManager().setWifiEnabled(true)) {
                      Log.d(TAG, &quot;establishHotspotConnection(): Wifi was off. The request has failed. Exiting.&quot;);
                      break;
                  }
              } else if (!isConnectedToNetwork(hotspotNetwork) &amp;&amp; !connectionToggled) {
                  Log.d(TAG, &quot;establishHotspotConnection(): Requested network toggle&quot;);
                  toggleConnection(hotspotNetwork);
  
                  connectionToggled = true;
              } else {
                  Log.d(TAG, &quot;establishHotspotConnection(): Waiting to connect to the server&quot;);
                  final DhcpInfo routeInfo = getWifiManager().getDhcpInfo();
                  //Log.w(TAG, String.format(&quot;establishHotspotConnection(): DHCP: %s&quot;, routeInfo));
  
                  if (routeInfo != null &amp;&amp; routeInfo.gateway != 0) {
                      final String testedRemoteAddress = NetworkUtils.convertInet4Address(routeInfo.gateway);
  
                      Log.d(TAG, String.format(&quot;establishHotspotConnection(): DhcpInfo: gateway: %s dns1: %s dns2: %s ipAddr: %s serverAddr: %s netMask: %s&quot;,
                              testedRemoteAddress,
                              NetworkUtils.convertInet4Address(routeInfo.dns1),
                              NetworkUtils.convertInet4Address(routeInfo.dns2),
                              NetworkUtils.convertInet4Address(routeInfo.ipAddress),
                              NetworkUtils.convertInet4Address(routeInfo.serverAddress),
                              NetworkUtils.convertInet4Address(routeInfo.netmask)));
  
                      Log.d(TAG, &quot;establishHotspotConnection(): There is DHCP info provided waiting to reach the address &quot; + testedRemoteAddress);
  
                      /*if (NetworkUtils.ping(testedRemoteAddress, pingTimeout)) {
                          Log.d(TAG, &quot;establishHotspotConnection(): AP has been reached. Returning OK state.&quot;);
                          remoteAddress = testedRemoteAddress;
                          break;
                      } else
                          Log.d(TAG, &quot;establishHotspotConnection(): Connection check ping failed&quot;);*/
  
                      if (UIConnectionUtils.isOSAbove(Build.VERSION_CODES.P)
                              ? NetworkUtils.ping(testedRemoteAddress, pingTimeout)
                              : NetworkUtils.ping(testedRemoteAddress)) {
                          Log.d(TAG, &quot;establishHotspotConnection(): AP has been reached. Returning OK state.&quot;);
                          remoteAddress = testedRemoteAddress;
                          break;
                      } else
                          Log.d(TAG, &quot;establishHotspotConnection(): Connection check ping failed&quot;);
  
                  } else
                      Log.d(TAG, &quot;establishHotspotConnection(): No DHCP provided. Looping...&quot;);
              }
  
              if (connectionCallback.onTimePassed(1000, passedTime) || interrupter.interrupted()) {
                  Log.d(TAG, &quot;establishHotspotConnection(): Timed out or onTimePassed returned true. Exiting...&quot;);
                  break;
              }
  
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  break;
              }
          }
  
          return remoteAddress;
      }
  
      public boolean hasLocationPermission(Context context) {
          return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
      }
  
      public Context getContext() {
          return mContext;
      }
  
      public ConnectivityManager getConnectivityManager() {
          return mConnectivityManager;
      }
  
      public HotspotUtils getHotspotUtils() {
          return mHotspotUtils;
      }
  
      public LocationManager getLocationManager() {
          return mLocationManager;
      }
  
      public WifiManager getWifiManager() {
          return mWifiManager;
      }
  
      public boolean isConnectionSelfNetwork() {
          WifiInfo wifiInfo = getWifiManager().getConnectionInfo();
  
          return wifiInfo != null
                  &amp;&amp; getCleanNetworkName(wifiInfo.getSSID()).startsWith(AppConfig.PREFIX_ACCESS_POINT);
      }
  
      public boolean isConnectedToAnyNetwork() {
          NetworkInfo info = getConnectivityManager().getActiveNetworkInfo();
  
          return info != null
                  &amp;&amp; info.getType() == ConnectivityManager.TYPE_WIFI
                  &amp;&amp; info.isConnected();
      }
  
      public boolean isConnectedToNetwork(NetworkDeviceListAdapter.HotspotNetwork hotspotNetwork) {
          if (!isConnectedToAnyNetwork())
              return false;
  
          if (hotspotNetwork.BSSID != null)
              return hotspotNetwork.BSSID.equals(getWifiManager().getConnectionInfo().getBSSID());
  
          return hotspotNetwork.SSID.equals(getCleanNetworkName(getWifiManager().getConnectionInfo().getSSID()));
      }
  
      public boolean isLocationServiceEnabled() {
          return mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      }
  
      public boolean isMobileDataActive() {
          return mConnectivityManager.getActiveNetworkInfo() != null
                  &amp;&amp; mConnectivityManager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE;
      }
  
      public boolean toggleConnection(NetworkDeviceListAdapter.HotspotNetwork hotspotNetwork) {
          if (!isConnectedToNetwork(hotspotNetwork)) {
              if (isConnectedToAnyNetwork())
                  disableCurrentNetwork();
  
              /*WifiConfiguration config = new WifiConfiguration();
              config.allowedAuthAlgorithms.clear();
              config.allowedGroupCiphers.clear();
              config.allowedKeyManagement.clear();
              config.allowedPairwiseCiphers.clear();
              config.allowedProtocols.clear();*/
  
              WifiConfiguration config = new WifiConfiguration();
  
              config.SSID = String.format(&quot;\&quot;%s\&quot;&quot;, hotspotNetwork.SSID);
  
              switch (hotspotNetwork.keyManagement) {
                  case 0: // OPEN
                      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                      break;
                  case 1: // WEP64
                      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
  
                      if (hotspotNetwork.password != null
                              &amp;&amp; hotspotNetwork.password.matches(&quot;[0-9A-Fa-f]*&quot;)) {
                          config.wepKeys[0] = hotspotNetwork.password;
                      } else {
                          //fail(&quot;Please type hex pair for the password&quot;);
                      }
                      break;
                  case 2: // WEP128
                      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
  
                      if (hotspotNetwork.password != null
                              &amp;&amp; hotspotNetwork.password.matches(&quot;[0-9A-Fa-f]*&quot;)) {
                          config.wepKeys[0] = hotspotNetwork.password;
                      } else {
                          //fail(&quot;Please type hex pair for the password&quot;);
                      }
                      break;
                  case 3: // WPA_TKIP
                      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                      config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                      config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  
                      if (hotspotNetwork.password != null
                              &amp;&amp; hotspotNetwork.password.matches(&quot;[0-9A-Fa-f]{64}&quot;)) {
                          config.preSharedKey = hotspotNetwork.password;
                      } else {
                          config.preSharedKey = '&quot;' + hotspotNetwork.password + '&quot;';
                      }
                      break;
                  case 4: // WPA2_AES
                      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                      config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                      config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                      config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                      config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
  
                      if (hotspotNetwork.password != null
                              &amp;&amp; hotspotNetwork.password.matches(&quot;[0-9A-Fa-f]{64}&quot;)) {
                          config.preSharedKey = hotspotNetwork.password;
                      } else {
                          config.preSharedKey = '&quot;' + hotspotNetwork.password + '&quot;';
                      }
                      break;
              }
  
              /*
              old wifi connectivity code works for below M
              int netId = getWifiManager().addNetwork(config);
              getWifiManager().disconnect();
              getWifiManager().enableNetwork(netId, true);
              return getWifiManager().reconnect();*/
  
              try {
                  int netId = getWifiManager().addNetwork(config);
  
                  if (/*Build.VERSION.SDK_INT &gt;= */UIConnectionUtils.isOSAbove(Build.VERSION_CODES.M)) {
                      List&lt;WifiConfiguration&gt; list = getWifiManager().getConfiguredNetworks();
                      for (WifiConfiguration hotspotWifi : list) {
                          if (hotspotWifi.SSID != null &amp;&amp; hotspotWifi.SSID.equalsIgnoreCase(config.SSID)) {
                              getWifiManager().disconnect();
                              getWifiManager().enableNetwork(hotspotWifi.networkId, true);
                              return getWifiManager().reconnect();
                          }
                      }
                  } else {
                      getWifiManager().disconnect();
                      getWifiManager().enableNetwork(netId, true);
                      return getWifiManager().reconnect();
                  }
              } catch (Exception exp) {
                  disableCurrentNetwork();
                  return false;
              }
          }
  
          disableCurrentNetwork();
  
          return false;
      }
  
      public interface ConnectionCallback {
          boolean onTimePassed(int delimiter, long timePassed);
      }
  }
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/STO9o.png" rel="nofollow noreferrer">This is my stack strace</a></p>