/**
* 获取mac地址,如果wifi没有开启则尝试开启wifi并获取mac地址,
* 为了防止NAR最好放在子线程内执行
*/
public String getMac2() {
String macSerial = "";
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
boolean state = wifiManager.isWifiEnabled();
if (!state) {
wifiManager.setWifiEnabled(true);
}
macSerial = getMac();
int time = 50; // MAXTIME;
while ((macSerial == null || macSerial.equals("")) && time > 0) {
try {
Thread.currentThread();
Thread.sleep(300);
macSerial = getMac();
time--;
} catch (Exception e) {
e.printStackTrace();
}
}
return macSerial;
}
/**
* 直接获取mac地址,如果wifi没有开启则获取不到
* @return
*/
public String getMac() {
String macSerial = "";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {
macSerial += line.trim();
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return macSerial;
}
安卓系统获取mac地址
最新推荐文章于 2024-08-15 16:27:36 发布