Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to use a Wifi network for specific hosts but have phone use Mobile network for everything else

Tags:

I am developing a mobile application (iOS and Android) to control a device over Wi-Fi. The device creates a wireless network (SoftAP) but does not provide access to the internet.

On iOS, I can connect to the device and make requests to its IP address (192.168.70.1) but all other requests fall back to the mobile network. This allows the phone to maintain internet connectivity while connected to the device over Wi-Fi.

On Android, if I connect to the device wireless network, internet requests do not fall back to the mobile connection, they just fail.

In my Android app, I can use ConnectivityManager.requestRouteToHost to force requests from my app to use the mobile network. However, requests made by other apps still use the device wifi network and fail.

I have also tried to use the ConnectivityManager to change the network preference with :

ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE)

This causes the phone to use the mobile network for all requests from all applications. Wi-Fi is disabled. It seems that in previous versions of Android Wi-Fi could still be used even if it is not the "preferred" network but this does not seem to work in KitKat. It is possibly related to a change in Android 4.2: https://code.google.com/p/android/issues/detail?id=73509 "ConnectivityManager since 4.2 tears down networks that are not the NetworkPreference"

Is there a solution that allows an Android app to use Wi-Fi for a specific IP address and the mobile network for everything else? Maybe this can be done via the NDK?

like image 896
Paul Greyson Avatar asked Sep 13 '14 00:09

Paul Greyson


People also ask

How can I use both Wi-Fi and mobile data simultaneously?

To do this, swipe down on your notification bar and check that the mobile data toggle is switched on. Or go into “Settings,” tap “Connections,” and “Data Usage” and make sure that mobile data is switched on. Step 2: Connect to a Wi-Fi network. Tap “Settings,” then “Connections”, then “Wi-Fi” and flip the switch on.

Can phone data be used over Wi-Fi network?

The answer is no. Generally, when your phone is connected to your home or any other Wi-Fi network, it will not connect to the 5G, 4G, 3G, or any type of wireless carrier network. Any data used via Wi-Fi will not count toward your data plan.

Can Android connect to two Wi-Fi networks?

You can only be connected to one network at the time. However, there is a standard called WiFi direct that will allow you to do what you are asking for, but it isn't yet implemented in Android.


2 Answers

Actually you can, but only since Lollipop (API 21).

From the Android API 21 Documentation:

    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. 

So you can use the NetworkRequest.Builder class to create a NetworkRequest that sends the request over a certain Transport Type.

Use the following function:

NetworkRequest.Builder addTransportType(int transportType) 

To set transport types (WIFI, ETHERNET or CELLULAR for example) for your requests.

like image 162
Robin Eisenberg Avatar answered Oct 21 '22 14:10

Robin Eisenberg


Android normally tears down the networks that are not network preference. A work-around you could use is create an AP through the mobile app and let the device connect to it. This way the mobile can maintain internet connectivity as well as communicate with the device.

Refer the following links for implementation details:

https://github.com/opengarden/android-tether

https://code.google.com/p/android-wifi-tether/

like image 22
Shadab Mirza Avatar answered Oct 21 '22 15:10

Shadab Mirza