How can I get both IMEI numbers from dual SIM mobile? - android

How can I get both IMEI numbers from dual SIM mobile? Can anyone help me to resolve this problem.

Any information regarding SIM #2 (or any other then default SIM) is purely manufacturer dependent. Android does not provide APIs for multi-SIM facility. Android apis only support default SIM Card slot. You can contact Micromax (device manufacturer) if he can provide you apis to support his hardware component.

You can try the following code it will help you.
TelephonyManager manager= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> telephonyClass = Class.forName(manager.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getFirstMethod = telephonyClass.getMethod("getDeviceId", parameter);
Log.d("SimData", getFirstMethod.toString());
Object[] obParameter = new Object[1];
obParameter[0] = 0;
String first = (String) getFirstMethod.invoke(manager, obParameter);
Log.d("IMEI ", "first :" + first);
obParameter[0] = 1;
String second = (String) getFirstMethod.invoke(manager, obParameter);
Log.d("IMEI ", "Second :" + second);
} catch (Exception e) {
e.printStackTrace();
}
And add the permission on menifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

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.

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

getLine1Number() Returns blank, not null [duplicate]

This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
I want to get Mobile Number of Device. I have used following code reference by Alex Volovoy's This Link
TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Log.d("msg", "Phone : "+mPhoneNumber);
OUTPUT in Logcat:
Without Simcard Phones Returns:
02-01 17:22:45.472: D/msg(29102): Phone : null
With Simcard:
02-01 17:22:45.472: D/msg(29102): Phone :
I have also taken permission in <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in AndroidManifest.xml
So what should i do? Is there any Mistake?
To get the phone number from the device , first you have to set your own phone number on the device, just through :
Settings -> About Phone -> Status -> My phone Number
Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:
To avoid this problem , we can catch the error and work accordingly. Like:
TelephonyManager tMgr = (TelephonyManager)
ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
String MyPhoneNumber = "0000000000";
try
{
MyPhoneNumber =tMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}
if(MyPhoneNumber.equals("")){
MyPhoneNumber = tMgr.getSubscriberId();
}
I have another solution to get a phone number when telephony manager returns blank. I hope it'll help you.
Here is my sample code:
public static final String main_data[] = {
"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"
};
object object = (TelephonyManager) context.getSystemService("phone");
if (!TextUtils.isEmpty(((TelephonyManager) (object)).getLine1Number())) {
}
object = context.getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"), main_data, "mimetype=?", new String[]{
"vnd.android.cursor.item/phone_v2"
}, "is_primary DESC");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext()) {
break;
}
if (s.equals("vnd.android.cursor.item/phone_v2")) {
String s1 = ((Cursor) (object)).getString(4);
boolean flag1;
if (((Cursor) (object)).getInt(5) > 0) {
flag1 = true;
} else {
flag1 = false;
}
Toast.makeText(SampleActivity.this, "Phone:-" + s1, Toast.LENGTH_LONG).show();
}
} while (true);
((Cursor) (object)).close();
}
Also don't forget to add these two permissions:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />

Retrieve cell towers information

Does anybody know how to retrieve cell tower list on GSM and CDMA on Android.
I have been trying to use Google Maps Locations API:
https://developers.google.com/maps/documentation/business/geolocation/
And I want to get cell towers information with these fields:
cellId: Unique identifier of the cell. On GSM, this is the Cell ID (CID); CDMA networks use the Base Station ID (BID).
locationAreaCode: The Location Area Code (LAC) for GSM networks; CDMA networks use Network ID (NID).
mobileCountryCode: The cell tower's Mobile Country Code (MCC).
mobileNetworkCode: The cell tower's Mobile Network Code. This is the MNC for GSM, or the System ID (SID) for CDMA.
age: The number of milliseconds since this cell was primary. If age is 0, the cellId represents a current measurement.
signalStrength: Radio signal strength measured in dBm.
timingAdvance: The timing advance value.
This code doesn't especially getting cell towers information.
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Type of the network
int phoneTypeInt = tel.getPhoneType();
String phoneType = null;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;
try {
if (phoneType != null) {
params.put("radioType", phoneType);
}
} catch (Exception e) {}
/*
* The below code doesn't work I think.
*/
JSONArray cellList = new JSONArray();
List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
for (int i = 0; i < neighCells.size(); i++) {
try {
JSONObject cellObj = new JSONObject();
NeighboringCellInfo thisCell = neighCells.get(i);
cellObj.put("cellId", thisCell.getCid());
cellList.put(cellObj);
} catch (Exception e) {}
}
if (cellList.length() > 0) {
try {
params.put("cellTowers", cellList);
} catch (JSONException e) {}
}
And I set permissions like this:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Please help me, thank you.
I had this problem much more recently and it ended up being the fact that
getNeighboringCellInfo
is deprecated from API 23 up. To get around this use something like the following (it's quite annoying, really):
public static JSONArray getCellInfo(Context ctx){
TelephonyManager tel = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
JSONArray cellList = new JSONArray();
// Type of the network
int phoneTypeInt = tel.getPhoneType();
String phoneType = null;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;
//from Android M up must use getAllCellInfo
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
for (int i = 0; i < neighCells.size(); i++) {
try {
JSONObject cellObj = new JSONObject();
NeighboringCellInfo thisCell = neighCells.get(i);
cellObj.put("cellId", thisCell.getCid());
cellObj.put("lac", thisCell.getLac());
cellObj.put("rssi", thisCell.getRssi());
cellList.put(cellObj);
} catch (Exception e) {
}
}
} else {
List<CellInfo> infos = tel.getAllCellInfo();
for (int i = 0; i<infos.size(); ++i) {
try {
JSONObject cellObj = new JSONObject();
CellInfo info = infos.get(i);
if (info instanceof CellInfoGsm){
CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
cellObj.put("cellId", identityGsm.getCid());
cellObj.put("lac", identityGsm.getLac());
cellObj.put("dbm", gsm.getDbm());
cellList.put(cellObj);
} else if (info instanceof CellInfoLte) {
CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
cellObj.put("cellId", identityLte.getCi());
cellObj.put("tac", identityLte.getTac());
cellObj.put("dbm", lte.getDbm());
cellList.put(cellObj);
}
} catch (Exception ex) {
}
}
}
return cellList;
}
Your phone might not support this function.
See this: http://code.google.com/p/android/issues/detail?id=24136
and this: Which Android phone models support getNeighboringCellInfo()?
It's not your phone, which doesn't support this info, it is purely crappy implementation of the RIL related C and Java used to provide those API calls, which prevent this. If you manage to go into the Service Mode menus (which vary widely from phone to phone) you will have full and instant access to RSSI and loads of other types of signals. You should blame Google (or your OEM) for not fixing those problems and provide better access to RF related variables.

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

Categories

Resources