tags:

views:

3005

answers:

3

Hey all,

I'm developing a little program which syncs some of the users data from my app on the cloud (just a load of strings, but that's not the point).

To help identify each device uniquely I would like to use the IMEI (or ESN number for CDMA devices) ...so here is the question, does anyone know how to access this programmatically?

Thanks, Tom.

+9  A: 

You want to call android.telephony.TelephonyManager.getDeviceId().

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).

You'll need the READ_PHONE_STATE permission to do this.

That being said, be careful about doing this. Not only will users wonder why your application is accessing their telephony stack, it might be difficult to migrate data over if the user gets a new device.

Trevor Johns
What would be a better solution then which would enable them to migrate data in the future? Google email address? If so how can I pull that out from the system?
Tom
The most reliable way to do this is to just have them create an account the first time they launch your app.There are ways to get the user's email address (see the docs on AccountManager), but verifying that a malicious user hasn't forged this information requires a bit of knowledge about how the Google Data APIs work -- more than I can put in this small comment box. ;)
Trevor Johns
Actually, for that matter, you don't have any guarantee that the user hasn't forged their IMEI/MEID either. If security is of any concern, you really need to use either a username/password, or the getAuthToken() method in AccountManager (and again, you'd need to verify this token with the server that originally issued it).
Trevor Johns
A: 

In addition to the answer of Trevor Johns, you can use this as follows:

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

And you should add the following permission into your Manifest.xml file:

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

In emulator, you'll probably get a like a "00000..." value. getDeviceId() returns NULL if device ID is not available.

+1  A: 

Or you can use the ANDROID_ID setting from Android.Provider.Settings.System (as described here strazerre.com).

This has the advantage that it doesn't require special permissions but can change if another application has write access and changes it (which is apparently unusual but not impossible).

Just for reference here is the code from the blog:

import Android.Provider.Settings.System;   

String androidID = System.getString(this.getContentResolver(),System.ANDROID_ID);
tonys