3

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.

Ankur Shinde
  • 304
  • 4
  • 19
  • 1
    You can read about your options and decide accordingly from the link below: https://developer.android.com/training/articles/user-data-ids – Dogu Deniz Ugur Mar 03 '20 at 06:40

3 Answers3

3

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();
}
  • Can you explain your code? Like why does it always return the "real" mac address instea of the randomized? Does it change if we restart the phone / factory reset? – Hiraeths Nov 16 '20 at 04:32
  • Hello, it returns the real mac address because this is what we need from this method. Regarding the randomization, you know that in android 10+ if you used a randomized mac address while connecting to wifi, then you tried to get the mac address using any other methods to get the mac address, you will get the randomized one instead of the real one. And this method always gets the real one and it will not be changed if you turned off wifi or restarted the device or performed a factory reset or firmware for the device. – Abdallah AlTaher Nov 18 '20 at 07:59
  • What if I use a rooted phone and spoof/change my mac address? Will it return real mac address? – Hiraeths Dec 02 '20 at 10:56
  • Honestly, I didn't test it on a rooted device, if you have one test it and share the result with us. – Abdallah AlTaher Dec 06 '20 at 09:52
2

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

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

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
  • 2
    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 it any way to access the Secure.android_id in Android 10. – Ankur Shinde Mar 04 '20 at 12:10
  • 3
    Hi So how did you solve your problem to get the permanent unique Id – rohit garg Oct 26 '20 at 09:19
  • I directly added the session ID from the server. As the latest devices are not providing the ID. – Ankur Shinde Aug 12 '22 at 03:43