Android Question Unique ID for Android 10+

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Well with Google doing away with us using AdID I am once again on a search for a way to tell if my app is running on a device the user paid for or are they sharing Google Log Ins

I came across this code (Unique ID / Android 10 >)
B4X:
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();
}

Not being very good (well actually pretty bad) I took Erel's (thanks Erel) code Udp Broadcast Address and made it look like Crap but it does return my Wi-Fi Mac address on Android 11

B4X:
Private Sub GetAddress As String
               Dim niIterator As JavaObject
          
               niIterator = niIterator.InitializeStatic("java.net.NetworkInterface").RunMethod("getNetworkInterfaces", Null)
          
               Do     While niIterator.RunMethod("hasMoreElements", Null)
                 Dim ni As JavaObject = niIterator.RunMethod("nextElement", Null)
                 Dim Name As String   = ni.RunMethod("getName", Null)
        
                 Log("GetAddress - Name:" &Name)
   
                 If  Name.EqualsIgnoreCase("p2p0") = False Then
                     Continue
                End If
   
                 Dim MacBytes() As Byte = ni.RunMethod("getHardwareAddress", Null)
              
                If  MacBytes.Length = 0 Then
                    Continue
                End If
              
                Dim BC            As ByteConverter
                Dim MacAddr        As String
              
                For Each B As Byte In MacBytes
                    MacAddr = MacAddr &BC.HexFromBytes(Array As Byte(B)) &":"
                Next

                If  MacAddr.Length > 0 Then
                    MacAddr = MacAddr.SubString2(0, MacAddr.Length-1)  
                  
                    Log($"MacAddr[${MacAddr}]"$)          
                  
                    If  MacAddr.Length > 0 Then
                        Return MacAddr                      
                    End If
                End If
               Loop
          
               Return ""
End Sub

The ONLY difference in the Mac Address it shows and my real Wi-Fi Mac Address is the one that is shown in this code starts with 16 and on my device and the Status Information shows it starting with a 14

Makes me wonder if I need to take off the FAKE IP 02:00:00:00:00:00 Taking that 02 off the first number would make everything exact.

Maybe someone could try this code and see what if outputs as far as what the Status Information page show.

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Tried on my devices Android 5.1.1 and Android 11 works fine.

Always first set is off by 2 - Even returns WiFi Mac Address when NOT connected to WiFi
I wasn't connected so when I went to my phone to look at status information nothing was showing - After I connected to wifi the address that showed in Status Information matched (minus the 2) what routine displays.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Glad to hear it. Will use as alternative check as well as others I use until
This Google Play services phased rollout will affect apps running on Android 12 devices starting late 2021 and will expand to affect apps running on devices that support Google Play in early 2022. In July, we will provide an alternate solution to support essential use cases such as analytics and fraud prevention.

Cannot wait to see what they provide for Fraud Prevention. LOL
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…