How can I enforce internet connection only through wifi? - android

We have a Wifi-connected device. We need to connect to the device via wifi for installation. Once connected, the application sends the request on a ip.
This works smoothly before android 9. But I'm having some problems when I do with android 9. Because when i connect to device i getting error message like this: wi-fi has no internet acces. And requests continue on mobile data. So app can't talk with device.
Can i doing something about this problem?

I guess this is related to permissions since API level 27 you need either
android.permission.ACCESS_FINE_LOCATION
or
android.permission.ACCESS_COARSE_LOCATION
permission.
You need to CHANGE_WIFI_STATE for Android Pie.
Try this
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();
String name = networkInfo.getExtraInfo();
String ssid = "\"" + wifiInfo.getSSID() + "\"";
}
You can study about these changes in detail.
Refer Here

Related

How to get current wifi connection name in android pie(9) devices?

I know it is very simple for you. Here I am just tried to get WiFi name in android pie devices. I am able to get WiFi name till Nogout devices with the help of below line of code.
String ssid = wifiInfo.getSSID();
I have tried lots of answer and Android developer docs but unfortunately, I can not get a WiFi name on my mobile(Nokia 6.1 plus). I Know I am doing mistakes.
I just want a code there I can get a wifi name from my mobile to my android studio logcat.
This is related to permissions....since API level 27 you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission. You may also need CHANGE_WIFI_STATE for Android 9 (that's the case for wifi scan anyway as per google permisson model
then try this code
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();
String name = networkInfo.getExtraInfo();
String ssid = "\"" + wifiInfo.getSSID() + "\"";
}
Try to get SSID value using
WifiManager wifiManager = (WifiManager)
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiInfo = wifiManager.getConnectionInfo();
if it returns SSID as null, then turn on the location and try the same.
Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before retrieving the connected SSID.
Check this link

Is connecting to a Network means that I am connecting to a WiFi

Given that, i have only WiFi networks available on the device and no mobile networks, only WiFi.
If I cheecked the connectivity to a network using connectivityManager and the result of it is that "I am connected", does that mean I am also connected to a WIFIalso? If yes, why I can not get info about my WiFi such as ssid,speed, link speed, ip and others from the Connectivity Manager object? Because, to have such info you have to be connected to a WiFi first.
You can check if you are connected & connected to a Wifi
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
You can use Wifi Manager to get the information about Wifi Network
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
More info on the WifiInfo
http://developer.android.com/reference/android/net/wifi/WifiInfo.html
I think being connected to a network indicates that you are connected to the internet whether through WiFi, Mobile network, Bluetooth or any other network.
And if you are connected to the internet through a WiFi, then, of course, you have connected to a network but through a specific network.
So, the connectivity manager class, has all the network info about the network through which you are connected to the internet. and since it is a generic class you can obtain info about the network through which you are connected to the internet.

getAllNetworkInfo detect wifi or mobile connection, but never both

I have a function which check network connections using:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
but, as soon the Wifi network is connected, the mobile network is given as DISCONNECTED
How could I know if both networks are connected?
Thanks in advance for your time.
Try this:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNet = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(netInfo!=null){
if(mobileNet.isConnected()){
System.out.println("Mobile Network is connected");
}
if(wifi.isConnected()){
System.out.println("Wi-fi Network is connected");
}
}
How could I know if both networks are connected?
You can create for sure method that will check for both types e.q.
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// do your stuff
}
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
// do your stuff
}
But in Android OS, if Wifi and Mobile network is available (and you can connect to), OS will choose Wifi by default so it'll use Wifi as active network connection.
Android device can have only one connection! (WiFi or Mobile)
If WiFi is available - system will try to connect to it
If WiFi is not available and Mobile is turned on - system will try to connect to Mobile
When system is connected to WiFi - You cant' be shure that Mobile will work until you try to connect.
Same for Moble

Android terminate wifi ability within application

One of the major requirments of my application is using cellular internet connection only.
In case of detecting wifi connection, the application have to disable the wifi ability and connecting through the cellualr internet connection.
How it can be done?
Thanks,
Eyal.
Checking WIFI connection
You can use the following code to check if the active network (if any - do some null checks here) is of type WIFI.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo!=null) {
// detect type using netInfo.getType()
}
There's a TYPE_WIFI int constant in ConnectivityManager that you can use for your check. (getType returns an int to identify the network type).
More info on the Activity Manager can be found on the Android dev site.
Disabling WIFI
n order to disable the WiFi state, you have to grant the following permission in the application manifest
android.permission.CHANGE_WIFI_STATE
You can then use the WifiManager to set enable/disable the WIFI.
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
More info on http://developer.android.com/reference/android/net/wifi/WifiManager.html
For a complete sample, check the following post : http://android-er.blogspot.com/2011/01/turn-wifi-onoff-using.html

Android Wi-Fi: 'Address family not supported'

I am making an application which makes use of the internet.
It works fine when the Wi-Fi is availble. But when the Wi-Fi is turned off, it gives me an error: "Address family not supported".
I am using the NetworkInfo Class to know, if the network is available. Im using WifiManager to enable and manage Wi-fi connection if available.
I've been using the following code to check if a network is available:
ConnectivityManager mConnectivity = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null) {
// No inet
}

Categories

Resources