How can I make a direct PHONE CALL in Flutter

6,699

Solution 1

Disclaimer: plugin author here.

Since Android API level 26, the method sendUssdRequest is exposed to make silent USSD requests.

I made a Flutter plugin called ussd_service to be able to easily access it from dart in a Flutter application. It can be used in the following manner:

import 'package:ussd_service/ussd_service.dart';

makeMyRequest() async {
  int subscriptionId = 1; // sim card subscription Id
  String code = "*21#"; // ussd code payload
  try {
    String ussdSuccessMessage = await UssdService.makeRequest(subscriptionId, code);
    print("succes! message: $ussdSuccessMessage");
  } on PlatformException catch (e) {
    print("error! code: ${e.code} - message: ${e.message}");
  }
};

makeMyRequest();

Hope this helps! Let me know on the Github repo's issues if you have any issue with it.

Solution 2

You need to use URL encoding for special character.

Like this:

launch("tel:" + Uri.encodeComponent('*123#'));

Solution 3

I made a plugin called flutter_phone_direct_caller exactly for this purpose.

You can use it like this:

import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';

void main() {
  runApp(Scaffold(
    body: Center(
      child: RaisedButton(
        onPressed: _callNumber,
        child: Text('Call Number'),
      ),
    ),
  ));
}

_callNumber() async{
  const number = '08592119XXXX'; //set the number here
  bool res = await FlutterPhoneDirectCaller.callNumber(number);
}

Hopefully it can help someone.

Share:
6,699
Deepak Gehlot
Author by

Deepak Gehlot

Working with widevision.

Updated on December 08, 2022

Comments

  • Deepak Gehlot
    Deepak Gehlot over 1 year

    I need to make a direct Phone Call in flutter but it's just opening the Phone app dialer.
    No direct phone call is made.

    In fact, I also tried with url_launcher package for this task but am getting the same result.

        _launchURL() async {
        SimplePermissions.requestPermission(Permission.CallPhone)
            .then((state) async {
          if (state == PermissionStatus.authorized) {
            String a = Uri.encodeFull("#");
            String url = 'tel:*123' + a;
            if (await canLaunch(url)) {
              await launch(url);
            } else {
              throw 'Could not launch $url';
            }
          }
        });}
    

    Has anyone solved this before?

  • MwamiTovi
    MwamiTovi over 4 years
    This user (in question) seems to be intent on making phone calls. Evidence, he used url_launcher which doesn't do ussd either.
  • vkammerer
    vkammerer over 4 years
    @MwamiTovi the String passed ("tel:*123#") indicates that the intended action is to send a USSD request rather than launch a phone call. While it is possible to do it by popping up the dialer application, my reply explains how to do it in the background in Android.
  • vkammerer
    vkammerer about 4 years
    @SifaturRahman this is because you need a main function entry point in dart. see pub.dev/packages/ussd_service#-example-tab- for a full working example
  • kimoduor
    kimoduor over 3 years
    The plugin(flutter_phone_direct_caller) remains undefined in flutter 1.20.4. i am not sure what is hapening