Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List Windows Mobile Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android Device Unique ID - Alternative to PhoneId

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2012, 09:25 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,792
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Device Unique ID - Alternative to PhoneId

This tutorial is based on the following blog post: Identifying App Installations | Android Developers Blog

The standard way to get a unique id is using PhoneId.GetDeviceId. However this method has several disadvantages:
- It requires the "android.permission.READ_PHONE_STATE" permission which is quite a sensitive permission.
- It doesn't work on devices without phone functionality.
- Apparently there is a bug in some devices which return the same id.

Starting from Android 2.3 there is a new field that returns a unique id and should work on all devices (without requiring any permission).

A general solution is to use this field for modern devices and to use a "fake" id for older devices. The fake id is randomly created when the application first runs.

Here is the code (requires the Reflection library):
Code:
Sub Activity_Create(FirstTime As Boolean)
    
Log(GetDeviceId)
End Sub

Sub GetDeviceId As String
    
Dim r As Reflector
    
Dim Api As Int
    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(0x100000000x7FFFFFFF)
            
File.WriteString(File.DirInternal, "__id", id)
            
Return id
        
End If
    
Else
        
'New device
        Return r.GetStaticField("android.os.Build""SERIAL")
    
End If
End Sub
Reply With Quote
  #2 (permalink)  
Old 02-07-2012, 08:07 PM
COBRASoft's Avatar
Knows the basics
 
Join Date: Dec 2011
Location: Oudenburg, Belgium
Posts: 259
Send a message via MSN to COBRASoft
Default

Erel: this returns 'unknown' on my SE Xperia Arc S. Any idea?
Reply With Quote
  #3 (permalink)  
Old 02-08-2012, 05:47 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,792
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Not really. Apparently your device doesn't implement this API properly.
Reply With Quote
  #4 (permalink)  
Old 02-20-2012, 05:36 PM
nad nad is offline
Knows the basics
 
Join Date: Jun 2011
Posts: 136
Default

Very interesting read. Thanks erel.

Last edited by nad : 02-20-2012 at 05:39 PM.
Reply With Quote
  #5 (permalink)  
Old 04-05-2012, 04:20 PM
timo's Avatar
Senior Member
 
Join Date: Oct 2011
Location: Italy
Posts: 353
Default

With Api < 9 the problem is that when a user reinstall the app a new random number is generated. If you have a trial app running x times, it is so easy bypassed.
Attached Images
File Type: jpg reinstall.jpg (27.6 KB, 203 views)
__________________
www.massimotestoni.it

Last edited by timo : 04-05-2012 at 04:25 PM.
Reply With Quote
  #6 (permalink)  
Old 04-11-2012, 10:20 PM
nad nad is offline
Knows the basics
 
Join Date: Jun 2011
Posts: 136
Default

One of my apps was being cracked and is not anymore. I added a delay of one minute between license checks to avoid that so after a failure any try to check license under a minute fails with ExitApplication and some other trcks. Maybe that helped (or they just didn't care enough to crack posterior updates)

Sent from my GT-I9100 using Tapatalk 2
Reply With Quote
  #7 (permalink)  
Old 04-21-2012, 11:06 AM
Junior Member
 
Join Date: Apr 2012
Posts: 10
Default

On my huawei mediapad7 return unknow

android 3.2
Reply With Quote
  #8 (permalink)  
Old 07-09-2012, 03:16 PM
Newbie
 
Join Date: Jun 2012
Posts: 6
Default

My Phone is Lenovo P700,android 4.0.3,return '123456789ABCDEF',My android Pad version 4.0.3 return 'unknown'.

Do not seem right
Reply With Quote
  #9 (permalink)  
Old 07-09-2012, 05:41 PM
Junior Member
 
Join Date: Dec 2011
Posts: 81
Default

Erel that Blog post recommends using android_id and not the serial number. Perhaps a mixture of all 4 would be best?

If a Phone use the Phone ID. If no phone id use the Serial Number and the android_Id; as last resort try and get the mac address and if that fails generate a UID.
Reply With Quote
  #10 (permalink)  
Old 07-10-2012, 06:03 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,792
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Seems like they updated this blog post. You can fetch the android_id value with Phone.GetSettings("android_id")
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Super-fast alternative to emulator -> x86 Android Jim Brown Basic4android Getting started & Tutorials 55 04-20-2013 03:56 AM
PhoneId returns wrong phone number Cdymock Basic4android Updates and Questions 2 06-25-2012 08:17 AM
Get PhoneId.GetDeviceId from server sigster Basic4android Updates and Questions 2 02-04-2012 06:08 PM
What makes a App Unique LittleEddie Basic4android Updates and Questions 2 03-13-2011 10:37 AM
Problem with phoneID, after compile device.exe schimanski Questions (Windows Mobile) 19 03-28-2010 02:42 PM


All times are GMT. The time now is 04:18 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0