Android force traffic over internet while WiFi enabled - android

I'm starting to write an Android application to perform background monitoring of my web server. With WiFi enabled a problem will be that traffic is directed over that by default, I'd like to always use 3G for the HTTP request to check external availability as well. I know I could use this code to disable WiFi programmatically:
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
But as it will be running in the background it's a bit clumsy and will interrupt other operations over WiFi while the check is in progress. Looking at the Socket Documentation I can't see any apparent way to achieve this. I'm wondering if there's any supported way to achieve this on a non-rooted phone? I have full control over the server so the protocol doesn't specifically have to be HTTP.

This seems like it would be an awfully dangerous feature for Android to allow.
What about users who are on metered 3G plans? How would they feel if an app forced them to use a 3G connection and then bugged out or something and blew away their whole data quota without them even knowing it (thinking they were on WiFi)?
Also, there could not be more than one active networks at a time.
Possible Resolutions-
What you can do is to force disable the wifi network, when your application is active and then enable it later.
Also, Try searching for a method called requestRouteToHost. It allows you to specify the network type and the host you want to find a route to.

I don't believe you can do this in android since only one network connection is active at any given time. A similar question is posted here as well:
Send HTTP request through 3G network without using WiFi?

Related

Using both Wifi and mobile network at the same time in flutter app

In my flutter app, I need to get information from a device via wifi as a stream. Also I need to keep my google maps online via Mobile internet. How can I use both wifi and mobile network at the same time in my phone. I made a lot of search but I can't find any solution for this.
I don't think it's possible.
The phone would need two network antennas and possibly two network adapters.
You can surely toggle both wifi and mobile data at the same time and they can probably switch frequently to optimize your connection or send requests to different services.
But you can't really use them both at the same time.
(if it's possible) I don't recommend it, since a majority of phones will not support this feature since you need two network antennas.
From the facts as presented, there are a couple of possible solutions, but they aren't perfect and I'm guessing on one as I haven't tested it yet.
One, Google Maps has an offline mode. You can set it up to download maps for offline use. It will show your position on the maps whether you are connected to the internet or not, but you won't get traffic data or accurate travel times. Not great, but it can work.
Two, it is possible for a phone to have a WiFi network and Cellular network active at the same time, but not in the way everyone seemed to think. Since you are wanting the cellular connection open to have access to the internet, I'm assuming this is because the WiFi network you want to access doesn't have the internet. So, reverse the WiFi network. Setup your phone to be a hotspot network, it runs its own WiFi network while maintaining access to the internet through its cellular network. I'm guessing here, but unless phones have some specific security protocol forbidding it I don't know about (which can be the case), your app should be able to contact anything connected to its phone's WiFi while still having access to the internet.
If this doesn't work for some reason, you can try a trick I used when I set up a car office. Get a mobile hotspot device. It's a device you can put a cellular SIMM in and it creates a WiFi hotspot. You can connect to it, along with other devices in the car. All will being using WiFi and the hotspot will provide the internet access.
If what you are connected to can only create a WiFi network for you to access and it can't be configured to connect to one itself, you could technically still be able to do it by network switching. Much harder to do (probably impossible on some devices) and will likely mess with other apps depending on internet access, so a real break glass in case of emergency type of situation there.

Send a POST request only through WiFi interface

I have a chip with a WiFi module on it, which acts as an access point. What I'm trying to do is connect to the AP (which has no Internet access), send a POST request to a local address (http://192.168.4.1/address) and receive a response from the chip. I am writing an Android application, which is supposed to do that (and it does most of the time).
The problem is, I have a test device, which has this optimizing feature and checks if the WiFi network you're connected to has Internet access and if not, it uses mobile data automatically. I don't want that so I would want to either "force" the application to send it through the WiFi interface or find a workaround.
I use HttpURLConnection at the moment and it works like a charm if I turn off my mobile data. Otherwise it just waits and at the end triggers the timeout.
I have searched a lot about this issue and so far I have found nothing.
Since Android 5 (API 21), you can force connections to use the WiFi even if it's not the default network.
One solution is to find the corresponding network, for example with ConnectivityManager.getAllNetworks() and ConnectivityManager.getNetworkInfo():
Once you have the Network, you can either :
Open a connection on this network using Network.openConnection().
Bind the application to the network with ConnectivityManager.setProcessDefaultNetwork or ConnectivityManager.bindProcessToNetwork() (API 23+)
See Connecting your App to a Wi-Fi Device (especially Routing network requests) for more details.

Can I instruct my network requests to only use WiFi, even when both WiFi and Data networks are available?

As far as I know, the hardware/os decides which network will execute my app's requests, if both MobileData and WiFi networks are on, judging by this answer here: In Android, if both mobile and wifi networks are available, which one is used?.
This comment here says the same: https://android.stackexchange.com/questions/57710/how-do-you-determine-if-youre-using-wi-fi-or-lte#comment79856_57919
Can I instruct my network requests to only use WiFi, even when both WiFi and Data networks are available?
I am trying to solve this problem: The user has a setting that says "Do NOT use mobile data" which should forbid the app from sending network requests if Mobile Data is on (which can be subject of charges).
However, if that answer to my linked question is really true, that means that if both Mobile and WiFi are on, WiFi is not guaranteed to take precedence over Mobile and Mobile will still perform requests and the user will be charged.
At the moment in my code I check what type of network connection is available, and if that setting is on ("Do NOT use mobile data") I only perform requests if WiFi is ON and MobileData is OFF. If both are ON, I do NOT perform requests in order to protect the user from being charged.
However, it is difficult explaining this to my colleagues, so I am thinking - is it possible to instruct my app's network requests to be only performed via WiFi, even if both WiFi and MobileData are ON simultaneously?
PS: I just found an exactly opposite opinion, stating that if both MobileData and WiFi are on, all requests will be performed via WiFi:
https://android.stackexchange.com/questions/17774/how-can-i-tell-that-my-phone-uses-the-wi-fi-connection-for-data-traffic
I am not sure who to believe.
EDIT: I found some code snippets that are supposed to do this job:
Send request over Mobile data when WIFI is ON.(Android L) but I don't know how to test them and if they really do what I want to do here.
How to use data connection instead of WIFI when both are enabled?

How to Cheat Android at checking Connectivity?

I'm doing a network App using tun as virtual network device on Android. I close other available interfaces such as eth0 (for WLAN) and rmnet0 (for 3G/GPRS) but keep my tun0 device alive and add a rule in route table
ip route add default dev tun0
so I can redirect all internet transfer into tun device (what I'm doing here is the same as OpenVPN). And then I saw the data transferring when I ping other IP. Browser and some other Apps work fine.
But some of Android Apps will check the Connectivity by ConnectivityManager before accessing netwrok. Since I close WIFI & 3G/GPRS, ConnectivityManager considers there is no connection, those Apps won't access network anymore.
So my question is:
How to CHEAT android ConnectiviyManager, make it believe that there is still an available connection (for any type)?
I don't think you can do this, and you wouldn't want to, it's a system-wide service. Imagine if your app relied on internet connectivity, but some malicious app was able to tell you there was and make your app unable to complete it's actions. It could cause a serious problem.
If your app needs to believe there is a connection in some states, I suggest you interface the connectivity / network classes, you can read the true state but then return whichever value you deem fit to your app

How do I wait during application startup until WIFI connects?

Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks
Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes
Use WifiManager to get the state of the connection.
Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.

Categories

Resources