Get the list of user SIM cards and select it - flutter

5.6k Views Asked by At

For the flutter application, it is necessary to send an SMS with the user's SIM card, and for this purpose, I want it to be possible to select the desired SIM card in dual-SIM phones.

i check sms_plugin package but user can't select SIM cart you must select in code .

4

There are 4 best solutions below

0
On BEST ANSWER

you can try https://pub.dev/packages/mobile_number this package,

Add dependency

dependencies:
  mobile_number: ^1.0.4

Code snippet

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mobile_number/mobile_number.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _mobileNumber = '';
  List<SimCard> _simCard = <SimCard>[];

  @override
  void initState() {
    super.initState();
    MobileNumber.listenPhonePermission((isPermissionGranted) {
      if (isPermissionGranted) {
        initMobileNumberState();
      } else {}
    });

    initMobileNumberState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initMobileNumberState() async {
    if (!await MobileNumber.hasPhonePermission) {
      await MobileNumber.requestPhonePermission;
      return;
    }
    String mobileNumber = '';
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      mobileNumber = (await MobileNumber.mobileNumber)!;
      _simCard = (await MobileNumber.getSimCards)!;
    } on PlatformException catch (e) {
      debugPrint("Failed to get mobile number because of '${e.message}'");
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _mobileNumber = mobileNumber;
    });
  }

  Widget fillCards() {
    List<Widget> widgets = _simCard
        .map((SimCard sim) => Text(
            'Sim Card Number: (${sim.countryPhonePrefix}) - ${sim.number}\nCarrier Name: ${sim.carrierName}\nCountry Iso: ${sim.countryIso}\nDisplay Name: ${sim.displayName}\nSim Slot Index: ${sim.slotIndex}\n\n'))
        .toList();
    return Column(children: widgets);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text('Running on: $_mobileNumber\n'),
              fillCards()
            ],
          ),
        ),
      ),
    );
  }
}
0
On

You can create communication channel between Android API and Flutter to get sim list.

Eg. https://github.com/flutter-moum/flutter_sim_info/blob/master/android/src/main/java/flutter/moum/sim_info/MethodHandlerImpl.java

or else you can use this Flutter package https://github.com/flutter-moum/flutter_sim_info

0
On

I would lean this library to use:

https://pub.dev/packages/sim_data

import 'package:sim_data/sim_data.dart';

void printSimCardsData() async {
  try {
    SimData simData = await SimDataPlugin.getSimData();
    for (var s in simData.cards) {
      print('Serial number: ${s.serialNumber}');
    }
  } on PlatformException catch (e) {
    debugPrint("error! code: ${e.code} - message: ${e.message}");
  }
}

void main() => printSimCardsData();
0
On

Get Device SIM Information


Use the AutoUssdFlutter package to get the details of SIM cards

import 'package:autoussdflutter/autoussdflutter.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final List<Network> networks = await AutoUssdFlutter
    .getInstance()
    .getDeviceSimNetworks();

  for (final network in networks) {
    debugPrint("Name: ${network.name}");
    debugPrint("MCC: ${network.mcc}");
    debugPrint("MNC: ${network.mnc}");
    debugPrint("Country Name: ${network.countryName}");
    debugPrint("Country ISO Code: ${network.countryIsoCode}");
    debugPrint("Country Dial Code: ${network.countryDialCode}");
  }
}

Set SIM To Send SMS


To actually select a SIM to send the message with (in a multi-SIM phone) is tricky on the Flutter side, and also depends on the user's SIM setting:

  1. Has the user set a default SIM card or
  2. Does the device ask which SIM to use every time an SMS is being sent

Most packages (E.g. Telephony) seem to delegate this to the device (I.e. allow the device to bring a prompt for the user to choose the SIM or just use the default SIM, if set)

If you want to override this and explicitly set the SIM to use, you may need to look into a platform specific solution.