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


要是有問題 還請小夥伴指出來 頭次發博客 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章