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.
Related
I am developing and app for a restaurant. They want their customers to connect their Wifi via app.
The code:
WifiManager mManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
//Asume I added things to config.
int id = mManager.addNetwork(config);
mManager.enableNetwork(id, true);
So even if I don't call
myManager.saveConfiguration();
can the device connect that Router without my app?
Thank you for your time.
OK! I read about it more and more, I found the answer. And also the solution.
So, you need to use use
mManager.removeNetwork(id);
after it is disconnected. I assume you saved your BSSID's in somewhere. Also save the ID that you got. With a Timer task working at background check the current BSSID, if it is not the same, remove the ID from Configured Networks as above.
Here is how you check current BSSID:
WifiInfo wifiInfo = mManager.getConnectionInfo();
String currentBSSID = wifiInfo.getBSSID();
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!
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.
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();
Can my Android app find the MAC address of the Wifi access point it is connected to?
The docs for android.net.wifi.WifiInfo getMacAddress() don't provide any details.
See http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getMacAddress().
I'm assuming this is the Mac address of my phone. Can I find the Mac address of the access point?
getBSSID() of WifiInfo class will return MAC address of remote access point.
BSSID explained here.
The following method will return the MAC address of the access point, null if there is no network currently connected.
public String getMacId() {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
return wifiInfo.getBSSID();
}
Check out the application "Network Info II" from the Android Market. It does show the MAC address, but I'm not sure if this is still the phone's MAC. It also shows the BSSID, which has the same format as a MAC address so perhaps is what you're looking for.
I'm fairly sure that getMacAddress(), is, as you suspected for the Local Device.
If you can get the IP of the router/gateway/accesspoint, then you might be able to use the code in this post: https://web.archive.org/web/20160308014312/http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/ to do your bidding. Good luck!