我想在我的应用程序中获得一个唯一的设备 ID。但正如谷歌文档所说,Android 10 增加了对不可重置标识符的限制。
我不想要随机 ID。我想获取唯一 ID,以便识别我的 API 从中被命中的设备。这样,我就可以管理我的 session 。除了 Secure.android_id 之外,还有另一个唯一的 id 是唯一的 android 吗?或者有什么方法可以访问 Android 10 中的 Secure.android_id。
最佳答案
无论您尝试连接wifi时是否使用随机地址,您都将使用以下代码获取wifi mac地址(对于某些设备,例如小米,这将返回wifi-direct mac地址)与否,无论 wifi 是否打开。
我使用了下面链接中的方法,并添加了一个小修改来获取准确地址而不是随机地址:
Getting MAC address in Android 6.0
public static String getMacAddr() {
StringBuilder res1 = new StringBuilder();
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("p2p0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
continue;
}
res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
}
} catch (Exception ex) {
}
return res1.toString();
}
关于android - 如何在 Android 10 中获得唯一的设备 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501286/