19

Android Lollipop seems to default to Mobile Data when the Wi-Fi you are connected to has no Internet access. Does anybody know if this is officially documented somewhere?

We have an application that needs to connect to machines via Wi-Fi that do not have Internet. Our customers are now reporting that the Wi-Fi connection does not work anymore, because the phone automatically switches to LTE.

My understanding would be that the phone still keeps the Wi-Fi connection but uses LTE in addition to provide access to the Internet (lollipop-feature-spotlight-android-now-defaults-to-mobile-data-when-wi-fi-has-no-internet-access-signal-icon-adds-a-for-no-connection).

Is my understanding of this feature wrong? And if so, is there a way to force using the Wi-Fi without Internet? I could not find anything about this in particular in the developer documentation.

Any help is really appreciated.

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
Florian
  • 2,048
  • 3
  • 28
  • 36

4 Answers4

26

To extend on @ianhanniballake's answer, I've found that binding the network using ConnectivityManager.setProcessDefaultNetwork() prevents roaming and allows for full TCP access. Thus, within the onAvailable() callback you could bind the application process to that network rather than opening a connection to a particular URL.

ConnectivityManager connection_manager = 
    (ConnectivityManager) activity.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

connection_manager.registerNetworkCallback(request.build(), new NetworkCallback() {

    @Override
    public void onAvailable(Network network) {
        ConnectivityManager.setProcessDefaultNetwork(network);
    }
}

As of API Level 23: Please use the following OnAvailable Method:

@Override
public void onAvailable(Network network) {
    connection_manager.bindProcessToNetwork(network);
}
Zane
  • 371
  • 2
  • 9
Tez
  • 743
  • 6
  • 18
  • Just for clarification, what if while downloading from server wifi connection gone and mobile data got connected,as we are binding our process to wifi so it will wait for wifi connection? – Rishikesh pathak Oct 05 '17 at 10:33
  • For this to work on Android 9, you need to add the following permission to your app manifest: android.permission.CHANGE_NETWORK_STATE – matdev Apr 10 '19 at 08:16
8

By default, Android 5.0 will only send network requests over networks that it detect have an active internet connection so while it may be 'connected' to the wifi, it is not going to send any data over the network.

However, the Android 5.0 APIs guide talks about the new multiple network support:

Android 5.0 provides new multi-networking APIs that let your app dynamically scan for available networks with specific capabilities, and establish a connection to them. This functionality is useful when your app requires a specialized network, such as an SUPL, MMS, or carrier-billing network, or if you want to send data using a particular type of transport protocol.

This allows you to build a NetworkRequest for a TRANSPORT_WIFI type and direct traffic to it via Network.openConnection() when you receive a onAvailable() callback if you must have certain connections happen over wifi even when the wifi network does not have internet access.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • That seems stupid, why not route traffic to the interfaces based on the destination subnet? – Max Ehrlich Jan 10 '15 at 00:45
  • 3
    @MaxEhrlich - if you have a better implementation please do [submit a patch](https://source.android.com/source/submit-patches.html). – ianhanniballake Jan 10 '15 at 01:14
  • 2
    I shouldn't have to, every other OS does this already. Google clearly has it in their mind how they want it to work and would probably reject such a patch – Max Ehrlich Jan 10 '15 at 02:29
  • 1
    @ianhanniballake Yes, I am on the browser trying to connect to my local server over wifi which is not connected to internet... Since lollipop I can't access the local server as it is trying to connect via mobile network. What Max suggested shoudl be the expected behavior. – Pavan K Mar 11 '15 at 11:03
1

Android 5.0 provides new multi-networking APIs that let your app dynamically scan for available networks with specific capabilities, and establish a connection to them. More info here

So solution for you is ConnectivityManager.requestNetwor().

Petr Duchek
  • 1,223
  • 14
  • 19
0

This is not a solution for those who try to solve the connectivity problem from within their own app. But for those who come here looking for a solution to access a non-internet-WiFi with some standard app like Google Chrome:

You simply have to disabled "Mobile Data" in your phone settings and Chrome or any other App will readily access your WiFi.

Jpsy
  • 20,077
  • 7
  • 118
  • 115