34

Is there some adb or android shell command that I could run that would return a device's IMEI or MEID number? Preferably that's all that would be returned.

n8schloss
  • 2,723
  • 2
  • 19
  • 27
  • There are a few answers here that run `adb shell ...` and pipe the output into `awk` and then into multiple `sed` and `tr` commands and then maybe into `awk` again. That isn't necessary, `awk` can do everything `sed` and `tr` can do. e.g. `awk -F"'" 'NR>1 { gsub(/\./,"",$2); imei=imei $2 } END {print imei}'` – cas Apr 03 '18 at 00:29

8 Answers8

52

I figured out how to do this. You need to run adb shell dumpsys iphonesubinfo in a shell. It will give you a bit more than you need, but it will also return IMEI or MEID number.

Edit (2017): In Android 5.0+ you'll need to use the service call command. More info about that can be found here.

Community
  • 1
  • 1
n8schloss
  • 2,723
  • 2
  • 19
  • 27
12

For IMEI you can use:

adb shell service call iphonesubinfo 1 | awk -F "'" '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • 1
    this one, and 'cas' comment to mitesh' answer, are the only ones that worked for me (Galaxy s8+, Android 8.0, adb version = Android Debug Bridge version 1.0.40, Version 4797878 – ttulinsky Aug 23 '18 at 01:10
10

The following ADB command works for me to get the IMEI:

adb shell "service call iphonesubinfo 1 | cut -c 52-66 | tr -d '.[:space:]'"

Devlpr
  • 165
  • 2
  • 5
4

For ESN you can do

service call iphonesubinfo 16

at least it gives me the right one on Motorola Photon Q.

To clean it up (assuming you have shell on the device and have a capable busybox there, if not I highly recommend one):

    service call iphonesubinfo 16 | busybox awk -F "'" '{print $2}' | busybox sed 's/[^0-9A-F]*//g' | busybox tr -d '\n' && echo

For MEID with cleanup:

service call iphonesubinfo 1 | busybox awk -F "'" '{print $2}' | busybox sed 's/[^0-9A-F]*//g' | busybox tr -d '\n' && echo
Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20
3

This works for me on my nexus 5 and moto 5G.

output:

[build.id]: [M4B30X]
[build.version.release]: [6.0.1]
[build.version.sdk]: [23]
[build.version.security_patch]: [2016-10-05]
[product.brand]: [google]
[product.manufacturer]: [LGE]
[product.model]: [Nexus 5]
[product.name]: [hammerhead]
[serialno]: [05xxxxxxxxxxx4]
[device.imei]: [xxxxxxxxxxxx]
[device.phonenumber]: [+xxxxxxxxxx]

Script: get.deviceinfo.bash

#!/bin/bash
# Get the device properties
adb shell getprop | grep "model\|version.sdk\|manufacturer\|ro.serialno\|product.name\|brand\|version.release\|build.id\|security_patch" | sed 's/ro\.//g'
# get the device ime
echo "[device.imei]: [$(adb shell service call iphonesubinfo 1 | awk -F "'" '{print $2}' | sed '1 d'| tr -d '\n' | tr -d '.' | tr -d ' ')]"
# get the device phone number
echo "[device.phonenumber]: [$(adb shell service call iphonesubinfo 19 | awk -F "'" '{print $2}' | sed '1 d'| tr -d '\n' | tr -d '.' | tr -d ' ')]"

It requires:

angelous
  • 781
  • 7
  • 5
1

For the IMEI, maybe this command is easier to understand

db -s <device id> shell  service call iphonesubinfo 1 | cut -c 52-66 | tr -d '.[:space:]'
  • db -s <device id> shell service call iphonesubinfo 1 gets the full result, for example
Result: Parcel(
      0x00000000: 00000000 0000000f 00350033 00340037 '........3.5.7.4.'
      0x00000010: 00350032 00370030 00310032 00390039 '2.5.0.7.2.1.9.9.'
      0x00000020: 00370034 00000032                   '4.7.2...        ')
  • cut -c 52-66 trims away all columns except 52-66, using the above example
........3.5.7.4
2.5.0.7.2.1.9.9
4.7.2...       
  • and tr -d '.[:space:]' trims off any '.' and white space, using the example above
357425072199472

Warning A downside of this approach is that it is brittle, in the sense that the output must always be in the same format, having the exact same columns. I verified it on CentOs and OS X, but an update to the adb version could break this command simply by adjusting the whitespace in the output.

Micha Pringle
  • 123
  • 2
  • 8
0

As the iphonesubinfo 1 command does not work on many devices, here is a little workaround that should work consistently on most Android versions and on rooted and unrooted devices:

If you already have an own app that you can install on the device that you want to know the IMEI from, add this BroadcastReceiver to your app:

public class GetImeiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
        setResultData(imei);
    }

}

and to the AndroidManifest.xml:

<receiver android:name=".GetImeiReceiver">
  <intent-filter>
    <action android:name="com.myapp.GET_IMEI"/>
  </intent-filter>
</receiver>

Call your receiver over ADB:

adb shell am broadcast -a com.myapp.GET_IMEI

...and the output will be something like:

Broadcast completed: result=0, data="000000000000000"

...where data is the device IMEI.

If you have don't have an existing app to integrate this solution into, I created this simple one which includes the required code: https://github.com/saschoar/android-imei-getter (also includes the APK and full instructions).

saschoar
  • 8,150
  • 5
  • 43
  • 46
  • where and how to write this command adb shell am broadcast -a com.myapp.GET_IMEI . Downloaded your code but does not work – AbdulSaleem Apr 27 '17 at 15:35
  • I need to find imei for real device, just java code does not works for me – AbdulSaleem Apr 27 '17 at 15:42
  • This is an irresponsible implementation, as it allows any app on the device to query for the IMEI or MEID. You should add `android:permission="android.permission.READ_PRIVILEGED_PHONE_STATE"` to the receiver in order to close this security hole, since that permission is required by `getDeviceId`, and the android shell will already have been granted this permission by default. – BLuFeNiX Mar 06 '20 at 21:06
  • Note: Older API levels only require `READ_PHONE_STATE` instead of `READ_PRIVILEGED_PHONE_STATE` in order to call `getDeviceId`, but `READ_PRIVILEGED_PHONE_STATE` is safer and should work as far back as Android 4.0. Using only `READ_PHONE_STATE` will leave a security hole open for Android 10 and later. – BLuFeNiX Mar 06 '20 at 21:17
0

IMEI- adb shell service call iphonesubinfo 1 | awk -F "'" '{print $2}' | sed '1 d'| sed 's/.//g' | awk '{print}' ORS=''

Android ID=

adb shell settings get secure android_id

mitesh
  • 39
  • 7
  • Nice... this is a small edit for running on Mac OSX, the backslash before the "." prevents matching anything [outputting nothing] and instead matches the actual "."'s, and the END will add a linefeed to make the output stand out alone: adb shell service call iphonesubinfo 1 | awk -F "'" '{print $2}' | sed '1 d' | sed 's/\.//g' | awk '{print} END {printf("\n");}' ORS='' – user107172 Apr 27 '17 at 22:33
  • 3
    good idea but that awk|sed|sed|awk pipeline is horrible - awk can do it all. Try `adb shell service call iphonesubinfo 1 | awk -F"'" 'NR>1 { gsub(/\./,"",$2); imei=imei $2 } END {print imei}'` – cas Apr 03 '18 at 00:17