18

I need to get the hardware serial code of Android device where my app is installed. This hardware serial number is the one that you can see on Settings > About Device > Status > Serial number.

I though I would get it using Settings.Secure.ANDROID_ID or android.os.Build.SERIAL, but neither of them worked, meaning that they didn't give me unique identifier I'm looking for. For instance android.os.Build.SERIAL got me the unique ID shown on ADB when you run the command adb devices.

Notice that the goal of this question is not to find another unique identifier that could help me, it's only about getting the device hardware serial number.

Thanks for the help.

EDIT:

Please be clear that I'm not looking for the value provided by the String android.os.BUILD.SERIAL. I know how to get that value but I'm not interested in it.

I have a Samsung device, therefore, the serial number they used is different than the one provided by android.os.BUILD.SERIAL.

Storo
  • 988
  • 1
  • 7
  • 31
  • Post your code, and also explain why you think it is not working – arlistan Nov 05 '14 at 18:15
  • @FedeBucich It's that it's not working it's that I get other unique identifiers that are not the one I'm looking for. I need to get the one I can check on settings, not the one I get when I connect the device to the PC using ADB, or other unique IDs. – Storo Nov 05 '14 at 18:17
  • @Funkystein I'm sorry but it has nothing to do with it, since on that post they are interested in using android.os.Build.Serial, which is not the unique ID I need. – Storo Nov 05 '14 at 18:21
  • 1
    http://stackoverflow.com/questions/2322234/how-to-find-serial-number-of-android-device – joao2fast4u Nov 05 '14 at 18:28
  • the settings app is open source, you probably can go into that code to figure out what is used. – njzk2 Nov 05 '14 at 18:29
  • @joao2fast4u Thanks for the link but that gives you the IMEI ID and thats not what I'm looking for. Thanks though. – Storo Nov 05 '14 at 18:33
  • @Storo : should be in there : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.4_r1/com/android/settings/deviceinfo/Status.java?av=f – njzk2 Nov 05 '14 at 19:25
  • (which apparently uses `Build.SERIAL`) – njzk2 Nov 05 '14 at 19:25
  • @njzk2 You're right, it does, but I have a Samsung device and the value provided is different. – Storo Nov 05 '14 at 19:39
  • that's an interesting information – njzk2 Nov 05 '14 at 19:43
  • possible duplicate of [Serial number from Samsung Device running Android](http://stackoverflow.com/questions/14161282/serial-number-from-samsung-device-running-android) – njzk2 Nov 05 '14 at 19:43
  • @njzk2 Could post the same first answer so I can mark it as the correct one? – Storo Nov 05 '14 at 19:50
  • the proper way of handling this situation is to close the question as duplicate. I don't yet have the power of closing questions on my own, though, so this will require 3 more closing votes. – njzk2 Nov 05 '14 at 19:53
  • How do I vote to close it? – Storo Nov 05 '14 at 20:05
  • @Storo If your problem is solved, you can delete the question, if it is a duplicate. – joao2fast4u Nov 05 '14 at 22:52
  • @joao2fast4u Good suggestion, but it should be closed and not deleted, since it will make the search easier for other people. – Storo Nov 06 '14 at 03:39

1 Answers1

17

The solution is very simple:

Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
serialNumber = (String) get.invoke(c, "sys.serialnumber", "Error");
if(serialNumber.equals("Error")) {
    serialNumber = (String) get.invoke(c, "ril.serialnumber", "Error");
}

This will get you the serial number of most samsung devices. I use the "sys.serialnumber" value to get the serial number of my SM-T210 (Galaxy Tab 3) and SM-T230 (Galaxy Tab 4) tablets, but probably works with a lot more Samsung tablets. The "ril.serialnumber" is to get the value on my Samsung GT-I8550L (Galaxy Win).

I hope this helps.

avenet
  • 2,894
  • 1
  • 19
  • 26
Storo
  • 988
  • 1
  • 7
  • 31
  • 2
    Galaxy Tab 2 (GT-P3113) oddly uses `ril.serialnumber` ([so question](http://stackoverflow.com/questions/14161282/serial-number-from-samsung-device-running-android) and [gist](https://gist.github.com/jgold6/f46b1c049a1ee94fdb52)). – dev Apr 17 '15 at 13:23
  • 1
    It didn't work for me. But this did: http://stackoverflow.com/questions/11029294/android-how-to-programmatically-access-the-device-serial-number-shown-in-the-av – unludo Oct 03 '16 at 13:10