device id in emulators - android

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;
}

Related

Getting the Wrong Mobile Prefix in Android

I am making an app where I will get user's complete phone number from the sim - including the country and mobile prefixes. The phone number I have is 061555555, so when I save it to server it should look like this: +38761555555.
And here is my problem - when I use the code from below I get the following: +387218900032555555, ie. instead of 061 becoming 61, it becomes 218900032. This line number gives the wrong number:
MyPhoneNumber = tMgr.getLine1Number();
I have also tried this and this.
This is my code:
// Get users phone number
private String getMyPhoneNumber() {
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Get the SIM country ISO code
String simCountry = tMgr.getSimCountryIso().toUpperCase();
// Get the operator code of the active SIM (MCC + MNC)
String simOperatorCode = tMgr.getSimOperator();
// Get the name of the SIM operator
String simOperatorName = tMgr.getSimOperatorName();
// Get the SIM’s serial number
String simSerial = tMgr.getSimSerialNumber();
String MyPhoneNumber = "0000000000";
try {
MyPhoneNumber = tMgr.getLine1Number();
} catch (NullPointerException ex) {
}
if (MyPhoneNumber.equals("")) {
MyPhoneNumber = tMgr.getSubscriberId();
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber countryNumberProto = null;
try {
countryNumberProto = phoneUtil.parse(MyPhoneNumber, simCountry);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
String myPhone = phoneUtil.format(countryNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
// I tried INTERNATIONAL and NATIONAL as well
return myPhone;
}
I think you are parsing "national_number" instead of "country_code". Check your JSON parsing code fields logic
{
"country_code": 41,
"national_number": 446681800
}
https://github.com/googlei18n/libphonenumber
I checked my settings->status and found out my phone number is unknown, and instead of my phone number it displays IMEI, so I guess I will have the user confirm his phone number by entering it manually.

android unique id for each device like serial number

I have a samsung j5. I am using the Serial Number that can be found in
Settings > About Device > Status > Serial Number.
It has a value of RF8H933KWLF which I assume to be unique. Now I register this value to my database and when I run my app I send a request to my database and check if the device I use is registered in my database. The way I get the Serial Number of device is:
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 (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
For my device I can get a correct value.
My question is why when I use a different device I cant get the value that I see in the Serial Number.
I tried on device and it returns Error.
What could be an alternative ID I can use that is unique to a device (does not change even if device is restored or rooted).
I need the ID to be visible so before hand I can register it to my database to allow access to app's login.
refer this you can find many other ways Is there a unique Android device ID?
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
You can use the combination of there phone number and IMEI number to get the unique Id.
You can ask phone number to user and also device can show IMEI numbers.
To get IMEI number and phone number programmatically do something like this.
TelephonyManager teleMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI number
String imei = teleMan.getDeviceId();
//get The Phone Number
String phone = teleMan.getLine1Number();
Hope this solves it.
Note :- I haven't tried this yet.

How to get real serial number of lenovo tab in android

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.

Issue - IMEI - HTC Flyer Tab [Telephony Manager]

I have HTC Flyer tab with version Android 2.3.4. I am not able to retrieve the IMEI number through TelephonyManager.getDeviceId(). It always return null.
Can somebody try to read out the IMEI on another device. I would like to know whether it is a Google or HTC problem.
This is a 'GSM' device. And it is brand new, didn't have any OS update.
FYI,
Included Manifest:
My programs on sumsung galaxy, Motorola xoom, and all smartphones working well.
Some of the tab devices do not have IMEI number. You can get WI-FI MAC address of the device.
WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String ID = wifiInf.getMacAddress();
if you are getting null with TelephonyManager.getDeviceId(), use
Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
for example:
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
AndroidDeviceId = mTelephony.getDeviceId();
}
else{
AndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
even ANDROID_ID is not secure to use:
More specifically, Settings.Secure.ANDROID_ID. This is a 64-bit
quantity that is generated and stored when the device first boots. It
is reset when the device is wiped.
ANDROID_ID seems a good choice for a unique device identifier. There
are downsides: First, it is not 100% reliable on releases of Android
prior to 2.2 (“Froyo”). Also, there has been at least one
widely-observed bug in a popular handset from a major manufacturer,
where every instance has the same ANDROID_ID.
but i recommed use this method suggested in Android Developer´s Blog: Identifying App Installations:
public 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();
}
}
#SuppressWarnings("rawtypes")
Class SystemProperties = null;
SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
#SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
Method get=null;
get = SystemProperties.getMethod("get", paramTypes);
//Parameters
Object[] params = new Object[1];
params[0] = new String("ro.gsm.imei");
IMEI = (String) get.invoke(SystemProperties, params);

How to read serial number on a Samsung device (from within app / on the device)?

How to read the device serial number (not IMEI) of a Samsung Android phone (same value that you get when you call 'adb devices') but from within an app or from the device; not using PC/USB/adb.
I found a solution for HTC and other devices, which is - to call
getprop ro.serialno
in a terminal
as described here,
http://groups.google.com/group/android-developers/browse_thread/thread/3d57b1a214cdf928
but it doesn't work on a Samsung Galaxy S.
Edit: You may also want to check my other answer on this topic.
Not sure whether that was possible when the question was first posted. 3 years later however, this is possible:
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;
}
I conclude that it's not possible on Samsung.
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";
}

Categories

Resources