Reputation: 2445
I am trying to get the device serial number programmatically. I have used the following line:
Build.SERIAL
Which returns something like :
95b9efad04ad28
However which I go to the settings on the device, I can see that it displays a different string:
Could anyone point me in the correct direction?
Upvotes: 8
Views: 14489
Reputation: 1976
I know this question is several years old now, but I wanted to post an updated version, especially for those who may be using Xamarin. This is a solution for C# Xamarin Android, based on the solution posted above by user flawyte.
Here is the code:
public static string GetDeviceSerialCode ( )
{
string serial_number = string.Empty;
try
{
var c = Java.Lang.Class.ForName("android.os.SystemProperties");
var get = c.GetMethod("get", Java.Lang.Class.FromType(typeof(Java.Lang.String)));
serial_number = (string)get.Invoke(c, "gsm.sn1");
if (string.IsNullOrEmpty(serial_number))
{
serial_number = (string)get.Invoke(c, "ril.serialnumber");
}
if (string.IsNullOrEmpty(serial_number))
{
serial_number = (string)get.Invoke(c, "ro.serialno");
}
if (string.IsNullOrEmpty(serial_number))
{
serial_number = (string)get.Invoke(c, "sys.serialnumber");
}
if (string.IsNullOrEmpty(serial_number))
{
serial_number = Build.GetSerial();
}
}
catch (Exception e)
{
serial_number = string.Empty;
}
return serial_number;
}
Upvotes: 1
Reputation: 9367
There are several ways to get that number depending on the device's manufacturer and Android version :
public static String getSerialNumber() {
String serialNumber;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serialNumber = (String) get.invoke(c, "gsm.sn1");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ril.serialnumber");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ro.serialno");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "sys.serialnumber");
if (serialNumber.equals(""))
serialNumber = Build.SERIAL;
// If none of the methods above worked
if (serialNumber.equals(""))
serialNumber = null;
} catch (Exception e) {
e.printStackTrace();
serialNumber = null;
}
return serialNumber;
}
Taken from this gist.
Upvotes: 8
Reputation: 2445
This worked for me:
String serialNumber;
try {
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");
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 4
Reputation: 378
Since the Android ID is broken on many devices and Android builds alike I am using a mix of the Android ID and the devices WIFI-MAC address, which is unique per device. Both are used to create a UUID, which is always unique, no matter if the Android ID is null or not.
Try something along the lines of this:
final WifiManager wm = (WifiManager) myAct.getSystemService(Context.WIFI_SERVICE);
String macAddr = null;
if (wm != null)
macAddr = wm.getConnectionInfo().getMacAddress();
String androidId = Secure.getString(myAct.getContentResolver(), Secure.ANDROID_ID);
if (androidId == null)
androidId = "0000000000000000";
if (macAddr == null || macAddr.contains("\\s+") || !macAddr.contains(":"))
macAddr = "00:00:00:00:00:00";
UUID deviceUuid = new UUID(macAddr.hashCode(), androidId.hashCode());
String deviceId = deviceUuid.toString();
Upvotes: 0
Reputation: 75629
I am trying to get the device serial number programmatically
This feature is broken on Android for ages. Some devices return null
, some return the same ID for all the devices etc. If your app is using Google Play Services, use InstanceId instead, or see this, but ancient now, but still valid, blog post.
Upvotes: 0
Reputation: 2532
this is not your answer?:
TelephonyManager tManager = (TelephonyManager)myActivity.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
I found it in here
Upvotes: 0