How to check whether 3G is enabled when connected to Wifi? - android

I want to allow the user to toggle 3G state (ON/OFF) even when the device is connected to Wifi (I read here that disabling 3G while connected to Wifi saves battery).
However, when I check the 3G state when the device is connected to Wifi it always return disconnected.
My code is:
// access to mobile networtk service - 3G
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// check mobile status
NetworkInfo.State state = mobile.getState(); // returns Disconnected
Boolean isConnected = mobile.isConnectedOrConnecting(); // returns false
if (isConnected)
{
// Turn 3G off
}
else
{
// Turn 3G on
}

According what i know, When a wifi is connected the Mobile Network Connections are automatically disabled in Android, so if you check the status of 3G it will be disconnected only. You don't need to do this programmatically, it is already present in Android System.
Even though in the Navigation panel you may see 3G enabled, but it is not ON simultaneously with WiFi.

Related

Difference between `isConnected()` and `isAvailable()` in android `NetworkInfo`

I'm trying to check if the device is connected to internet or not. I have the below implementation to do that
public static boolean isConnectedToNetwork(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
NetworkInfo provides two methods isConnected() and isAvailable(). Which one should I use and what is the difference between them.
And is there a way to detect the state where the device is connected to Wifi without an internet connection?
If the device is connected to a network, isConnected returns true.
If the device is not connected but network is available to connect, isAvailable returns true, isConnected returns false.
You can read this topic for find your last question.
Android Check if there is WiFi but no internet
isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
- Always call this before attempting to perform data transactions.
isAvailable()
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include
- The device is out of the coverage area for any network of this type.
- The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
- The device's radio is turned off, e.g., because airplane mode is enabled.
Reference Link

Android - force network requests go through wifi instead of mobile network

I have an app which connects to a hardware device Wi-Fi hotspot.
It seems that Android forward requests over other networks (3G/4G for example) instead the hotspot, since my hot spot has no internet connection.
Is there any way to force network stream to work on the wifi?
I've come across the following function, but it's deprecated:
https://developer.android.com/reference/android/net/ConnectivityManager.html#setNetworkPreference(int)
Per the Connecting your App to a Wi-Fi Device blog post:
To direct all the network requests from your app to an external Wi-Fi device, call ConnectivityManager#setProcessDefaultNetwork on Lollipop devices, and on Marshmallow call ConnectivityManager#bindProcessToNetwork instead, which is a direct API replacement.
Use the ConnectivityManager to get the state of the Wifi adapter and then you can check if it is connected or even available using NetworkInfo.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Continue with logic
}
In case he is not connected to wifi then don't proceed else continue you flow.
Add the following permission in you Manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

what does "isRoaming" in the Wifip2p networkinfo mean?

What is the meaning of isRoaming in Wifip2p network info mean?
[http://developer.android.com/reference/android/net/NetworkInfo.html#isRoaming()]
It tells that usage of data on that network may incur extra charges? Which data is it refering to and how the usage will lead to extra charges?
Well, if you're outside your home country/state and you turn on the phone it will automatically establish a GSM connection to the roaming partner network -> TelephonyManager.isNetworkRoaming() will return true. If data roaming is disabled or 'use only 2g' is enabled NetworkInfo.isRoaming() will return false, because no data connection is established. If you switch data services on NetworkInfo.isRoaming() will return true as well, since now both (GSM and data connection) are established and in roaming mode.
Hence, setting data-roaming on/off will make NetworkInfo.isRoaming() return true/false.
UPD: It is used when one wants to know whether the user has enabled Data on Roaming while on 2G/3G network.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
//...Here check if this is connected and available...
if (ni.isRoaming())
{
// user has enabled data even while he is on Roaming!!
}
}
Since he asked specifically about Wifi P2P the correct answer that this will be an unused field as roaming only applied to cellular network and does not apply to Wifi P2P network information.
The only fields of networkinfo you should be using concerning wifi P2P is: isConnected (after which you normally request the connection info from the wifip2pmanager).

Android connect to Wi-Fi AP when screen is off

I'm working on a project which need to connect to a Wi-Fi in a background service, the service running when the device is screen off.
The connecting code is like below:
public boolean connect_android(String ssid) {
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
boolean find = false;
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
find = true;
break;
}
}
while after the connecting code is executed in the background running service, the device never connect to the Wi-Fi successfully until the screen is turned on. I logged the supplicant state, it is in complete state. As the google docs says:
This state indicates that the supplicant has completed its processing for the association phase and that data connection is fully configured. Note, however, that there may not be any IP address associated with the connection yet. Typically, a DHCP request needs to be sent at this point to obtain an address.
So can i come to the conclusion that, when the screen is turned on, a DHCP request is send the device receives an IP and the connection is complete successfully. But who is in charge of sending the DHCP, the wpa_supplicant or Android framework, is there any docs about this? How can i connect to a Wi-Fi AP without turn on the screen? Thanks in advance!
Maybe because your device use feature turn of wifi when screen of (Settings -> Wireless and network -> WiFi settings -> (menu button) Advanced -> Wifi sleep policy)
Your app will cannot connect to network

How to detect in Android that WIFI is ON, but unreachable?

Does anyone know how to programatically detect in Android that wifi is unreachable?
I've got an app running where I am able to easily detect type of network connection and whether is on or not (NetworkInfo class), but I can't detect that wifi is ON, but I am out of reach on the other side of building, so there is no internet. Network info keeps on saying that type of network is WIFI, isConnected = true, getDetailedState = CONNECTED, so these don't help me.
Detect WIFI is connected or on. If wifi is not connected to any INTERNET connection then display message that "WIFI is unreachable or no INTERNET connection."
For checking WIFI is connected use following code.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}

Categories

Resources