1.简单 暴力直接上代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv_mac= (TextView) findViewById(R.id.tv_mac);
tv_mac.setText("本机的mac地址"+getDeviceInfo(this));
Log.e("mac",getDeviceInfo(this));
}
private String getDeviceInfo(Context context) {
try {
String mac = null;
FileReader fstream = null;
try {
fstream = new FileReader("/sys/class/net/wlan0/address");
} catch (FileNotFoundException e) {
fstream = new FileReader("/sys/class/net/eth0/address");
}
BufferedReader in = null;
if (fstream != null) {
try {
in = new BufferedReader(fstream, 1024);
mac = in.readLine();
} catch (IOException e) {
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return mac;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.效果图