1. 程式人生 > >Android 不開WiFi獲取Mac地址 夜未央

Android 不開WiFi獲取Mac地址 夜未央

這是我在網站上找到的資料 整理一下 不知道哪天會用到 尷尬

//在不開起WiFi的情況下獲取mac地址

public static String getLocalMacAddress() {
String mac = null;
try {
String path = "sys/class/net/eth0/address";
FileInputStream fis_name = new FileInputStream(path);
byte[] buffer_name = new byte[8192];
int byteCount_name = fis_name.read(buffer_name);
if (byteCount_name > 0) {
mac = new String(buffer_name, 0, byteCount_name, "utf-8");
}


if (mac == null) {
fis_name.close();
return "";
}
fis_name.close();
} catch (Exception io) {
String path = "sys/class/net/wlan0/address";
FileInputStream fis_name;
try {
fis_name = new FileInputStream(path);
byte[] buffer_name = new byte[8192];
int byteCount_name = fis_name.read(buffer_name);
if (byteCount_name > 0) {
mac = new String(buffer_name, 0, byteCount_name, "utf-8");
}
fis_name.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


if (mac == null) {
return "";
} else {
return mac.trim();
}
 
}

//未免別的小夥伴要用mac地址做裝置唯一標示 此方法是獲取裝置唯一id  用mac地址 還是裝置id 看小夥伴自己意願吧

public static String getDeviceId(Context context) {
    return Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
要是有問題 還請小夥伴指出來 頭次發部落格