2

Starting SDK 22, Android officially support Dual-SIM and provide some documentation.

I would like to know if it is possible for an app to change wich SIM should access the network. For example I have a One Plus One 3T phone running Nougat and I use two SIM cards. Going into settings, I can manually change / select which SIM card to use to access the network.

Is it possible to achieve this ?

Thanks !

Mackovich
  • 3,319
  • 6
  • 35
  • 73
  • Not sure, but seems they added only possibility to check which SIM active and if it in roaming and so on. There is even explanation for what it useful `This information is useful for developers who want to throttle their apps' data access down or off for device users who are sensitive to data access charges`. [Link](https://developer.android.com/about/versions/android-5.1.html) – Divers Mar 01 '17 at 10:23
  • Yes I read that. Its very slim. How does application such as this one (https://play.google.com/store/apps/details?id=pt.joaormf.mtkcontrol.free&hl=en&rdid=pt.joaormf.mtkcontrol.free) work ? – Mackovich Mar 01 '17 at 10:33
  • Probably they just use API of phone manufacture? Because `Requires Android 4.0 and up`. – Divers Mar 01 '17 at 10:35
  • What about pure Android ? As in Google would release a Nexus (or Pixel) with dual sim slots. It must mean there is some internal API, right ? If it is the case, I can definitely do some reflections. Furthermore, dual sim Android phones is not a recent thing. Sure, "back in the days", each manufacturer would've developed their own solution same as for Bluetooth LE (HTC, Samsung, Motorola own SDks) before Google standardized it in 4.3. So today, it ought to be standardized as well if there is some basic support for dual SIM? Like knowing which one is roaming: there's definitely something here... – Mackovich Mar 01 '17 at 10:41
  • When they will standardize it - they will publish info about that. Till that moment - I didn't see that information. – Divers Mar 01 '17 at 10:45

1 Answers1

1

The solution I found for Android 7.1.1 API 25 is using SubscriptionManager and reflection api.

Note that you have to install your app in /system/priv-app/

You can do this through adb root.

Make sure you have in your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>

The code is the following

The only thing this code does is exchange the sim data

private Integer subActual = null;
private Integer sim1 = null;
private Integer sim2 = null;
private SubscriptionInfo simInfo1;
private SubscriptionInfo simInfo2;
private Boolean dualSim;

SubscriptionManager subscriptionManager = SubscriptionManager.from(this);

    @SuppressLint("MissingPermission")
    List smList = subscriptionManager.getActiveSubscriptionInfoList();

    Method[] smMethods = subscriptionManager.getClass().getMethods();

    dualSim = smList.size() == 2;

    if (dualSim) {
        simInfo1 = (SubscriptionInfo) smList.get(0);
        simInfo2 = (SubscriptionInfo) smList.get(1);
        sim1 = simInfo1.getSubscriptionId();
        sim2 = simInfo2.getSubscriptionId();
    }

    for (Method m : smMethods) {
        if (m.getName().equals(("getDefaultDataSubscriptionId"))) {
            try {
                subActual = (int) m.invoke(subscriptionManager);
            } catch (Exception e) {

            }
        }
    }

    for (Method m : smMethods) {
        if (m.getName().equals("setDefaultDataSubId") && dualSim) {
            try {
                if (subActual == sim1) {
                    m.invoke(subscriptionManager, sim2);
                }
                if (subActual == sim2) {
                    m.invoke(subscriptionManager, sim1);
                }
            } catch (Exception e) {

            }
        }
    }
Julio
  • 11
  • 1