Call using sim index in android, API level 31, android 12

123 Views Asked by At

I have a scenerio where I want to call someone without human interation, I am sending phone number and sim slot index using socket, if default sim is not selected android shows me sim selector popup, but if default sim is selected it will call through sim default sim.

But I want to call throug any sim index.

Same approach is working for sms, but not for phone calls

Here is my code.

@ReactMethod public void makeCallWithSim(int simSlotIndex, String phoneNumber, Callback callback) { Context context = getReactApplicationContext(); TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager == null) { // TelephonyManager is not available, handle error if (callback != null) { callback.invoke("TelephonyManager is not available."); } return; }

    SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    if (subscriptionManager == null) {
        // SubscriptionManager is not available, handle error
        if (callback != null) {
            callback.invoke("SubscriptionManager is not available.");
        }
        return;
    }

    // Get the active subscriptions
    List<SubscriptionInfo> activeSubscriptions = subscriptionManager.getActiveSubscriptionInfoList();
    if (activeSubscriptions == null || activeSubscriptions.isEmpty()) {
        // No active subscriptions found, handle error
        if (callback != null) {
            callback.invoke("No active subscriptions found.");
        }
        return;
    }

    // Check if the device supports multiple SIM cards
    if (activeSubscriptions.size() < 2) {
        // Device does not support multiple SIM cards
        // Use the default SIM card for the call
        Log.d("Info", "In IF");
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (callIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(callIntent);
            if (callback != null) {
                callback.invoke(); // Call initiated successfully
            }
        } else {
            if (callback != null) {
                callback.invoke("Error: No activity found to handle the call intent.");
            }
        }
    } else {
        Log.d("Info", "In ELSE");
        // Get the subscription ID for the given SIM slot
        // SubscriptionInfo subscriptionInfo = activeSubscriptions.get(simSlotIndex);
        SubscriptionInfo subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simSlotIndex);
        int subscriptionId = subscriptionInfo.getSubscriptionId();

        // Set the SIM subscription ID for the call
        telephonyManager = telephonyManager.createForSubscriptionId(subscriptionId);

        // Make the call using the specified SIM
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (callIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(callIntent);
            if (callback != null) {
                callback.invoke(); // Call initiated successfully
            }
        } else {
            if (callback != null) {
                callback.invoke("Error: No activity found to handle the call intent.");
            }
        }
    }
}
1

There are 1 best solutions below

0
Usama Ahmad Baig On

Try to add putExtra()

callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.putExtra("com.android.phone.extra.slot", simSlotIndex);  
callIntent.putExtra("simSlot", simSlotIndex);