I want to access MAC Address of phone using Wifi. but somehow I could not get it right. I have written following code.
context=this;
setContentView(R.layout.activity_main);
try{
WifiManager wifi=(WifiManager)this.context.getSystemService(context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
WifiInfo info=wifi.getConnectionInfo();
String address=info.getMacAddress();
if(address==null){
Toast.makeText(context, "Null", Toast.LENGTH_LONG).show();
}
else{
new AsyncClass(MainActivity.this,address).execute();
}
}catch(Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
When I run this on my phone, it give me error that "Unfortunately Application Stopped" When I run this program on Imulator, it always gives address is Null. Please help me to get rid of it. Thanks in advance.
My Manifest file is as below:
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
Your code is working properly,just remove asyncTask Line and put Toast message. Then your code will run on device.its working properly. I have checked your code.i think you have to get error in async task.
try {
WifiManager wifi = (WifiManager) this
.getSystemService(this.WIFI_SERVICE);
wifi.setWifiEnabled(true);
WifiInfo info = wifi.getConnectionInfo();
String address = info.getMacAddress();
if (address == null) {
Toast.makeText(this, "Null", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, address, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}