Question

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"/>
Was it helpful?

Solution 2

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();
        }

OTHER TIPS

Please add a permission at manifest as follows

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

write a functin/method retrieveMacAddress as follows

public String retrieveMacAddress(Context context) {
    WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String macAddress = wimanager.getConnectionInfo().getMacAddress();
    if (macAddress == null) {
        macAddress = "Device don't have mac address or wi-fi is disabled";
    }
    return macAddress;
}
public void uploadMacAddressTOserver(String address) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    CallWebserviceToUploadMacAddress(address);
                } catch (IOException e) {

                }

            }
        }).start();
    }

You can use thread and upload your data to server. May be in AsyncTask some you have to pass some wrong parameter so it gives error.

This is my code and works well in android 5 +.

public static String getMacAddress() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }
            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                // res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:",b));
            }
            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
        //handle exception
    }
    return "";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top