简体   繁体   中英

How can I get unique device id in android 10?

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.

You will get the wifi mac address(for some devices such as Xiaomi, this will return the wifi-direct mac address) by using the following code, regardless of whether you used a randomized address when you tried to connect to the wifi or not, and regardless of 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();
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM