Android Question Getting AndroidID

Peekay

Active Member
Licensed User
Longtime User
I need to read the AndroidID of the phone.
Is there a direct way other than with a java object, or how do I use the java object to read that.

Thanks
PK
 

udg

Expert
Licensed User
Longtime User
It's some time now that Google encourages the use of the advertising ID instead.

See here.

Anyway, you may try code like this for Android ID or libraries like my old dgUID to see if it works with your device and limited to your specific goal.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
My Huawei device sports Android 4.4, so the old methods should still work :)
Seriously, I can't say about newer Huawei devices. I read about an ID for models making use of the HMS (the equivalent of Google Play Services) but I've no hands-on experience so can't really say.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
And for Huawei devices?
I guess this also applies to huawei.

The changes are Android 10 related. We are not able to get ANY fixed Hardware-ID any longer. Instead we are suggested to use the Advertising ID.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Get DeviceID:
Sub GetDeviceId As String
    Dim r As Reflector
    Dim Api As Int
    Dim DeviceID As String
    'Dim DeviceSerialNumber As String
    Dim pi As PhoneId
    
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    If Api < 9 Then
        'Old device
        If File.Exists(File.DirInternal, "__id") Then
            Return File.ReadString(File.DirInternal, "__id")
        Else
            Dim id As Int
            id = Rnd(0x10000000, 0x7FFFFFFF)
            File.WriteString(File.DirInternal, "__id", id)
            Return id
        End If
    Else
        'New device
        
        DeviceID=r.GetStaticField("android.os.Build", "SERIAL")
        
        If DeviceID.ToLowerCase.Contains("unknown") Then
'           
            DeviceID=pi.GetDeviceId
            
        End If
        
        Return DeviceID
        
    End If
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Alex,

Your code apparently can also get the Phone serial number.
I think that would suit me better and I would probably not need permission to read that.
How would I be able to read that?

Thanks
PK
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Alex,

Your code apparently can also get the Phone serial number.
I think that would suit me better and I would probably not need permission to read that.
How would I be able to read that?

Thanks
PK
I'm using android.permission.READ_PHONE_STATE permission.

Try
Phone serial number:
Dim pi As PhoneId
log(pi.GetSimSerialNumber)
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Thanks Alex,
I had thought the serial number is that of the phone and not of the simcard. Sorry.
I need to have a positive indentification which cannot be fiddled with. If it is the AndoidID and simcard serial number, then both may be changed by the user without me knowing it.
I presume the READ_PHONE_STATE will allow me to read the IMEI number?

PK
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Oliver,
I have seen the thread by Erel, but thought it talked about the AndroidID.
PK
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Newer versions of Android do not return IMEI/serial number information due to user privacy. You’ll have to come up with another scheme to uniquely identify a device.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Newer versions of Android do not return IMEI/serial number information due to user privacy. You’ll have to come up with another scheme to uniquely identify a device.
We tested on versions 9 and 10 - works fine. I can return IMEI number with my code.
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
The IMEI number seems to be unreadable, I imagine that due to Google's security policies.
Personally several applications that I had made I had to change them, here I leave the code

B4X:
Sub GenerateID() As String
    Dim P As Phone
    Dim IDAndroid As String=P.GetSettings("android_id")
    Return IDAndroid
End Sub

This code returns a hexadecimal number, for example: adf75a6be80e47
I hope there is a better way to identify an Android phone and that Google will not make any more changes in the future.
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Bladimir, the name of the Sub 'Generate' confuses me somewhat.
Does this mean that every time that sub executes, it will generate a different AndroidID?

What I do is the following:
1. Every time the app is opened, it reads the AndroidID.
2. The first time it is opened, I code the AndroidID with an algorithm that produces a unique registration code.
3. The user then enters this code and the app saves it in the key value store.
4. Every time the app is opened, I check whether the app is registered by getting the AndroidID, decoding it, and checking it with the saved registration code.
5. I do not worry too much about factory resets, but some other apps on the phone may reset the AndroidID, and then I must register the app again.

Thanks,
PK
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Bladimir, the name of the Sub 'Generate' confuses me somewhat.
Does this mean that every time that sub executes, it will generate a different AndroidID?

What I do is the following:
1. Every time the app is opened, it reads the AndroidID.
2. The first time it is opened, I code the AndroidID with an algorithm that produces a unique registration code.
3. The user then enters this code and the app saves it in the key value store.
4. Every time the app is opened, I check whether the app is registered by getting the AndroidID, decoding it, and checking it with the saved registration code.
5. I do not worry too much about factory resets, but some other apps on the phone may reset the AndroidID, and then I must register the app again.

Thanks,
PK


The AndroidID is assumed to be the same hexadecimal number.
If a factory reset, this AndroidID must change by another hexadecimal number
I would like to know the algorithm that you use to encode or decode.
How do you know if the smartphone changed or someone wants to copy your APK?
I appreciate you can share some of the code you use.
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
I appreciate you can share some of the code you use

Here is my coding of the AndroidID, somewhat changed for security reasons:
Coding:
Sub AndroidIDtoCode
    Dim Scramble As String = ""
    Dim ScramVal(16) As Int
    AndroidID = Kvs.Get("androidId")
    For i = 0 To AndroidID.Length - 1
        Scramble = Scramble & AndroidID.CharAt(i)
        ScramVal(i) = NativeMe.RunMethod("covertToASCII", Array(AndroidID.CharAt(i)))
    Next
    
    For i = 0 To 16
        CalcRegNo = CalcRegNo + ScramVal(i)
    Next
    
End Sub

How do you know if the smartphone changed or someone wants to copy your APK?

The App will not show the menu if the decoded AndroidID does not correspond with the registered code. So, anybody can copy the App, but will not be able to use it.
For extra security, I save the users ID number in the key value store and he has to enter it every time the app opens as a sort of password. There are also other GPS related checks in it.

PK
 
Upvote 0

annitb

Member
Licensed User
Longtime User
there was some concept of a blob store or the like...i believe it was something that can be written to by an app and even root cannot read/write to it...root can delete it, i think ( not to fret about this too much, root can do anything, at least there's a guarantee that it's what the app created it as instead of possibly all fake ids that root can present to apps )...so an app should theoretically be able to try and read one's own stored id ( let's say this guid was created the first time the app was run on the device ), if it doesn't find it, then it should create one and then try to read it, and then use that forever...the beauty of this blob store ( i really forget the exact name of the mechanism ) is that it can reside in the app directory ( i think ) ...the app invokes the api call to add to the store...and it's there for ever...
 
Upvote 0

Similar Threads

Top