You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

小米/Redmi设备eSIM安装返回EMBEDDED_SUBSCRIPTION_RESULT_ERROR但实际已安装的问题求助

小米/Redmi设备eSIM安装返回EMBEDDED_SUBSCRIPTION_RESULT_ERROR但实际已安装的问题求助

大家好,我正在给我的Flutter应用开发eSIM安装功能,基于原生Android代码实现,但在小米/Redmi设备上遇到了一个非常奇怪的问题,想请教社区里的朋友们有没有解决方案。

我梳理一下整个踩坑过程:

  • 最开始我尝试使用flutter_esim插件,它底层调用的是EuiccManager的downloadSubscription方法,但在我们的三星测试设备上一直崩溃。
  • 后来我换成启动EuiccManager.ACTION_START_EUICC_ACTIVATION系统Intent的方式,大部分机型都正常工作,唯独小米/Redmi设备会直接返回Activity.RESULT_CANCELED,甚至连任何系统提示都不弹出来。
  • 为了找思路,我研究了Play Store里的一款同类eSIM应用,发现它没有启动Intent,而是直接调用downloadSubscription方法。于是我在自己的原生代码里做了条件判断,针对小米/Redmi设备直接使用downloadSubscription——结果现在会返回EMBEDDED_SUBSCRIPTION_RESULT_ERROR错误,但实际eSIM已经成功安装到设备上了,和那款参考App的行为完全一致。
  • 目前我只能临时加了一个提示,让用户安装完成后去系统eSIM设置里确认,但这种体验实在不太好,想找到更合理的处理方案。

以下是我当前的MainActivity.kt代码:

package com.pacakage.pacakage

import android.annotation.SuppressLint
import android.app.Activity
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccManager
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterFragmentActivity() {
    private val CHANNEL = "samples.flutter.dev/esim"
    private val TAG = "MainActivity"
    private val ACTION_DOWNLOAD_SUBSCRIPTION = "com.pacakage.esim.DOWNLOAD_RESULT"
    private val REQUEST_CODE_INSTALL = 9999

    private var methodResult: MethodChannel.Result? = null
    private lateinit var euiccManager: EuiccManager
    private lateinit var downloadReceiver: BroadcastReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        euiccManager = getSystemService(EuiccManager::class.java) ?: throw IllegalStateException("EUICC not available")

        // Initialize the broadcast receiver
        downloadReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if (ACTION_DOWNLOAD_SUBSCRIPTION != intent.action) {
                    return
                }

                when (resultCode) {
                    EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> {
                        Log.d(TAG, "Resolvable error encountered")
                        handleResolvableError(intent)
                    }
                    EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> {
                        Log.d(TAG, "eSIM installation successful (via broadcast)")
                        methodResult?.success("Installation successful")
                        methodResult = null
                    }
                    EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR -> {
                        Log.e(TAG, "eSIM installation failed (via broadcast)")
                        methodResult?.error(
                            "ESIM_INSTALLATION_ERROR",
                            "EMBEDDED_SUBSCRIPTION_RESULT_ERROR",
                            null
                        )
                        methodResult = null
                    }
                    else -> {
                        Log.e(TAG, "Unknown result code from broadcast: $resultCode")
                        methodResult?.error(
                            "UNKNOWN_ERROR",
                            "Unknown error occurred (result code: $resultCode)",
                            null
                        )
                        methodResult = null
                    }
                }
            }
        }
    }

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    override fun onResume() {
        super.onResume()
        // Register the receiver
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(
                downloadReceiver,
                IntentFilter(ACTION_DOWNLOAD_SUBSCRIPTION),
                Context.RECEIVER_NOT_EXPORTED
            )
        } else {
            registerReceiver(
                downloadReceiver,
                IntentFilter(ACTION_DOWNLOAD_SUBSCRIPTION)
            )
        }
    }

    override fun onPause() {
        super.onPause()
        // Unregister the receiver to avoid leaks
        try {
            unregisterReceiver(downloadReceiver)
        } catch (e: IllegalArgumentException) {
            // Receiver was not registered, ignore
        }
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "launchESimSetup") {
                handleEsimSetupCall(call, result)
            } else {
                result.notImplemented()
            }
        }
    }

    private fun handleEsimSetupCall(call: MethodCall, result: MethodChannel.Result) {
        if (methodResult != null) {
            result.error("ALREADY_IN_PROGRESS", "Another eSIM installation is already in progress", null)
            return
        }
        try {
            methodResult = result
            // 针对小米/Redmi设备的特殊处理:直接调用downloadSubscription而非启动系统Intent
            val smdpAddress = call.argument<String>("smdpAddress")
            val activationCode = call.argument<String>("activationCode")
            val downloadableSubscription = DownloadableSubscription.forActivationCode(activationCode)
            val intent = Intent(ACTION_DOWNLOAD_SUBSCRIPTION)
            val pendingIntent = PendingIntent.getBroadcast(
                this,
                REQUEST_CODE_INSTALL,
                intent,
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else PendingIntent.FLAG_UPDATE_CURRENT
            )
            euiccManager.downloadSubscription(downloadableSubscription, smdpAddress, pendingIntent)
        } catch (e: Exception) {
            methodResult?.error("EXCEPTION", e.localizedMessage, null)
            methodResult = null
        }
    }

    private fun handleResolvableError(intent: Intent) {
        val resolutionIntent = intent.getParcelableExtra<Intent>(EuiccManager.EXTRA_RESOLUTION_INTENT)
        resolutionIntent?.let {
            startActivity(it)
        }
    }
}

现在的核心问题是:明明eSIM已经成功安装到设备上了,但系统还是返回EMBEDDED_SUBSCRIPTION_RESULT_ERROR,导致我无法在App里给用户准确的成功反馈。有没有朋友遇到过类似的小米/Redmi设备eSIM适配问题,或者知道如何绕过错误码、直接判断eSIM是否真的安装成功?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:04:31