I am trying to get the serial number of my Lenovo Idea Tab running Android. I tried all the options available in Google search, which are:
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
String serial = (String) get.invoke(c, "ril.serialnumber" );
String serial_no = (String) get.invoke(c, "ro.serialno");
String serial_no1 = (String) get.invoke(c, "sys.serialnumber");
ro.serialno is giving a value as 8TYDE67HYLUOZPOZ, but the serial number shown in "Status -> About Tablet" is HGC2TKH4, and the printed serial number in the back side of the tab is also HGC2TKH4.
ril.serialnumber and sys.serialnumber are empty.
In some other tabs also, the ril.serialnumber is empty, but it is supposed to have the serial number, I hope.
Please, how can I find the real serial number of my device on Android?
I recently had to also get the SN for a lenovo tablet.
You can use the following methods
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager != null ? (telephonyManager.getDeviceId() != null ? telephonyManager.getDeviceId() : "N/A") : "N/A";
}
public static String getSN() {
String deviceSN = "N/A";
try {
Class<?> propClass = Class.forName("android.os.SystemProperties");
Method getProp = propClass.getMethod("get", String.class, String.class);
deviceSN = (String) getProp.invoke(propClass, "gsm.sn1", "N/A");
} catch (Exception ignored) {
}
return deviceSN;
}
public static String getPN() {
String devicePN = "N/A";
try {
Class<?> propClass = Class.forName("android.os.SystemProperties");
Method getProp = propClass.getMethod("get", String.class, String.class);
devicePN = (String) getProp.invoke(propClass, "ro.lenovosn2", "N/A");
} catch (Exception ignored) {
}
return devicePN;
}
Lenovo has custom property names, you can use the following adb command to see all available props:
adb shell getprop
I used this code to get the serial number from the Lenovo tab 7 Essential (2017 model running Android 7.0). I was also using it to get Samsung serial numbers (Galaxy Camera 1/2 running Android 4 and a Tab A running 6, then updated to 7.1.1).
private static String UNKNOWN = "unknown";
static String GetDeviceSerialNumber() {
String serial;
if (Build.MANUFACTURER.toLowerCase().contains("lenovo")) {
serial = GetLenovoManufacturerSerialNumber();
} else {
serial = GetSamsungManufacturerSerialNumber();
}
if (serial == null || serial.equals(UNKNOWN)) {
serial = Build.SERIAL; // this is NOT usually the serial displayed on the device, but it returns something
}
return serial;
}
// http://stackoverflow.com/questions/14161282/serial-number-from-samsung-device-running-android
private static String GetSamsungManufacturerSerialNumber() {
String serial = GetSystemPropertyString("ril.serialnumber"); // older Samsung devices - works on Galaxy Camera 1
if (serial == null || serial.equals(UNKNOWN)) {
serial = GetSystemPropertyString("sys.serialnumber"); // newer Samsung devices
}
return serial;
}
// https://stackoverflow.com/questions/23773975/how-to-get-real-serial-number-of-lenovo-tab-in-android
private static String GetLenovoManufacturerSerialNumber() {
return GetSystemPropertyString("ro.lenovosn2");
}
private static String GetSystemPropertyString(String prop) {
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
return (String)get.invoke(c, prop, UNKNOWN);
} catch (Exception ignored) {
ignored.printStackTrace();
return null;
}
}
Hopefully that's helpful to someone.
HI you can get everything about your device hardware by using the following code.
Build.BRAND //Brand Name
Build.DEVICE //Device Name
Build.MODEL //Model No
Build.SERIAL //Serial No
import this namespace :: import android.os.Build;
Let me know if my answer is right or not.
Related
Hello I try to get my Bluetooth device id but that's not working.
i try below code so can you check which mistake is i made in that.
public static String getBluetoothMacAddress(Context context) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "0";
try {
Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object btManagerService = mServiceField.get(bluetoothAdapter);
if (btManagerService != null) {
bluetoothMacAddress = (String) btManagerService.getClass()
.getMethod("getAddress").invoke(btManagerService);
Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
}
bluetoothMacAddress= android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
} catch (Exception e) {
Utils.printLog("bluetoothMacAddress1 ", e.getMessage());
}
if (bluetoothMacAddress == null || bluetoothMacAddress.equals("") || bluetoothMacAddress.equals("02:00:00:00:00:00")) {
bluetoothMacAddress = "0";
}
Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
return bluetoothMacAddress;
}
You mac address is a sort of id actually but on network layer, so that network can identify your device. You should always get same mac address for particular device, though there are some exceptions in manufacturing when multiple device could have same mac (like Arduino controllers). However, this is EXCEPTIONAL, rather than general
Can I verify whether a device is a real android device or an emulator from only the information of the device ID? I read that you get NULL if you try to get the value in an emulator although I tried it and I get different values each time.
String android_id = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
you can check the System Properties usertype
in most emulators it will return "userdebug" and on normal devices will return "user"
NOTE : on some rooted devices it may return "userdebug" too
public static String getUserType() {
String uType = "-";
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
uType = (String) get.invoke(c, "ro.build.type", "-");
} catch (Exception ignored) {}
return uType;
}
Pre Marshmallow my app would obtain it's device MAC address via BluetoothAdapter.getDefaultAdapter().getAddress().
Now with Marshmallow Android is returning 02:00:00:00:00:00.
I saw some link(sorry not sure where now) that said you need to add the additional permission
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>
to be able to get it. However it isn't working for me.
Is there some additional permission needed to get the mac address?
I am not sure it is pertinent here but the manifest also includes
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
So is there a way to get the local bluetooth mac address?
zmarties is right but you can still get the mac address via reflection or Settings.Secure:
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
Access to the mac address has been deliberately removed:
To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs.
(from Android 6.0 Changes)
You can access Mac address from the file
"/sys/class/net/" + networkInterfaceName+ "/address" ,where networkInterfaceName can be wlan0 or eth1.But Its permission may be read-protected,so it may not work in some devices.
I am also attaching the code part which i got from SO.
public static String getWifiMacAddress() {
try {
String interfaceName = "wlan0";
List<NetworkInterface> interfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (!intf.getName().equalsIgnoreCase(interfaceName)) {
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null) {
return "";
}
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) {
buf.append(String.format("%02X:", aMac));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
} catch (Exception exp) {
exp.printStackTrace();
}
return "";
}
First the following permissions have to be added to Manifest;
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
Then,
public static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS = "bluetooth_address";
String macAddress = Settings.Secure.getString(getContentResolver(), SECURE_SETTINGS_BLUETOOTH_ADDRESS);
After that the application has to be signed with OEM / System key. Tested and verified on Android 8.1.0.
Please use the below code to get the bluetooth mac address. let me know if any issues.
private String getBluetoothMacAddress() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
try {
Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object btManagerService = mServiceField.get(bluetoothAdapter);
if (btManagerService != null) {
bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
}
} catch (NoSuchFieldException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
} else {
bluetoothMacAddress = bluetoothAdapter.getAddress();
}
return bluetoothMacAddress;
}
Getting the MAC address via reflection can look like this:
private static String getBtAddressViaReflection() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
if (bluetoothManagerService == null) {
Log.w(TAG, "couldn't find bluetoothManagerService");
return null;
}
Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
if (address != null && address instanceof String) {
Log.w(TAG, "using reflection to get the BT MAC address: " + address);
return (String) address;
} else {
return null;
}
}
using a reflection library (net.vidageek:mirror) but you'll get the idea.
Since below method return null for android O.
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
I found new way to get Bluetooth Mac address, you can try by using below command line.
su strings /data/misc/bluedroid/bt_config.conf | grep Address
NOTE: In my case, i was working with root device so my app has super user permission.
As it turns out, I ended up not getting the MAC address from Android. The bluetooth device ended up providing the Android device MAC address, which was stored and then used when needed. Yeah it seems a little funky but on the project I was on, the bluetooth device software was also being developed and this turned out to be the best way to deal with the situation.
Worked great
private String getBluetoothMacAddress() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "";
try {
Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object btManagerService = mServiceField.get(bluetoothAdapter);
if (btManagerService != null) {
bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
}
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
}
return bluetoothMacAddress;
}
How can I find out for sure that device really has gsm, cdma or other cellular network equipment (not just WiFi)?
I don't want to check current connected network state, because device can be offline in the moment.
And I don't want to check device id via ((TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId() because some devices would just give you polymorphic or dummy device ID.
Actualy, I need to check cell equipment exactly for skipping TelephonyManager.getDeviceId and performing Settings.Secure.ANDROID_ID check on those devices that don't have cellular radio. I have at least one tablet (Storage Options Scroll Excel 7") which returns different IMEIs every time you ask it, although it should return null as it has no cell radio (the same situation here: Android: getDeviceId() returns an IMEI, adb shell dumpsys iphonesubinfo returns Device ID=NULL). But I need to have reliable device id that is the same every time I ask.
I'd be glad to hear your thoughts!
If you're publishing in the store, and you want to limit your application only being visible to actual phones, you could add a <uses-feature> into your manifest that asks for android.hardware.telephony. Check out if that works for you from the documentation.
Just in case somebody needs complete solution for this:
Reflection is used because some things may not exist on some firmware versions.
MainContext - main activity context.
static public int getSDKVersion()
{
Class<?> build_versionClass = null;
try
{
build_versionClass = android.os.Build.VERSION.class;
}
catch (Exception e)
{
}
int retval = -1;
try
{
retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
}
catch (Exception e)
{
}
if (retval == -1)
retval = 3; //default 1.5
return retval;
}
static public boolean hasTelephony()
{
TelephonyManager tm = (TelephonyManager) Hub.MainContext.getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null)
return false;
//devices below are phones only
if (Utils.getSDKVersion() < 5)
return true;
PackageManager pm = MainContext.getPackageManager();
if (pm == null)
return false;
boolean retval = false;
try
{
Class<?> [] parameters = new Class[1];
parameters[0] = String.class;
Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
Object [] parm = new Object[1];
parm[0] = "android.hardware.telephony";
Object retValue = method.invoke(pm, parm);
if (retValue instanceof Boolean)
retval = ((Boolean) retValue).booleanValue();
else
retval = false;
}
catch (Exception e)
{
retval = false;
}
return retval;
}
i'm newbie
I'm using samsung tab P3100 (ICS 4.04).on menus setting->about device->status, i can got the serial number of device.How to get this serial number value programmatically ?
please help me...
android.os.Build.Serial <> Serial Number
Note :
Serial number <> IMEI <> android_id <> mac address
public class Utility {
Context context;
public AdParameters(Context context) {
this.context = context;
metrics = context.getResources().getDisplayMetrics();
}
public String getDeviceName() {
return android.os.Build.MODEL;
}
public String getIMEI() {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
public String getMobielNo() {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(context.TELEPHONY_SERVICE);
return tm.getLine1Number();
}
}
Android: How to programmatically access the device serial number shown in the AVD manager (API Version 8). Have a look at this link.
You can use the getprop command on the adb shell and check yourself that which of the files contains the correct Serial number.Many times the serial number is located on different files and code has to be device specific.
Foe samung Tab 3 you can use the following code:
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
serialnum = (String) (get.invoke(c, "sys.serialnumber", "unknown"));
} catch (Exception ignored) {
serialnum = "unknown";
}