Android: How to determine which Wi-Fi network is connected - android

I know how to determine if a wi-fi connection is the active connection. However, can someone tell how to determine which wi-fi network is the current active?

Use WifiManager.getConnectionInfo(). This returns a WifiInfo object which contains all the info you need about the active wifi connection; in particular, WifiInfo.getSSID() will give you the SSID.

WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
String BSSID=wifiManager.getConnectionInfo().getBSSID();

Related

Android WifiManager disconnect from a WifiSpot but never connects automatically again

I am using this code to disconnect from a Wifispot:
if(getActivity()!=null) {
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
}
WiFi disconnects and Settings show Wifi On, the wifi I was connected is shown as
Saved, secured with WPA/WPA2
That's perfect. The problem is that I get out of range and return into the range again of the WiFi, it doesn't "reconnect" automatically. Which code should I add to automatically reconnects WiFis disconnected by WifiManager?
When you set up your wifi configuration.
add this :
mWifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
for more information http://developer.android.com/intl/es/reference/android/net/wifi/WifiConfiguration.html
Hope this help!

Android WiFi Connection Info

I'm getting wifi connection info this way:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
But, could getConnectionInfo method return null in any case? Android documentation reference say this:
Return dynamic information about the current Wi-Fi connection, if any
is active.
I guess if the device is not connected to any network it will return null, won't it?
Thanks
Yes it will return null, if it is not connected to the wi-fi.
But if you are also checking whether internet is available by using above code, then it is not 100% correct way to do so.
Cause it may happen that your device is connected to wifi but no internet is available. so for that
you have to ping any live site (such as google,facebook, or any other trusted website) and check if the returned status code is 200.
I hope this helps.

Find available wifi connections in Android

I am trying to create a help wizard to recover from bad network connections in the app. One test case I hope to handle is the case where an end user has WiFi turned off, WiFi is available, and the mobile network is slower than the WiFi network. In this event, I want to be able to (1) discover the available WiFi network s, (2) find the WiFi network speed, (3) Compare its speed to the mobile network speed, (4) digest the user changes to the faster network.
For this to work, I need to know how to programmatically get information on available connections. Is that something we can do? If so, how can we tell what connections are available? Thanks in advance.
Task-1: Discover the available WiFi network
This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
Task-2&3: find the network speed
This link gives an answer to your question about how to get network speeds of wifi and mobile network
Wifi:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
In case of mobile it should work:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4: Switching to the option with higher speed
In Android by default, if wifi is on and connected then your mobile network won't be used. Hence to use mobile data you must either disconnect from all available wifi-networks or switch off the wifi.
I will also suggest you to read link this, this and this for getting more information on how to get connection speed.

How can I learn SSID of that I am connected to wifi point?

I want to learn the connected wif-fi point SSID. By doing so, I will not try to connect to the same point. How can I learn SSID of that I am connected to wifi point ? Is there a method like isConnected() ?
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("wifiInfo", wifiInfo.toString());
Log.d("SSID",wifiInfo.getSSID());
you will need the android.permission.ACCESS_WIFI_STATE in your manifest
There is one problem:
I selected wifi "XYZ" whoz pwd was unknwn. Entered some random pwd.
It shows authentication problem. on wifi screen.
But, Log.d("SSID",wifiInfo.getSSID());
Results into "XYZ"

how to get the rate of wifi connection on android?

I want to know if somebody can tell how can I get the rate of wifi connection on my android phone?
You find the connection speed programmatically in Android as follows:
WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
Note that you should use the ConnectivityManager to state your preferred network if you are anticipating use of wifi over other available networks, otherwise the speed may not actually be in use

Categories

Resources