0

Does anybody know how to get the serial number of a device?

I know I can do this in a NativeScript-5 application (TypeScript):

import { Page } from "tns-core-modules/ui/page";    
import * as platformModule from "tns-core-modules/platform";

export function onNavigatingTo(args: EventData) {
    let page = <Page>args.object;

    console.log("Manufacturer:" + platformModule.device.manufacturer);
    console.log("Model:" + platformModule.device.model);
    console.log("UUID:" + platformModule.device.uuid);
}

But I couldnt find any property for the device's serial number. Anybody an idea? It's alright if your solution only covers Android (since my project is targeting Android tablets only).

Thanks!

Update:

Manoj pointed me to some Java code that probably solves my problem. However, I wasn't able to marshal the following code to TypeScript.

      public static String getManufacturerSerialNumber() { 
          String serial = null; 
          try {
               Class<?> c = Class.forName("android.os.SystemProperties");
               Method get = c.getMethod("get", String.class, String.class);
               serial = (String) get.invoke(c, "ril.serialnumber", "unknown");
          } catch (Exception ignored) {}
          return serial;
      }

Maybe anybody could help me? That would be awesome!

Ingmar
  • 1,525
  • 6
  • 34
  • 51

2 Answers2

4

iOS never allows any confidential information to be accessed by apps, that applies to serial number too. With Android, you may read the serial number with

android.os.Build.SERIAL

I had it tested with Playground

Edit 1:

Manufactures like Samsung seems to have a different serial number which is not same as android.os.Build.SERIAL. Here is another SO thread that seems very relevant for Samsung, the sample code is written in Java.

Edit 2:

Here is how you may get serial number on Samsung devices

        let serialNumber = ''; 
        try {
            const cl = application.android.context.getClassLoader();
            const SystemProperties = cl.loadClass('android.os.SystemProperties');

            const paramTypes = (<any>Array).create(java.lang.Class, 2);
            paramTypes[0] = java.lang.String.class;
            paramTypes[1] = java.lang.String.class;
            const getMethod = SystemProperties.getMethod('get', paramTypes);

            const params = (<any>Array).create(java.lang.Object, 2);
            params[0] = new java.lang.String('ril.serialnumber');
            params[1] = new java.lang.String('unknown');

            serialNumber = getMethod.invoke(SystemProperties, params);
        } catch (err) {
            console.error(err);
        }

Updated Playground

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • Hello Manoj, amazing how many IDs a device has. On my Samsung Galaxy Tablet your code returns "330050c2712865fb" which is actually the ID that NativeScript's CLI displays when I do "tns run". Except, this still is not the serial number I am looking for. On my tablet, when I go into "Settings > About tablet" I can see a "Serial number: R52K90L6TZH". This is also the number which is printed on the box and a sticker on the device. It's also the number that Samsung Knox uses for MDM. Weird, so many numbers ... – Ingmar Dec 21 '18 at 18:58
  • Samsung / MI brands do customise the Android for their device. As per the official android documentation, this is the API we should use and it works on my Moto G5+ which is a true (untouched) Android from Google. I will still take a look if I find anything on Samsung. – Manoj Dec 21 '18 at 19:01
  • Thank you so much, Manoj. I also keep on researching ... if you find a solution for Samsung that's good enough for my purposes. – Ingmar Dec 21 '18 at 19:05
  • I have updated the answer with a link to other SO thread that has multiple solutions for Samsnug, except it's written in Java. I hope you may able to marshall that into JS / TS. – Manoj Dec 21 '18 at 19:08
  • Not sure. Will have a look at this on the weekend ;) Thanks for now, Manoj. – Ingmar Dec 21 '18 at 19:09
  • Let me know in case if you find it hard. I will have to find my old Samsung Tablet if I have to test this. – Manoj Dec 21 '18 at 19:12
  • Damn. Wasn't able to solve this one. Tried to access "android.os.SystemProperties" by following https://docs.nativescript.org/core-concepts/android-runtime/metadata/accessing-packages#access-android-classes, but ended up ... nowhere. – Ingmar Dec 22 '18 at 16:31
0

You can check properties 'ro.serialno', 'ril.serialnumber', 'gsm.sn1', 'sys.serialnumber'

Android: How to programmatically access the device serial number shown in the AVD manager (API Version 8)

In my case Samsung TAB S2 has 2 serials. Property 'ro.serialno' more accurate then 'ril.serialnumber'. So i check it first.

getSerial() {
  if (isAndroid) {
    let serialNumber = null;
    try {
      const cl = application.android.context.getClassLoader();
      const SystemProperties = cl.loadClass('android.os.SystemProperties');

      const paramTypes = (<any>Array).create(java.lang.Class, 2);
      paramTypes[0] = java.lang.String.class;
      paramTypes[1] = java.lang.String.class;
      const getMethod = SystemProperties.getMethod('get', paramTypes);

      const params = (<any>Array).create(java.lang.Object, 2);

      // Reorder if needed
      const props = ['ro.serialno', 'ril.serialnumber', 'gsm.sn1', 'sys.serialnumber'];

      for(let i = 0, len = props.length; i < len; i++) {

        params[0] = new java.lang.String(props[i]);
        serialNumber = getMethod.invoke(SystemProperties, params);

        if (serialNumber !== '') {
          return serialNumber;
        }
      }

    } catch (err) {
      console.error(err);
    }

    return serialNumber;
  }
}

Full code: https://play.nativescript.org/?template=play-tsc&id=ViNo6g&v=6

Roman Rhrn Nesterov
  • 3,538
  • 1
  • 28
  • 16