Check Network connectivity and obtain Wifi MAC address in Android

Update: 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.

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:

/**
 * 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

android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE

uses permissions in AndroidManifest.xml file.

Advertisements

About cmanios

programming
This entry was posted in Android, Java and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s