Issue - IMEI - HTC Flyer Tab [Telephony Manager] - android

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

Related

device id in emulators

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

Disable/Enable mobile data on Android L with root access

I am writing a little app that only I will use and I want to pragmatically enable / disable my mobile data on a rooted android 4.5 device (I am running a custom Android L for Nexus 4).
I have looked for a while and I found the methods with reflection that worked until android 4.3.
I have also seen the method from this post Toggle mobile data programmatically on Android 4.4.2 but this requires cyanogenmod.
From what I can find on the internet this is impossible for non-root apps but my question is:
is there something I can do with my root privileges to accomplish this?
I've created this method looking around on internet; it works fine on rooted android 5.0.1
Basically you have to pass true if you want the connection to be enabled, false otherwise, and the context of your application.
private final static String COMMAND_L_ON = "svc data enable\n ";
private final static String COMMAND_L_OFF = "svc data disable\n ";
private final static String COMMAND_SU = "su";
public static void setConnection(boolean enable,Context context){
String command;
if(enable)
command = COMMAND_L_ON;
else
command = COMMAND_L_OFF;
try{
Process su = Runtime.getRuntime().exec(COMMAND_SU);
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes(command);
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
Please report if this has problems on some device.
EDIT: Now also compatible with android 5.1 Credit
Use this
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = tm.class.getDeclaredMethod( "setDataEnabled", boolean.class);
methodSet.invoke(tm, true);
Edit:
This requires permission MODIFY_PHONE_STATE, this is System or signature level permission.
Ideally you could create a runnable jar file with this code and execute it using
export CLASSPATH=<jar path>
exec app_process <jar-dir-path> your.package.name.classname "$#"
from su shell.
I noticed that the service call method does not work consistently on all devices. The number to be used in it varied from device to device.
I have found the following solution which works without any issue across all ROOTED devices.
Simply execute the following via su
To enable mobile data
svc data enable
To disable mobile data
svc data disable
It's as simple as that.
void turnData(boolean ON) throws Exception
{
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if(currentapiVersion == Build.VERSION_CODES.FROYO)
{
Log.i("version:", "Found Froyo");
try{
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) cx.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (ON) {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
}catch(Exception e){
Log.e("Error:",e.toString());
}
}
else
{
Log.i("version:", "Found Gingerbread+");
final ConnectivityManager conman = (ConnectivityManager) cx.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
}

Is there any way to get the unique device id from android [duplicate]

This question already has answers here:
Is there a unique Android device ID?
(54 answers)
Closed 8 years ago.
i want unique device id from android device. I want unique device id from android device how to get that one.
yes you can get android device mac id which is known as unique identifier assigned to network interfaces for communications on the physical network segment this is a unique id for each device which cannot be changed however.
you can get it by WIFImanager in android
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
String MACAddress = wifiInfo.getMacAddress();
dont forget to add permission to your manifest file
<uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
I don't think there's any (perfect) way to do this. See link. If you just want to identify the user, you can generate one UUID and store it in your application's storage.
have created once class and use this in my app see pastie
its create unique ID and generate MD5 message as string of unique device ID
public String getUDID(Context c) {
// Get some of the hardware information
String buildParams = Build.BOARD + Build.BRAND + Build.CPU_ABI
+ Build.DEVICE + Build.DISPLAY + Build.FINGERPRINT + Build.HOST
+ Build.ID + Build.MANUFACTURER + Build.MODEL + Build.PRODUCT
+ Build.TAGS + Build.TYPE + Build.USER;
// Requires READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager) c
.getSystemService(Context.TELEPHONY_SERVICE);
// gets the imei (GSM) or MEID/ESN (CDMA)
String imei = tm.getDeviceId();
//gets the android-assigned id you can omit this because
//It's known to be null sometimes, it's documented as "can change upon factory reset".
//Use at your own risk, and it can be easily changed on a rooted phone.
String androidId = Secure.getString(c.getContentResolver(),
Secure.ANDROID_ID);
// requires ACCESS_WIFI_STATE
WifiManager wm = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
// gets the MAC address
String mac = wm.getConnectionInfo().getMacAddress();
// concatenate the string
String fullHash = buildParams + imei + androidId + mac;
return md5(fullHash);
}
md5(String fullHash) Function
public String md5(String toConvert) {
String retVal = "";
MessageDigest algorithm;
try {
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(toConvert.getBytes());
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
retVal = hexString + "";
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}
It's really simple, actually..
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);

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.

Creating a Globally Unique Android Identifier

When talking about unique Android ID's, I'm sure everyone has seen this, however I too am trying to come up with a solution to uniquely identify any android device. I would be happy to use a publicly released class but I am yet to find one.
In my situation, the requirement is to be able to uniquely identify any devices that have some form of an internet connection (such as GPRS or Wi-Fi) and run on API level 8 (v2.2 Froyo). I'm not aware of any devices that doesn't have Wi-Fi or a SIM so let me know if there is any!
We solved this problem in iOS by using a hash of the Wi-Fi mac address as all iOS devices should have this. However for Android, techniques (such as the answers to the SO post referenced above) refer to the TelephonyManager class which can return null (such as when the device doesn't have a SIM connection, like with the Nexus 7) or getContext().getContentResolver(), Secure.ANDROID_ID which according to here isn't 100% reliable on versions prior to Froyo (which is fortunate for me). It doesn't however state of any issues post-Froyo so again, if there are any, let me know! I've also read this can return null. According to here it can change on a factory reset, but that isn't a major concern.
So I put together this to generate a hopefully-unique-GUID:
[This method is not complete!]
public static String GetDeviceId(Context context)
{
// Custom String Hash 1
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("someRandomData"); // Not really needed, but means the stringBuilders value won't ever be null
// TM Device String
final TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String tmDeviceId = tm.getDeviceId(); // Could well be set to null!
LogMsg.Tmp("TM Device String [" + tmDeviceId + "]");
// Custom String Hash 2
stringBuilder.append(tmDeviceId);
int customHash = stringBuilder.toString().hashCode();
LogMsg.Tmp("Custom String hash [" + customHash + "]");
// Device ID String
String androidIDString = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
LogMsg.Tmp("Device ID String [" + androidIDString + "]");
// Combined hashes as GUID
UUID deviceUuid = new UUID(androidIDString.hashCode(), ((long)customHash << 32));
LogMsg.Tmp("Combined hashes as GUID [" + deviceUuid.toString() + "]");
return deviceUuid.toString();
}
So you may find tmDeviceId being set to null, in this case the the customHash will be the same regardless of the device but then this combined with androidIDString should be globally unique. I think. Obviously I will need to work out if tmDeviceId AND androidIDString aren't available and throw an exception there.
So... is this overkill? If so, is it safe to just use context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID?
Why don't you get MAC address of the device as you've done in iOS? This can be performed in Android devices as well.
I'll give a code snippet that obtains mac address of the device..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HardwareUtil {
public static String getMacAddress()
{
try{
Process process = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0){
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
String hwaddr = output.toString();
return hwaddr;
}catch (IOException e) {
e.printstacktrace();
}catch (InterruptedException e){
e.printstacktrace();
}
}
}
HardwareUtil.getMacAddress() will return mac address of the device.
EDIT: If mac address is not appropriate for your situation. Following can be useful!
public static String getDeviceId(Context context) {
final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
if (deviceId != null) {
return deviceId;
} else {
return android.os.Build.SERIAL;
}
}
Don't forget to add following permission to your AndoridManifest.xml file if you use getDeviceId method.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Categories

Resources