I am using below code to get the IMEI of the Android devices,
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
IMEI = tm.getDeviceId();
It is fine for the devices having a single sim active. If we apply the same code for the devices having two sim cards, then how can I get the DeviceID and tell whether I got SIM1 Id or SIM2 Id?
IMEI number should be associated with the phone and not with the sim, so also in dual sim devices you should have only one IMEI number.
"The IMEI is only used for identifying the device" [...] "Instead, the subscriber is identified by transmission of an IMSI number, which is stored on a SIM card" - ref: Wikipedia
EDIT:
Check the source code, maybe you can find some hint: Source of Settings app
Here a snippet of the "IMEI" part:
// NOTE "imei" is the "Device ID" since it represents
// the IMEI in GSM and the MEID in CDMA
if (mPhone.getPhoneName().equals("CDMA")) {
setSummaryText(KEY_MEID_NUMBER, mPhone.getMeid());
setSummaryText(KEY_MIN_NUMBER, mPhone.getCdmaMin());
if (getResources().getBoolean(R.bool.config_msid_enable)) {
findPreference(KEY_MIN_NUMBER).setTitle(R.string.status_msid_number);
}
setSummaryText(KEY_PRL_VERSION, mPhone.getCdmaPrlVersion());
removePreferenceFromScreen(KEY_IMEI_SV);
if (mPhone.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE) {
// Show ICC ID and IMEI for LTE device
setSummaryText(KEY_ICC_ID, mPhone.getIccSerialNumber());
setSummaryText(KEY_IMEI, mPhone.getImei());
} else {
// device is not GSM/UMTS, do not display GSM/UMTS features
// check Null in case no specified preference in overlay xml
removePreferenceFromScreen(KEY_IMEI);
removePreferenceFromScreen(KEY_ICC_ID);
}
} else {
setSummaryText(KEY_IMEI, mPhone.getDeviceId());
setSummaryText(KEY_IMEI_SV,
((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
.getDeviceSoftwareVersion());
// device is not CDMA, do not display CDMA features
// check Null in case no specified preference in overlay xml
removePreferenceFromScreen(KEY_PRL_VERSION);
removePreferenceFromScreen(KEY_MEID_NUMBER);
removePreferenceFromScreen(KEY_MIN_NUMBER);
removePreferenceFromScreen(KEY_ICC_ID);
// only show area info when SIM country is Brazil
if ("br".equals(mTelephonyManager.getSimCountryIso())) {
mShowLatestAreaInfo = true;
}
}
In Dual Sim Devices, they have two IMEI Numbers for each SIM Slots. both are static. First IMEI No. is for First Slot and Second No. is for Second Slot.
Related
I have a use case where I need to check whether a SIM is active in the device. In older devices, I can use TelephonyManager to get the SIM state and check whether it is SIM_STATE_READY. The issue is with API 22 and above.
Using SubscriptionManager, when I call getActiveSubscriptionInfoList, it sends me details about the SIMs present, even if I have turned them off. I went through the documentation of SubscriptionManager but couldn't find a similar method to check SIM's state. Using TelephonyManager in API above 22 gives information only about the default SIM, I would like to know this about both slots in dual SIM phones. Also, I found an overloaded variant of getSimState in TelephonyManager which does accept the slot as a parameter, but that got introduced in API 26. I would like a solution that will work in APIs 22-25 as well.
Is there a way I could identify that even though the SIM is present in the device, it isn't active?
You can check by like this if Device is dual sim
SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
int count = 0;
if (infoSim1 != null ) {
count++;
}
if (infoSim2 != null){
count++;
}
count decides that how many sims are active.
OR
You can get the count then check one by one
sManager.getActiveSubscriptionInfoCount()
Hope it's work.
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager.getPhoneCount() == 2) {
// Dual sim
}
Examples here
I'm working on an Android app and am getting null back for the IMEI number when using TelophonyManager. This is happening on several Huawei phones. (All of them are Ascend Y530s).
The phones all have sim cards and otherwise seem to be operating normally. I was under the impression that only a broken phone would return null IMEI. Clearly this is not the case..
Questions. What exactly is this IMEI number - i.e where is it stored on the device? And what does it mean when a seemingly fine phone returns its value as null?
EDIT
I should mention that the IMEI number is not always null. About half the time it seems to be valid (though this is very difficult to measure since we have 5 phones returning null IMEI numbers \ sometimes )
After your comment, to get unique device id for the survey app, i would suggest you to use Settings.Secure.ANDROID_ID as your unique id.
String myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
Or you can use both as
public String getUniqueID(){
String myAndroidDeviceId = "";
TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
myAndroidDeviceId = mTelephony.getDeviceId();
}else{
myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
return myAndroidDeviceId;
}
As announced for Lollipop MR1, SubscriptionManager and its SubscriptionInfo provide lots of information about all (active) SIMs, but I'm missing their IMEIs.
I get info about SIMs like this:
SubscriptionManager sm = SubscriptionManager.from(context);
List<SubscriptionInfo> sil = sm.getActiveSubscriptionInfoList();
if (sil != null) {
for (SubscriptionInfo subInfo : sil) {
Log.d(TAG, "SubInfo:" + subInfo);
}
} else {
Log.d(TAG, "SubInfo: list is null");
}
Am I missing something or can we still only get the IMEI (only of the 1st SIM card) via telephonyManager.getDeviceId()?
The method
public String getDeviceId(int slotId)
is available under TelephonyManager in API 23. slotId is just a number from 0 to the number of SIMs - 1.
In API 22, the same method exists but is hidden. You need to use reflection to call it.
The IMEI is used to identify the device, not the SIMs.So yes, you can only get the IMEI through telephonyManager.getDeviceId().
UPDATE: So it turns out I was wrong and a device can have an IMEI for each SIM card. I've found this answer in StackOverflow that can help you.
Android : Check whether the phone is dual SIM
In Android, I try to get the neighbor cells information. I use the following piece of code
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
List<NeighboringCellInfo> neighborCells = telephonyManager.getNeighboringCellInfo();
if (neighborCells == null) {
Log.d("cells", "Neighbor cells is null");
} else {
for (NeighboringCellInfo cell : neighborCells) {
Log.d("cells", cell.getCid()+"-"+cell.getLac()+" "+(-113+cell.getRssi()*2)+"dB");
}
}
Using logcat, I get the following output
D/cells ( 7668): Neighbor cell: -1--1 -81dB
D/cells ( 7668): Neighbor cell: -1--1 -113dB
D/cells ( 7668): Neighbor cell: -1--1 -113dB
Do you know why ? Is it related with the hardware ? With another phone, I get always "Neighbor cells is null"
Thank you
Check if youu are using a CDMA phone or a GSM phone. NeighboringCellInfo only works for a GSM phone since you don't have neighboring towers for CDMA. CDMA has a globally unique network id.
TelephonyManager mManager_;
mManager_ = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if(mManager_.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA){
//CDMA PHONE
}
else if(mManager_.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM){
//GSM PHONE
}
uses permission: android.permission.ACCESS_NETWORK_STATE
hope this helps!
Ok I found the solution, I needed to enable the option "use only 2G networks". What would be nice is the possibility to enable that option from my application. It seems it is not possible but strange because this application does it...
Does somebody knows why I have more information with 2G cells than 3G ?
I want to detect whether two SIM cards are there in my dual-SIM android phone programmatically. I found one API (TelephonyManager.getSIMState()), but it is for normal single-SIM phones. Are there any APIs to detect whether or not two SIMs are inserted in my dual-SIM phone?
Android does not support multiple SIMs, at least from the SDK. Device manufacturers who have created multi-SIM devices are doing so on their own. You are welcome to contact your device manufacturer and see if they have an SDK add-on or something that allows you to access the second SIM.
Edit: (15th July, 2015)
Since API 22, you can check for multiple SIMs using SubscriptionManager's method getActiveSubscriptionInfoList(). More details on Android Docs.
From now, if the phone is MTK powered one, you can use TelephonyManagerEx class from MediaTek SDK.
Take a look at the docs.
final SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
int simCount = activeSubscriptionInfoList.size();
btnBack.setText(simCount+" Sim available");
Log.d("MainActivity: ","simCount:" +simCount);
for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
Log.d("MainActivity: ","iccId :"+ subscriptionInfo.getIccId()+" , name : "+ subscriptionInfo.getDisplayName());
}
Well, this is not fool proof. But if you have two SIMs which are on two different network operators you can try something like this:
PhoneServiceStateListener listener = new PhoneServiceStateListener(this);
tm.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
.
.
.
class PhoneServiceStateListener extends PhoneStateListener {
Context context = null;
public PhoneServiceStateListener(Context context) {
this.context = context;
}
public PhoneServiceStateListener() {
}
#Override
public void onServiceStateChanged(ServiceState serviceState) {
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
//You get this event when your SIM is in service.
//If you get this event twice, chances are more that your phone is Dual SIM.
//Alternatively, you can toggle Flight Mode programmatically twice so
//that you'll get service state changed event.
}
super.onServiceStateChanged(serviceState);
}
}
Ideally you'll get SIM service state changed event for both the SIMs and then you can check for network operator name or something like that to check if you have two SIM cards. But you need to have two SIM cards running on two different networks.