網路服務常需要拿 IP 或 MAC Address 來做存取管控。筆記一下。
權限:
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
程式碼:
public static String getMacAddress(Context context) {
WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
return wifiInf.getMacAddress();
}
public static String getIPAddress(Context context) {
WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
long ip = wifiInf.getIpAddress();
if( ip != 0 )
return String.format( "%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
}
return "0.0.0.0";
}
這是可以對手機wifi的mac address 做亂數更動嗎?
回覆刪除