How can I get unique device id in android 10?

∥☆過路亽.° 提交于 2020-12-13 17:57:25

问题


I want to get a unique device id in my application. But As per google document says Android 10 adds restrictions for non-resettable identifiers.

I don't want random id. I want to get the unique id so I can identify the device from which my API gets hit. and by that, I can manage my session. is there is another unique id other than Secure.android_id which is unique for android? or is there any way to access the Secure.android_id in Android 10.


回答1:


You will get the wifi mac address by using the following code, regardless whether you used a randomized address when you tried to connect to the wifi or not, and regardless whether the wifi was turned on or off.

I used a method from the link below, and added a small modification to get the exact address instead of the randomized one:

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



回答2:


As per my thought you should use below code to get unique Id -

String uniqueID = UUID.randomUUID().toString();

the above code does not need any run time permission from user to access device id and always generate unique id.you can map this uniqueID with user data or manipulate according to you need.

Thanks




回答3:


You can get Android Unique Id for device by using this, for this You Need to get android.permission.READ_PHONE_STATE

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 


来源:https://stackoverflow.com/questions/60501286/how-can-i-get-unique-device-id-in-android-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!