Facing an issue while installing ESim directly into android

437 Views Asked by At

Currently, i'm working on application which is related to eSIM installation by using direct method, i followed Implementing eSIM provided example code but not succeeded yet.

i used TEST_SIM_PROFILE, TEST_SIM_PROFILE1 and TEST_SIM_PROFILE3 but it always return 0 in onNewIntent(intent: Intent), so please check is there anything need to be add or improve into my code,

here is my Code


import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccManager
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    private val TEST_SIM_PROFILE =
        "LPA:1\$MTAwMDAwMDAwMzQ2MzM5Y2E2NDgwNQ==$7UMyhYJKZLBfHZt2ve9vQH0oKvE=$"
    private val TEST_SIM_PROFILE1 =
        "1\$prod.smdp-plus.rsp.goog\$3TD6-8L82-HUE1-LVN6"
    private val TEST_SIM_PROFILE2 = "LPA:1\$lpa.airalo.com\$TEST"
    private val TEST_SIM_PROFILE3 = "dfdffdf"
    private val REQUEST_CODE_INSTALL = 1

    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val esimButton = findViewById<Button>(R.id.esimButton)
        esimButton.setOnClickListener { downloadTestProfile() }
    }


    @RequiresApi(Build.VERSION_CODES.P)
    private fun downloadTestProfile() {
        val euiccManager = getSystemService(EUICC_SERVICE) as EuiccManager
        if (!euiccManager.isEnabled) {
            Toast.makeText(this, "eSIM is not supported on this device", Toast.LENGTH_LONG).show()
            return
        }
        Log.d("eSIMInstall", "Attempting to install profile...");
        val subscription = DownloadableSubscription.forActivationCode(TEST_SIM_PROFILE)
        euiccManager.downloadSubscription(
            subscription, true, PendingIntent.getActivity(
                this, REQUEST_CODE_INSTALL, Intent(this, MainActivity::class.java),
                PendingIntent.FLAG_IMMUTABLE
            )
        )
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Log.d("eSIMInstall", "on calling of newIntent...");
        val resultCode =
            intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0)
        Log.d("eSIMResult", "Result code: $resultCode");

        when (resultCode) {
            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> Toast.makeText(
                this,
                "eSIM profile installed successfully!",
                Toast.LENGTH_LONG
            ).show()

            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> { }
            else -> Toast.makeText(this, "Failed to install eSIM profile", Toast.LENGTH_LONG).show()
        }
    }
}
------------------------Manifest----------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.WRITE_EMBEDDED_SUBSCRIPTIONS"
        tools:ignore="ProtectedPermissions" />
    <uses-feature android:name="android.permission.MODIFY_PHONE_STATE"
        tools:ignore="ProtectedPermissions" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.EsimInstallation"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1

There are 1 best solutions below

1
On

The code you provided looks mostly correct, but there are a few issues that need to be addressed. I've made the necessary corrections and improvements to your code to handle eSIM installation and results properly. Here's the corrected code in Kotlin:

import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccManager
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private val TEST_SIM_PROFILE =
        "LPA:1\$MTAwMDAwMDAwMzQ2MzM5Y2E2NDgwNQ==$7UMyhYJKZLBfHZt2ve9vQH0oKvE=$"
    private val REQUEST_CODE_INSTALL = 1

    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val esimButton = findViewById<Button>(R.id.esimButton)
        esimButton.setOnClickListener { downloadTestProfile() }
    }

    @RequiresApi(Build.VERSION_CODES.P)
    private fun downloadTestProfile() {
        val euiccManager = getSystemService(EUICC_SERVICE) as EuiccManager
        if (!euiccManager.isEnabled) {
            Toast.makeText(this, "eSIM is not supported on this device", Toast.LENGTH_LONG).show()
            return
        }
        Log.d("eSIMInstall", "Attempting to install profile...")
        val subscription = DownloadableSubscription.forActivationCode(TEST_SIM_PROFILE)
        euiccManager.downloadSubscription(
            subscription,
            true,
            PendingIntent.getActivity(
                this,
                REQUEST_CODE_INSTALL,
                Intent(this, MainActivity::class.java),
                PendingIntent.FLAG_IMMUTABLE
            )
        )
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Log.d("eSIMInstall", "on calling of newIntent...")
        val resultCode =
            intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_RESULT, EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR)
        Log.d("eSIMResult", "Result code: $resultCode")

        when (resultCode) {
            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> Toast.makeText(
                this,
                "eSIM profile installed successfully!",
                Toast.LENGTH_LONG
            ).show()

            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> {
                // Handle resolvable error here if needed
            }
            else -> Toast.makeText(this, "Failed to install eSIM profile", Toast.LENGTH_LONG).show()
        }
    }
}

I've made the following changes and improvements:

Changed the key for retrieving the result code from EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE to EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_RESULT. This is the correct key for obtaining the result code.

Removed the TEST_SIM_PROFILE1, TEST_SIM_PROFILE2, and TEST_SIM_PROFILE3 variables as they were not used in your code.

Updated the handling of result codes to use EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR as the default value for resultCode.

Added a comment indicating where you can handle resolvable errors if needed.

Make sure to replace the TEST_SIM_PROFILE value with the appropriate activation code for your eSIM profile.