Device ID...

Everest

Member
Licensed User
Longtime User
Hello,

This is my first post and first Basic4Android application.

To start the larger project I had to verify I could get a unquie ID for each device and started searching the web.

Lots of hits, but I ran across this "Android Unique Device ID" Android Unique Device ID « PocketMagic

and it seemed to be the most complete example.

Then to convert it. Here is the result. Thought someone might be able to use it.

The conversion starts at GetIDAsString. The other bits are for me or testing.


B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

 'must have the ABWiFi, ByteConvertor and Encryption library

 Dim BlueTooth As Serial

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)

Dim idString,formattedString As String
 Dim i As Int

 Activity.LoadLayout("Main")

 idString = GetIDAsString      
 formattedString = ""

'break it into groups of four
 For i = 1 To 32 
  formattedString = formattedString & idString.CharAt(i - 1)
  If ((i Mod 4) = 0) Then
   formattedString = formattedString &  CRLF
  End If
 Next
 
 Label1.Text = formattedString
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GetIDAsString As String
 
 Dim m_szImei,m_szDevIDShort,m_szAndroidID,m_szWLANMAC,m_szBTMAC,m_szLongID,m_szUniqueID As String
 Dim tempString As String
 
 Dim i,aByte As Int
 Dim b As Boolean
 Dim p_md5Data() As Byte
 
 Dim PhoneId As PhoneId
 Dim Ph1 As Phone
 Dim r As Reflector
 Dim myWifi As ABWifi
 Dim md As MessageDigest
 Dim ByteCon As ByteConverter
  
 m_szImei = PhoneId.GetDeviceId         'part 1
 If (m_szImei.Length < 1) Then
  m_szImei = "987654321"
 End If
 
 'part 2
 m_szDevIDShort = "35"
 tempString = r.GetStaticField("android.os.Build","BOARD")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","BRAND")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10) 
 tempString = r.GetStaticField("android.os.Build","CPU_ABI")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","DEVICE")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","DISPLAY")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","HOST")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","ID")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
tempString = r.GetStaticField("android.os.Build","MANUFACTURER")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","MODEL")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","PRODUCT")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","TAGS")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","TYPE")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 tempString = r.GetStaticField("android.os.Build","USER")
 m_szDevIDShort = m_szDevIDShort & (tempString.Length Mod 10)
 
 'part 3
 m_szAndroidID = Ph1.GetSettings("android_id")
 
 'part 4
 m_szWLANMAC = "01:23:45:67:89:01" 
 If (myWifi <> Null) Then
  b = myWifi.ABLoadWifi
  If b Then
   m_szWLANMAC = myWifi.ABGetCurrentWifiInfo.MacAddress
  End If
 End If
 

'part 5
 BlueTooth.Initialize("dummySerialSub")
 m_szBTMAC = BlueTooth.Address
 
'collection complete 
 
 m_szLongID = m_szImei & m_szDevIDShort & m_szAndroidID & m_szWLANMAC & m_szBTMAC

'compute md5
 p_md5Data = md.GetMessageDigest(m_szLongID.GetBytes("UTF8"),"MD5")
 For i = 0 To p_md5Data.Length - 1
  aByte = Bit.And(0xFF, p_md5Data(i))
'if it is a single digit, make sure it has a 0 in front (proper padding)
  If (aByte <= 0xF) Then
   m_szUniqueID = m_szUniqueID & "0"
  End If
  m_szUniqueID = m_szUniqueID & Bit.ToHexString(aByte)      'add number to string
 Next

 Return m_szUniqueID.ToUpperCase
End Sub

Sub dummySerialSub
End Sub

Comments/suggestions welcome.

Cheers,

Mark
 
Top