title: Force Android to Use Wifi network with no internet
body: <p>I am building an android app that needs to communicate over a WiFi network that will not have any internet access. The problem is that even when the WiFi is connected android chooses to use cellular/mobile data when no connection internet is present on the wifi network.</p>
  
  <p>I have read many posts on the issue many of which involve rooting the device but that is not possible with a production app (rooting devices is <strong>not</strong> an option). other solution (like my code bellow) suggest using <code>bindProcessToNetwork()</code> which works perfectly on my Sony Z2 but not on other devices I have tested on (all running 6.0.1)</p>
  
  <pre><code>private void bindToNetwork() {
      final ConnectivityManager connectivityManager = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkRequest.Builder builder;
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
          builder = new NetworkRequest.Builder();
          //set the transport type do WIFI
          builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
          connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
              @Override
              public void onAvailable(Network network) {
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
  
  
                      connectivityManager.bindProcessToNetwork(null);
                      if (barCodeData.getSsid().contains("ap-name")) {
                          connectivityManager.bindProcessToNetwork(network);
                      }
  
                  } else {
                      //This method was deprecated in API level 23
                      ConnectivityManager.setProcessDefaultNetwork(null);
                      if (barCodeData.getSsid().contains("ap-name")) {
  
                          ConnectivityManager.setProcessDefaultNetwork(network);
                      }
                  }
  
                  connectivityManager.unregisterNetworkCallback(this);
              }
          });
      }
  }
  </code></pre>