權限:
<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();
}
WifiManager wifiMan = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
return wifiInf.getMacAddress();
}
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "02:00:00:00:00:00";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
device_serial = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
} else {
device_serial = Build.SERIAL;
}
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(device_serial.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuilder hexString = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
String h = Integer.toHexString(0xFF & aMessageDigest);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
device_serial = hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Android 10强制要求MAC是随机的,上面的方法取得也是随机的,真实的IP需要使用DevicePolicyManager.getWifiMacAddress(@NonNull ComponentName admin).这个权限基本没有,大家可以不用想了。
藍芽MAC ?
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";
}
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";
}
String androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
String uuid = UUID.randomUUID().toString();
沒有留言:
張貼留言