title: Disabling Samsung "Auto Network Switch" for WiFi Connection
body: <p>In our app, we handle initiating a WiFi connection to a device that broadcasts its own wireless access point (with no internet connection) for direct communication. </p>
  
  <p>It works very well on all of our test devices; however, we're receiving reports from users that on certain Samsung devices (Galaxy S4, Galaxy Note 3) there is a setting under Wi-Fi Settings called "Auto Network Switch" that Samsung has added that looks for "unstable" networks, and will automatically disconnect and revert to mobile data. Unfortunately, since our device has no internet connection, Samsung reports it as un unstable network and immediately disconnects. </p>
  
  <p>I don't have either of these devices available for testing on, so I'm curious if anyone else is aware of this issue or knows of a way to either programmatically disable this setting or work around it?</p>
  
  <p>The code we use for the connection is:</p>
  
  <pre><code>/**
   * Attempt to connect to an open wifi network with the given SSID
   * @param ssid the SSID of the unsecured wireless network to connect to
   */
  public static void connectToOpenNetwork (String ssid) {
      WifiManager mgr = getManager();
      WifiConfiguration configuration = getOpenWifiConfiguration(ssid);
      mgr.addNetwork(configuration);
      mgr.saveConfiguration();
  
      int res = findOpenNetworkId(ssid);
      if (res != INVALID_NETWORK_ID) {
          mgr.enableNetwork(res, true);
          mgr.reconnect();
      } else {
          Log.e(TAG, "Received request to connect to network " + ssid + " but the network was not found in the configurations.");
      }
  }
  
  /**
   * Get a WifiConfiguration object configured for an unsecured wireless network with the
   * given SSID.
   * @param ssid the SSID of the network to configure
   * @return a WifiConfiguration object that can be passed to
   * {@link WifiManager#addNetwork(android.net.wifi.WifiConfiguration)}
   */
  private static WifiConfiguration getOpenWifiConfiguration (String ssid) {
      WifiConfiguration config = new WifiConfiguration();
  
      config.SSID = "\"" + ssid + "\"";
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  
      return config;
  }
  </code></pre>