42

I am looking for the easiest way to get a unique android device identifier from both the Android adb and the Android ADK itself.

For example, when i use the adb 'devices' command, the serial number of my connected device is outputted to the screen. I have yet to identify a method in the Android sdk to get me the same serial number.

I don't care what unique identifier is used, just something that can be easily retrieved from both the adb and android sdk. Rooting a device will not be an option.

2

12 Answers 12

73

You would probably want to use ANDROID_ID.

This is how you can query its value via adb shell:

settings get secure android_id

Or by querying the settings content provider:

content query --uri content://settings/secure/android_id --projection value
3
  • Content doesn't seem to be a found by the shell?
    – i_am_jorf
    Jan 15, 2013 at 19:17
  • Just escape the ['] chars in the end of the query to run it smoothly. Aug 4, 2013 at 11:51
  • 9
    or as a single oneliner: adb shell 'settings get secure android_id'
    – ccpizza
    Jun 12, 2016 at 1:47
16

Simple as that with adb

adb devices -l
13

Android stores all the device related info in one or the other sqlite DB. These databases can be found in /data/data/ folder. To read these databases from adb you will need root permissions.

The android id is available in /data/data/com.android.providers.settings/databases/settings.db

So for adb use this command.

adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db "select value from secure where name = 'android_id'"

**you must be running adb as root in adb

1
  • 2
    Author has mentioned that rooting a device is not an option Jul 24, 2015 at 20:22
7

The Android serial number can be retreived within the device with getprop :

getprop ro.serialno

From Java, you may want to execute it with Runtime.exec()

2
  • 3
    serial number is not the same as android id.
    – ccpizza
    Jun 12, 2016 at 1:48
  • 1
    @ccpizza exact, but OP wanted any device id
    – McX
    Jun 13, 2016 at 13:53
6

Try ANDROID_ID. As documentation says "[it] is randomly generated on first boot and should remain constant for the lifetime of the device".

3
  • 1
    Do have any idea of how this can be accessed from adb?
    – user163757
    Mar 30, 2011 at 14:19
  • As far as I know most reliable way will be to create an application that copies it (ANDORID_ID) to database. Than you can access this database trough adb (use sqlite3 command from shell). Mar 30, 2011 at 14:57
  • I haven't tested it myself but you may also find ANDORID_ID already stored in this places: - /data/data/com.android.providers.settings/databases/settings.db - table secure - /data/data/com.google.android.googleapps/databases/gls.db - table meta Mar 30, 2011 at 15:05
6

I use adb. First I list the attached device ID numbers with the following command:

adb devices

And the output will look something like this:

List of devices attached
YOGAA1BBB412    device

From the previous output, you will need the device ID number (eg: YOGAA1BBB412). To get the Device ID, I use the following adb command:

adb -s YOGAA1BBB412 shell getprop | grep product.model

And the output looks like this:

[ro.product.model]: [Lenovo YT3-X90F]
2
  • You'd think that adb devices would show that by default. Thanks for this. Jan 12, 2018 at 18:01
  • Ah, it does. You just need to use the -l flag for long output. Jan 12, 2018 at 18:03
4

Please make sure your device is connected by running the below command.

adb devices -l

after that to get the android id of that device you can run the below command.

adb shell content query --uri content://settings/secure --where "name=\'android_id\'"
1

adb shell settings get secure android_id

This command returns ANDROID_ID but it may not be the same as ANDROID_ID received at code level. The application can use different min API level and the value obtained from adb shell may not match

It was possible to get the ANDROID_ID from [net.hostname] prop. It worked in API level 23.

Command to get the android_id for API level 23

adb shell getprop net.hostname

In android 8.0 and above this prop returns null due to android privacy policy changes. Hence, ANDROID_ID doesn't remain to be a reliable method of unique device id comparison.

0

Try this and find the id besides Android. The command gives basically all info about the phone.

adb -s <YOUR-DEVICE-ID> shell getprop
0

Use getprop net.hostname which has an Android unique Id followed by keyword android-<64 bit Hexadecimal String>

example:

[net.hostname]: [android-a487e0560d669e4a]
-1

To find everything about connected usb device on ubuntu use below command lsusb -v
See the link enter image description here it will give u device id like Bus 001 Device 005: ID 18d1:4ee7 Google Inc. So use first four "18d1" to add android usb rules

-3

you should probably use IMEI:

    TelephonyManager m = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    String imei = m != null ? m.getDeviceId() : null;
1
  • IMEI may not be a good proxy for a unique identifier since you can't guarantee that every device your app runs on will have a cell radio. Given that you can specify a <uses-feature> for android.hardware.telephony, I wouldn't recommend assuming the presence of telephony hardware. On the other hand, if you're happy restricting your app to only those devices with telephony hardware, then this should be useable.
    – RivieraKid
    Mar 30, 2011 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.