How to get MAC Address of device using Wifi in Android -


i want access mac address of phone using wifi. somehow not right. 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 run on phone, give me error "unfortunately application stopped" when run program on imulator, gives address null. please me rid of it. in advance.

my manifest file 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 working properly,just remove asynctask line , put toast message. code run on device.its working properly. have checked code.i think have error in async task.

try {             wifimanager wifi = (wifimanager)                     .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();         } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -