And for Huawei devices?It's some time now that google encourages the use of the advertising ID instead.
I guess this also applies to huawei.And for Huawei devices?
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
I'm using android.permission.READ_PHONE_STATE permission.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
Dim pi As PhoneId
log(pi.GetSimSerialNumber)
I presume the READ_PHONE_STATE will allow me to read the IMEI number?
It will not work on new versions of Android. You should either generate a random value when the app starts for the first time or use the advertising id: https://www.b4x.com/android/forum/threads/advertising-id.101050/#content
We tested on versions 9 and 10 - works fine. I can return IMEI number with my code.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.
I'm surprised. It returns an empty string on my Android 10 Xaiomi Mi Max 3, which is what I would expect.We tested on versions 9 and 10 - works fine
Sub GenerateID() As String
Dim P As Phone
Dim IDAndroid As String=P.GetSettings("android_id")
Return IDAndroid
End Sub
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
I appreciate you can share some of the code you use
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?