Pages

Saturday, May 4, 2013

How to get Device Unique ID in Android


Hello everyone! Here posting to get unique Device-ID in android. In android require the Unique ID in some special case when developing application.

Ways to get device id in Android
  • Unique number (IMEI, MEID, ESN, IMSI)
  • MAC Address
  • ANDROID_ID

Unique number (IMEI, MEID, ESN, IMSI)

TelephonyManager.getDeviceId() is required to return (depending on the network technology) the IMEI, MEID, ESN and IMSI of the phone, which is unique to that piece of hardware.


The IMEI, MEID, ESN, IMSI can be defined as follows:
  • IMEI( International Mobile Equipment Identity ) - The unique number to identify GSM, WCDMA mobile phones as well as some satellite phones
  • MEID(Mobile Equipment IDentifier)  - The globally unique number identifying a physical piece of CDMA mobile station equipment, the MEID was created to replace ESNs(Electronic Serial Number)
  • ESN(Electronic Serial Number)  - The unique number to identify CDMA mobile phones
  • IMSI(International Mobile Subscriber Identity)  - The unique identification associated with all GSM and UMTS network mobile phone users
To get the Device ID, you would include the following code in your project:


import android.content.Context;
import android.telephony.TelephonyManager;

String imeistring=null;                        
String imsistring=null;

    TelephonyManager telephonyManager;                                            
    telephonyManager  = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    /*
    * getDeviceId() function Returns the unique device ID.
    * for example,the IMEI for GSM and the MEID or ESN for CDMA phones. 
    */                                                               
    imeistring = telephonyManager.getDeviceId();

    /*
    * getSubscriberId() function Returns the unique subscriber ID,
    * for example, the IMSI for a GSM phone.
    */
    imsistring = telephonyManager.getSubscriberId();    


To allow read only access to phone state add permission READ_PHONE_STATE in the AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 


MAC Address


It may be possible to retrieve a Mac address from a device’s Wi-Fi or Bluetooth hardware. But, it is not recommended using this as a unique identifier.


Device should have Wi-Fi (where not all devices have Wi-Fi)
If Wi-Fi present in Device should be turned on otherwise does not report the MAC address

You would include the following code in your project for get MAC Address:


import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

 String macAddress=null;
    WifiManager wifiManager = ( WifiManager ) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    String macAddress = wInfo.getMacAddress();
    if (macAddress != null)
        macAddress = macAddress;

To allow only access to wifi state add permission ACCESS_WIFI_STATE in the AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

ANDROID_ID

In ANDROID_ID. A 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device (The value may change if a factory reset is performed on the device.) ANDROID_ID seems a good choice for a unique device identifier.

To retrieve the ANDROID_ID for using Device ID, please refer to example code below


import android.provider.Settings;

              String androidId = Settings.Secure.getString(getContentResolver(), 
                                         Settings.Secure.ANDROID_ID);

Not 100% reliable of Android prior to 2.2 (“Froyo”) devices
Also, there has been at least one widely-observed bug in a popular handset from a major manufacturer, where every instance has the same ANDROID_ID.


Sample Example

Create the project "com.uniqueid" with the activity "UniqueDeviceID ".

Class :-  UniqueDeviceID.java


  import android.app.Activity;
  import android.content.Context;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.telephony.TelephonyManager;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
 
  public class UniqueDeviceID extends Activity implements OnClickListener {

       Button bt;
       TextView txt_View;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);

              bt=(Button)findViewById(R.id.button1);
              txt_View =(TextView)findViewById(R.id.textView1);       
              bt.setOnClickListener(this);     
  }

  @Override
  public void onClick(View v) {
       // TODO Auto-generated method stub
       String imeistring=null;                        
       String imsistring=null;

       TelephonyManager   telephonyManager  = 
(TelephonyManager)getSystemService( Context.TELEPHONY_SERVICE );

       /*
        * getDeviceId() function Returns the unique device ID.
        * for example,the IMEI for GSM and the MEID or ESN for CDMA phones. 
        */                           
       imeistring = telephonyManager.getDeviceId();
       txt_View.append("IMEI No : "+imeistring+"\n");


       /*
        * getSubscriberId() function Returns the unique subscriber ID,
        * for example, the IMSI for a GSM phone.
        */                                                                                                             
       imsistring = telephonyManager.getSubscriberId();                                      
       txt_View.append("IMSI No : "+imsistring+"\n");

/*
        * returns the MacAddress             
        */

       WifiManager wifiManager =
(WifiManager) getSystemService(Context.WIFI_SERVICE);
       WifiInfo wInfo = wifiManager.getConnectionInfo();
       String macAddress = wInfo.getMacAddress();
       if (macAddress == null)
              txt_View.append( "MAC Address : " + macAddress + "\n" );
       else
              txt_View.append( "MAC Address : " + macAddress + "\n" );
       }


       /*
        * Settings.Secure.ANDROID_ID returns the unique DeviceID
        * Works for Android 2.2 and above                         
        */
       String androidId = Settings.Secure.getString(getContentResolver(), 
                     Settings.Secure.ANDROID_ID);                          
       txt_View.append( "AndroidID : " + androidId + "\n" );

      
  }


Change the layout "main.xml" to the following.


  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:padding="10dp" >

      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" />

      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="GetDeviceID" >
      </Button>

      <TextView
          android:id="@+id/textView1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" >
      </TextView>

  </LinearLayout>

Add the permission "READ_PHONE_STATE" and  to "AndroidManifest.xml" to allow your application to access  WIFI and PHONE state.


  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.uniqueid"
      android:versionCode="1"
      android:versionName="1.0" >

      <uses-sdk
          android:minSdkVersion="7" />

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

      <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          <activity
              android:name="com.uniqueid.UniqueDeviceID"
              android:label="@string/app_name" >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />

                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      </application>

  </manifest>



Output :- 
Below image shows the output of above sample example.

More Reference Like here


Download Sample code ClickHere