Device identifier of Android emulator
Asked Answered
E

4

29

I want to test in the emulator an app that depends of the device identifier (ANDROID_ID).

I currently obtain device identifier with the following code:

final String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

When I run this in an emulator it returns null, which gives me all sort of problems. It seems that higher Android versions it returns something.

Is there a way to get a device identifier in the Android emulator? Am I obtaining the device id wrongly?

Maybe it's possible to set the device identifier of the emulator through the console?

Etymology answered 9/12, 2010 at 19:31 Comment(2)
Also, the device ID for the emulator will be all 0'sBisector
My Android 4.3 x86 emulator now returns an ANDROID_ID (287a676592dc7b0d).Spaak
S
39

In the emulator, the values of IMEI and IMSI are hardcoded:

2325     { "+CIMI", OPERATOR_HOME_MCCMNC "000000000", NULL },   /* request internation subscriber identification number */
2326     { "+CGSN", "000000000000000", NULL },   /* request model version */

therefore, you will always get null.

If you still want to use these id numbers for your testing and you want to keep the same code for the emulator and the real device, you must change it in the emulator somehow.

There are at least two ways how to do it:

  1. Change the values in the code and recompile the code for the emulator. However, this might be too complicated and time consuming... :-)

  2. "Hack" the emulator binary (since it is neither compressed or encrypted - you can do it!) and modify the strings (in the right place) right there.

Here's how to do it:

  • backup the emulator binary (to roll back! later). In Windows, the binary can be found under the name "emulator.exe", located in your android "\tools" folder.

  • open the binary with your favourite hex editor

  • search for the +CGSN string followed by a null byte (it should be followed by 15 digits of the IMEI number - see the printscreen below)

alt text

  • edit the number (be careful not to change the original number of the digits)

  • and save the file!

  • and maybe change/adjust your code to use the IMEI for your id (as Falmari points out), or use this trick to change some other values.

Stramonium answered 9/12, 2010 at 22:2 Comment(4)
I think that doesn't work anymore. I can't find the value "+CGSN" or "+CIMI" in the emulator.exe.Sarcoma
It still works! You just have to look in emulator-arm.exe (or emulator-x86.exe if you're using the Intel platform).Embalm
I really have no idea why this answer was accepted as the correct one. ANDROID_ID has absolutely nothing to do with IMEI / IMSI. see developer.android.com/reference/android/provider/…Greenhaw
This way to change the imei is too old. Please add the updated way to change the imei numberPsychoanalysis
P
3

If you want non-null emulator uuid, then start the emulator like this:

emulator -avd jbx86 -prop emu.uuid=5ec33f90-a471-11e2-9e96-0800200c9a66
Paloma answered 13/4, 2013 at 19:38 Comment(2)
This does nothing for me.Isidora
shouldn't it be qemu instead of emu?Beatrizbeattie
B
2

As Falmarri says, the device Id will be 0 in the emulator. I use this method to generate a unique device Id based on a combination of parameters (it seems to work for me although I haven't tested it extensively - the emulator and an HTC Desire) - it's not my method (I can't remember where I dug it up - but attribution where it's due)

/*
 * Creates a UUID specific to the device. There are possibly some instances where this does
 * not work e.g. in the emulator or if there is no SIM in the phone.
 */
public static void setDeviceUUID(Context context)
{
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = "" + Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);

    deviceMobileNo = tm.getLine1Number();

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
    deviceUUID = deviceUuid.toString();    
}

Hope this helps.

Bribe answered 9/12, 2010 at 21:20 Comment(0)
S
0

It's fine to hack the emulator binary to put in an alternate value. However, it must start with a decimal digit because in reference-ril.c, it calls at_send_command_numeric() to read the value. I believe that has to be changed to at_send_command_singleline() to support MEID strings (that are typically 14 hex digits starting with 'A'). Unless you're really clever and can find and swap the function addresses in the binary, you'll have to build from source after patching it to use the same value that some phones have.

Suitable answered 16/9, 2011 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.