Я получил следующий код: < /p>
mainactivity.ktобразной
Код: Выделить всё
package deep.esim.deep_esim_mobile
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.os.Bundle
class MainActivity : FlutterActivity() {
private val CHANNEL = "deep.esim.mobile/esim"
private lateinit var esimService: EsimService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
esimService = EsimService(this)
val flutterEngine = this.getFlutterEngine()
if (flutterEngine != null) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"isEsimAvailable" -> {
try {
val isAvailable = esimService.isEsimAvailable()
result.success(isAvailable)
} catch (e: Exception) {
result.error("ESIM_ERROR", "Failed to check eSIM availability", e.message)
}
}
"startEsimActivation" -> {
try {
val activationCode = call.argument("activationCode")
if (activationCode != null) {
val success = esimService.startEsimActivation(activationCode)
result.success(success)
} else {
result.error("INVALID_ARGUMENT", "Activation code is required", null)
}
} catch (e: Exception) {
result.error("ESIM_ERROR", "Failed to start eSIM activation", e.message)
}
}
}
}
}
}
< /code>
eSimservice.kt
package deep.esim.deep_esim_mobile
import android.content.Context
import android.telephony.TelephonyManager
import android.telephony.SubscriptionManager
import android.telephony.euicc.EuiccManager
import android.telephony.euicc.DownloadableSubscription
import android.os.Build
import androidx.annotation.RequiresApi
class EsimService(private val context: Context) {
private val telephonyManager: TelephonyManager by lazy {
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
}
private val subscriptionManager: SubscriptionManager by lazy {
context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
}
private val euiccManager: EuiccManager? by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
context.getSystemService(Context.EUICC_SERVICE) as EuiccManager
} else {
null
}
}
/**
* Check if eSIM is available on the device
* @return true if eSIM is available, false otherwise
*/
fun isEsimAvailable(): Boolean {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
euiccManager?.isEnabled == true
} else {
telephonyManager.phoneType == TelephonyManager.PHONE_TYPE_GSM ||
telephonyManager.phoneType == TelephonyManager.PHONE_TYPE_CDMA
}
} catch (e: Exception) {
false
}
}
/**
* Start eSIM activation process
* This method triggers the system's LUI (Local User Interface) flow for eSIM activation
*
* @param activationCode The activation code for the eSIM profile
* @return true if activation was started successfully, false otherwise
*/
fun startEsimActivation(activationCode: String): Boolean {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val manager = euiccManager ?: return false
if (!manager.isEnabled) {
return false
}
val downloadableSubscription = DownloadableSubscription.forActivationCode(activationCode)
val intent = android.content.Intent(context, MainActivity::class.java)
val pendingIntent = android.app.PendingIntent.getActivity(
context,
0,
intent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT or android.app.PendingIntent.FLAG_IMMUTABLE
)
manager.downloadSubscription(
downloadableSubscription,
false,
pendingIntent
)
true
} else {
false
}
} catch (e: Exception) {
false
}
}
}
< /code>
android manifest.kt
< /code>
startEsimActivationsuper.onNewIntent(intent)
// Handle eSIM download callback
if (intent.action == "DOWNLOAD_CALLBACK") {
val resultCode = intent.getIntExtra(
android.telephony.euicc.EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DOWNLOADABLE_SUBSCRIPTION,
android.telephony.euicc.EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR
)
handleDownloadResult(resultCode, intent)
}
}
fun handleDownloadResult(resultCode: Int, data: Intent?, callback: EsimActivationCallback) {
when (resultCode) {
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> {
Log.d(TAG, "eSIM downloaded successfully")
callback.onSuccess()
}
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> {
Log.d(TAG, "Resolvable error occurred")
handleResolvableError(data, callback)
}
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR -> {
val errorCode = data?.getIntExtra(
EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
-1
) ?: -1
// Get additional error information
val errorMessage = getDetailedErrorMessage(errorCode, data)
Log.e(TAG, "eSIM download failed with error code: $errorCode")
Log.e(TAG, "Error details: $errorMessage")
// Log additional intent extras for debugging
data?.extras?.let { extras ->
Log.d(TAG, "Intent extras: ${extras.keySet()}")
for (key in extras.keySet()) {
Log.d(TAG, "Extra $key: ${extras.get(key)}")
}
}
callback.onError(errorCode, errorMessage)
}
else -> {
val errorMessage = "Unknown result code: $resultCode"
Log.e(TAG, errorMessage)
callback.onError(resultCode, errorMessage)
}
}
}
private fun getDetailedErrorMessage(errorCode: Int, data: Intent?): String {
return when (errorCode) {
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR -> {
"General eSIM download error"
}
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> {
"Resolvable error - user action required"
}
EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> {
"Success"
}
else -> {
val errorMessage = data?.getStringExtra("error_message") ?: "Unknown error"
"Error code $errorCode: $errorMessage"
}
}
}
< /code>
Now I know that it fails with EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, but still have no idea why.
Подробнее здесь: https://stackoverflow.com/questions/797 ... t-starting
Мобильная версия