Updated: 2012-05-09
Today I wanted to check if my Android device is connected to a TCP/IP network. So I searched in the web for a while and I found this StackOverFlow answer on connectivity.
1
2
3
4
5
6
7
8
|
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
|
Also I wanted to obtain my Wifi network card MAC address. The code snippet to do this is the following:
1
2
3
4
5
6
7
8
9
10
|
/**
* Get Wifi network card MAC address
* @return mac address String
*/
public String getMacAddress() {
WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
return wifiInf.getMacAddress();
}
|
The above methods require
1
2
|
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
|
uses permissions in AndroidManifest.xml file.