I want to detect whether or not a device has mobile data capability. By mobile data capability I don't mean an active or connected mobile data connection, just the ability to use mobile data.
I currently use the following
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
//I assume device has mobile data capability
}
This works fine for most cases but one - If the device does not have calling facility but has the capability to utilize a cellular network for data connection only. Such devices are typically tablets which have a SIM card slot but it can be used only for data connection, not calling.
How can I detect whether the device has mobile data capability in this case and in all other cases? What is the best method?
I found the solution myself. Posting it for others who might need it.
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (ni == null) {
// Device does not have mobile data capability
}
From the documentation of getNetworkInfo(int networkType):
Parameters: networkType integer specifying which networkType in which you're interested.
Returns: a NetworkInfo object for the requested network type or null
if the type is not supported by the device. This method requires the
caller to hold the permission
android.Manifest.permission.ACCESS_NETWORK_STATE.
This method can be extended to check for other types of network as well. Just put the required networkType in getNetworkInfo().
Personally, I perform this test only if
getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
returns false. This check can then confirm whether or not device has mobile data capability even though it does not have telephony feature (which is the case with some tablets). If device has FEATURE_TELEPHONY, I assume it has mobile data capability. This way we can possibly reduce execution time in most cases.
hmm I have not tested this yet but if it has a data connection then its location over network probably is enabled, thus you can use FEATURE_LOCATION_NETWORK
Use ConnectivityManager and NetworkInfo to check the current state of connection:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo eventInfo = cm.getActiveNetworkInfo();
//first check to see if an active connection exists
if (eventInfo != null
&& eventInfo.getState() == NetworkInfo.State.CONNECTED) {
//now checks for the type of connection
switch(eventInfo.getType()) {
case ConnectivityManager.TYPE_MOBILE:
case ConnectivityManager.TYPE_MOBILE_DUN:
case ConnectivityManager.TYPE_MOBILE_HIPRI:
case ConnectivityManager.TYPE_MOBILE_MMS:
case ConnectivityManager.TYPE_MOBILE_SUPL:
//is mobile connection
break;
}
} else {
//device currently in a disconnected state, including connecting
}
*psudo code, may not 100% work on copy & paste
Related
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
I want to write an xposed module where I can redirect VoLTE calls through VoWiFi. I want to know which method decides if the call is going through VoLTE or VoWifi and I would hook that method and get the work done.
Basically using WiFi I want to give the network an illusion that the device is using mobile data and send calls through wifi
Note : I am new to android programming. Excuse me if my question looks vague.
So you want to give illusion that you are connected to WIFI even though you are on mobile data.
the way we check that is this:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
So what you can do is, hook getType method of ConnectivityManager and in afterhook method always return ConnectivityManager.TYPE_WIFI
the way to do that is by calling
param.setResult(ConnectivityManager.TYPE_WIFI);//may be you need casting here
in afterHook.
read more about connectivity here:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
Edit: I hope you are familiar with afterHook and beforeHook methods and how xposed works.
My device is Nexus5 with Android 6.0.1, when I turn both wifi and cellular network on, I use the following code to check network type:
ConnectivityManager cm = (ConnectivityManager)mCtx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
mNetworkType = info.getTypeName();
}
}
}
Logger.d(TestUtils.TEST_TAG, "type is: " + mNetworkType);
the output type is : MOBILE, is this expected behavior of this api?
According to the Java Doc it delivers the default network.
#getActiveNetworkInfo()
Returns details about the currently active default data network. When
connected, this network is the default route for outgoing connections.
You should always check isConnected() before initiating network
traffic. This may return null when there is no default network.
This method requires the caller to hold the permission
ACCESS_NETWORK_STATE.
If you want to check for WIFI in particular you can use the method
#getNetworkInfo(Network)
Returns connection status information about a particular Network.
This method requires the caller to hold the permission
ACCESS_NETWORK_STATE.
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).
I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}