SMS Send without manual sim ard choosing option

This commit is contained in:
2025-10-22 12:49:38 +05:30
parent 7a0265ad8d
commit c9c52b39fa
3 changed files with 65 additions and 77 deletions

View File

@@ -1,7 +1,10 @@
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:simcards/sim_card.dart';
import 'package:simcards/simcards.dart';
import 'package:flutter_sms/flutter_sms.dart';
import 'package:uuid/uuid.dart';
class SmsService {
final Simcards _simcards = Simcards();
@@ -12,81 +15,56 @@ class SmsService {
required String message,
}) async {
try {
// Request permission to access SIM cards
await _simcards.requestPermission();
bool permissionGranted = await _simcards.hasPermission();
if (!permissionGranted) {
print("❌ Permission denied.");
print("❌ Permission denied." );
return;
}
List<SimCard> simCardList = await _simcards.getSimCards();
if (simCardList.isEmpty) {
print("❌ No SIM detected.");
print("❌ No SIM detected." );
return;
}
// If only one SIM card is available, send SMS directly
if (simCardList.length == 1) {
await _sendSms(destinationNumber, message, simCardList.first);
return;
}
// Show a dialog to let the user select a SIM card
int? selectedSimSlot = await showDialog<int>(
context: context,
builder: (BuildContext dialogContext) {
int? tempSelection;
return AlertDialog(
title: const Text("Select SIM to send SMS"),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DropdownButton<int>(
hint: const Text("Choose SIM"),
value: tempSelection,
onChanged: (int? newValue) {
setState(() {
tempSelection = newValue;
});
},
items: List.generate(simCardList.length, (index) {
final sim = simCardList[index];
return DropdownMenuItem<int>(
value: index,
child: Text(sim.displayName ?? "SIM ${index + 1}"),
);
}),
);
},
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(dialogContext, tempSelection);
},
child: const Text("OK"),
),
],
);
},
);
if (selectedSimSlot == null) {
print("⚠️ SIM not selected.");
return;
}
// Send SMS using the selected SIM card
await _sendSms(destinationNumber, message, simCardList[selectedSimSlot]);
} catch (e) {
print("⚠️ Error sending SMS: $e");
print("⚠️ Error in SMS process: $e");
}
}
Future<void> _sendSms(
String destinationNumber, String message, SimCard selectedSim) async {
// SMS Logic Here
print("✅ SMS sent via ${selectedSim.displayName}");
if (Platform.isAndroid) {
try {
// Generate a unique ID
var uuid = const Uuid();
String uniqueId = uuid.v4(); // Generates a random v4 UUID
// The message will be the unique ID
String smsMessage = uniqueId;
// flutter_sms sends the message using the device's default SMS app.
// It does not programmatically select a SIM card. The 'selectedSim'
// parameter from the dialog will not be used to choose the sending SIM.
String result = await sendSMS(
message: smsMessage,
recipients: [destinationNumber],
sendDirect: true, // This attempts to send directly without opening the messaging app.
// It requires more permissions and might not work on all devices/Androidversions.
// If false, it will open the default SMS app.
);
print("✅ SMS send result: $result. Sent via ${selectedSim.displayName} (Note: OS default SIM isused).");
} catch (e) {
print("⚠️ Error sending SMS: $e");
}
} else {
print("⚠️ SMS sending is only supported on Android.");
}
}
}

View File

@@ -307,6 +307,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_sms:
dependency: "direct main"
description:
name: flutter_sms
sha256: "2fe5f584f02596343557eeca56348f9b82413fefe83a423fab880cdbdf54d8d8"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
flutter_svg:
dependency: "direct main"
description:
@@ -955,7 +963,7 @@ packages:
source: hosted
version: "3.1.4"
uuid:
dependency: transitive
dependency: "direct main"
description:
name: uuid
sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff

View File

@@ -62,6 +62,8 @@ dependencies:
showcaseview: ^2.0.3
package_info_plus: ^4.2.0
simcards: ^0.0.1
flutter_sms: ^2.3.3
uuid: ^4.5.1
# jailbreak_root_detection: "^1.1.6"