diff --git a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt index 64d792b..5f2c1ae 100644 --- a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt +++ b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt @@ -8,6 +8,7 @@ import android.content.pm.PackageManager import android.net.Uri import android.os.Build import android.telephony.SmsManager +import android.telephony.SubscriptionManager import android.util.Log import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin @@ -73,10 +74,67 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { sendSMS(result, recipients, message!!, sendDirect) } "canSendSMS" -> result.success(canSendSMS()) + "getAvailableSIMs" -> result.success(getAvailableSIMs()) + "sendSMSWithSubscriptionId" -> { + val message = call.argument("message") ?: "" + val recipients = call.argument("recipients") ?: "" + val subscriptionId = call.argument("subscriptionId") ?: -1 + sendSMSWithSubscriptionId(result, recipients, message, subscriptionId) + } else -> result.notImplemented() } } + @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) + private fun sendSMSWithSubscriptionId(result: Result, phones: String, message: String, subscriptionId: Int) { + // 创建短信发送状态的 PendingIntent + val sentIntent = PendingIntent.getBroadcast(activity, 0, Intent("SMS_SENT_ACTION"), PendingIntent.FLAG_IMMUTABLE) + // 根据 subscriptionId 获取对应的 SmsManager 实例 + val smsManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && subscriptionId != -1) { + SmsManager.getSmsManagerForSubscriptionId(subscriptionId) + } else { + SmsManager.getDefault() + } + // 将电话号码字符串按分号分割成多个号码 + val numbers = phones.split(";") + + for (num in numbers) { + // 打印短信长度信息 + Log.d("Flutter SMS", "msg.length() : " + message.toByteArray().size) + if (message.toByteArray().size > 80) { + // 短信长度超过 80 字节,分割短信 + val partMessage = smsManager.divideMessage(message) + // 发送分割后的多条短信 + smsManager.sendMultipartTextMessage(num, null, partMessage, null, null) + } else { + // 短信长度未超过 80 字节,直接发送单条短信 + smsManager.sendTextMessage(num, null, message, sentIntent, null) + } + } + // 短信发送成功,返回结果 + result.success("SMS Sent with subscription ID!") + } + + + // 修改 getAvailableSIMs 方法,只返回 SIM 卡 ID + @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) + private fun getAvailableSIMs(): List { + val availableSIMs = mutableListOf() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { + val subscriptionManager = activity?.getSystemService(Activity.TELEPHONY_SUBSCRIPTION_SERVICE) as? SubscriptionManager + if (subscriptionManager != null) { + val subscriptions = subscriptionManager.activeSubscriptionInfoList + if (subscriptions != null) { + for (subscription in subscriptions) { + availableSIMs.add(subscription.subscriptionId) + } + } + } + } + return availableSIMs + } + + @TargetApi(Build.VERSION_CODES.ECLAIR) private fun canSendSMS(): Boolean { if (!activity!!.packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index e5dcf6e..c17c8c7 100644 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_ios","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_ios-6.3.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_android","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_android-6.3.14/","native_build":true,"dependencies":[],"dev_dependency":false}],"macos":[{"name":"url_launcher_macos","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_macos-3.2.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[{"name":"url_launcher_linux","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_linux-3.2.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"windows":[{"name":"url_launcher_windows","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_windows-3.1.4/","native_build":true,"dependencies":[],"dev_dependency":false}],"web":[{"name":"flutter_sms","path":"/home/david/code/forks/flutter_sms/","dependencies":[],"dev_dependency":false},{"name":"url_launcher_web","path":"/home/david/.pub-cache/hosted/pub.dev/url_launcher_web-2.4.0/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2025-02-21 17:07:22.951360","version":"3.29.0","swift_package_manager_enabled":{"ios":false,"macos":false}} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_ios","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_ios-6.3.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"url_launcher_android","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_android-6.3.14/","native_build":true,"dependencies":[],"dev_dependency":false}],"macos":[{"name":"url_launcher_macos","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_macos-3.2.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[{"name":"url_launcher_linux","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_linux-3.2.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"windows":[{"name":"url_launcher_windows","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_windows-3.1.4/","native_build":true,"dependencies":[],"dev_dependency":false}],"web":[{"name":"flutter_sms","path":"/home/kun/桌面/flutter_sms/","dependencies":[],"dev_dependency":false},{"name":"url_launcher_web","path":"/home/kun/.pub-cache/hosted/pub.dev/url_launcher_web-2.4.0/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2025-03-15 01:06:57.079472","version":"3.29.1","swift_package_manager_enabled":{"ios":false,"macos":false}} \ No newline at end of file diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index b8885ec..7ceb865 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -6,7 +6,8 @@ FlutterApplication and put your custom class here. --> - + + { } Future _sendSMS(List recipients) async { + var c = await getAvailableSIMs(); + print(c); try { - String _result = await sendSMS( + String _result = await sendSMSWithSubscriptionId( message: _controllerMessage.text, recipients: recipients, - sendDirect: sendDirect, + subscriptionId: 1, ); setState(() => _message = _result); } catch (error) { diff --git a/lib/flutter_sms.dart b/lib/flutter_sms.dart index b6a61e5..c5a7855 100644 --- a/lib/flutter_sms.dart +++ b/lib/flutter_sms.dart @@ -30,3 +30,17 @@ Future launchSmsMulti({ /// Check if you can send SMS on this platform Future canSendSMS() => FlutterSmsPlatform.instance.canSendSMS(); + +Future> getAvailableSIMs() => FlutterSmsPlatform.instance.getAvailableSIMs(); + +// 添加 sendSMSWithSubscriptionId 方法 +Future sendSMSWithSubscriptionId({ + required String message, + required List recipients, + required int subscriptionId, +})=> + FlutterSmsPlatform.instance.sendSMSWithSubscriptionId( + message: message, + recipients: recipients, + subscriptionId: subscriptionId, + ); \ No newline at end of file diff --git a/lib/src/flutter_sms_platform.dart b/lib/src/flutter_sms_platform.dart index cef5396..337af7b 100644 --- a/lib/src/flutter_sms_platform.dart +++ b/lib/src/flutter_sms_platform.dart @@ -56,6 +56,31 @@ class FlutterSmsPlatform extends PlatformInterface { } } + /// 获取可用的 SIM 卡 ID 列表 + Future> getAvailableSIMs() { + return _channel + .invokeMethod>('getAvailableSIMs') + .then((value) { + return value?.map((simId) => simId as int).toList() ?? []; + }); + } + + /// 指定 SIM 卡发送短信 + Future sendSMSWithSubscriptionId({ + required String message, + required List recipients, + required int subscriptionId, + }) { + final mapData = {}; + mapData['message'] = message; + final _phones = recipients.join(';'); + mapData['recipients'] = _phones; + mapData['subscriptionId'] = subscriptionId; + return _channel + .invokeMethod('sendSMSWithSubscriptionId', mapData) + .then((value) => value ?? 'Error sending sms'); + } + Future canSendSMS() { return _channel .invokeMethod('canSendSMS')