0

I've got this working, I think, but the data coming from the getSerial() request is not accurate.

The result does not match anything in my "about" section of my devices.

I am in need of this information to help my end users when they call into our helpdesk - they need to identify their device by serial number

is there a way to translate the getSerial() to the actual serial numbers?

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

String serialNumber;
serialNumber = android.os.Build.getSerial();

anyone know how to get the actual information?

Ed Kuhner
  • 369
  • 1
  • 2
  • 11
  • Although I have limited experience with hardware identifiers, for the last versions of Android is getting harder and harder to get them and I expect that Google will keep this trend. Maybe you can check the following doc to see if something might be useful: https://developer.android.com/training/articles/user-data-ids – Iulian Popescu Oct 01 '19 at 08:02

2 Answers2

1
TelephonyManager tManager = (TelephonyManager)myActivity.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();

getSystemService is a method from the Activity class. getDeviceID() will return the MDN or MEID of the device depending on which radio the phone uses (GSM or CDMA).

Each device MUST return a unique value here (assuming it's a phone). This should work for any Android device with a sim slot or CDMA radio. You're on your own with that Android powered microwave ;-)

Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22
  • awesome! any idea about WiFi only devices like Tablets? They have a Serial number - but nothing matches what the getSerial() is returning... I was hoping for the actual device serial number – Ed Kuhner Sep 30 '19 at 19:36
  • @EdKuhner https://stackoverflow.com/a/52606366/8663492 check this one . And if it works please accept my answer. – Muntasir Aonik Oct 01 '19 at 08:53
  • Muntasir - any chance you could walk me through what the heck this code is doing? I'm not really following how this builds with the Class.forname and then the c.getmethod - what's going on here? – Ed Kuhner Oct 02 '19 at 01:22
  • Just take the method . He shared his own class which you don't need to follow . @EdKuhner – Muntasir Aonik Oct 02 '19 at 11:19
0

Turns out getting unique hardware identifiers is getting harder and harder, so it's best to plan the application around not having access to these kinds of pieces of information, and instead to create your own.

I read a LOT of articles and tested a lot of code samples, and ultimately stumbled across this gem and find it to be very useful:

Create a Class called Installation:

public static class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}

Anywhere in your application that you need that unique ID - just call this to get it - or create it the first time:

    //INSTALLATION ID
    String installID =  Installation.id(this);
    Log.w("INSTALLATION_ID", installID);

Super easy And extensible if you ever need to or want to save anything else unique to your apps installation to the same place.

Ed Kuhner
  • 369
  • 1
  • 2
  • 11