1

Simply put, I want to use my Android device to connect to a LAN but not lose my internet capabilities.

I have dug through Google's guides on network connections, but the only possible solution I have found is Wi-Fi Direct. Unfortunately, I don't think this is possible because the LAN does not support the Wi-Fi Direct protocol.

Is there a way to a connect to a Wi-Fi access point with no internet and remain connected to either cellular or a previous Wi-Fi access point which has internet?

Re-configuring the LAN is something I can do, if that helps

Edit: I have seen this question, but it does not look like there is an answer and it was asked over 3 years ago

Community
  • 1
  • 1
Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
  • The first answer to the question you linked to should do what you want. – alex Jul 30 '15 at 01:33
  • Which one / how? I want the user to connect in-app to the LAN, and while still connected to the LAN, have internet capabilities outside of the app (such as in Chrome) – Daiwik Daarun Jul 30 '15 at 01:36
  • Sorry, didn't read the other answer carefully enough. I'll add a proper answer below. – alex Jul 30 '15 at 01:39

1 Answers1

1

You need to build a HttpClient that knows to only use the WiFi.

Android will check internet connections to see if it can get onto the internet with them and ignore them if it can't. Even for local IP addresses, which can be a pain.

This is part of a Dagger module I wrote to create a correctly configured OkHttp client.

/**
 * Find the WiFi Network object. If the WiFi is off this will return null. You might want to listen to the broadcasts from the WiFi system to retry when the WiFi is turned on.
 */
@Provides
public Network provideNetwork(ConnectivityManager connectivityManager) {
    for (final Network network : connectivityManager.getAllNetworks()) {
        final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
        final int networkType = networkInfo.getType();

        if (networkType == ConnectivityManager.TYPE_WIFI) {
            return network;
        }
    }

    return null;
}


/**
 * Create a HttpClient that will only use the network supplied. Changing this for the built in Apache HttpClient should be easy enough. 
 */
@Provides
public OkHttpClient provideOkHttpClient(final Network network) {
    if (network != null) {
        final OkHttpClient httpClient = new OkHttpClient();
        httpClient.setSocketFactory(network.getSocketFactory());

        Internal.instance.setNetwork(httpClient, new com.squareup.okhttp.internal.Network() {
            @Override
            public InetAddress[] resolveInetAddresses(String host) throws UnknownHostException {
                return network.getAllByName(host);
            }
        });

        return httpClient;
    }

    return null;
}
alex
  • 6,359
  • 1
  • 23
  • 21
  • I'm a bit confused... It looks like `provideNetwork` is grabbing a pre-existing Wi-Fi network connection. This might work, but as it stands the initial connection I make to the LAN is breaking my internet connection (Android is trying to access the internet through the LAN in other apps). Does this avoid this problem? – Daiwik Daarun Jul 30 '15 at 02:33
  • 1
    Which version of Android are you using? – alex Jul 30 '15 at 02:36
  • 1
    This code only creates a HttpClient that only uses the WiFi, it doesn't connect to any networks. – alex Jul 30 '15 at 02:36
  • Thanks for the fast response! Im using Android L. Do you mean the OkHttpClient returned is able to scan available networks and exclusively connect to a specific one (e.g. the LAN)? In other words my current internet connection won't be affected, because that network will not be disrupted – Daiwik Daarun Jul 30 '15 at 02:41
  • 1
    This HttpClient will only use the WiFi for data. Nothing to do with the network the WiFI is connected to. To choose a WiFi network to connect to see WiFi manager. https://developer.android.com/reference/android/net/wifi/WifiManager.html – alex Jul 30 '15 at 02:43
  • I think I see the problem (correct me if I misunderstood). What you're describing will force data over WiFi. What I want to do is connect to two networks simultaneously - one for LAN and one for internet – Daiwik Daarun Jul 30 '15 at 02:49
  • Ok. I was hoping this might be able to be done if an internet connection was not being used on one of the networks (similar to how you can use Wi-Fi Direct and still be connected to a network) – Daiwik Daarun Jul 30 '15 at 02:52