6

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
  • 2,425
  • 17
  • 25
Deepak Gehlot
  • 2,661
  • 5
  • 21
  • 25

4 Answers4

6

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.

vkammerer
  • 2,047
  • 1
  • 14
  • 9
  • This user (in question) seems to be intent on making phone calls. Evidence, he used `url_launcher` which doesn't do `ussd` either. – MwamiTovi Jan 19 '20 at 06:41
  • 1
    @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 Jan 20 '20 at 09:04
  • @SifaturRahman this is because you need a `main` function entry point in dart. see https://pub.dev/packages/ussd_service#-example-tab- for a full working example – vkammerer Mar 11 '20 at 18:50
2

You need to use URL encoding for special character.

Like this:

launch("tel:" + Uri.encodeComponent('*123#'));
davidrm90
  • 21
  • 4
1

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.

Yanis Alfian
  • 31
  • 1
  • 3
  • The plugin(flutter_phone_direct_caller) remains undefined in flutter 1.20.4. i am not sure what is hapening – kimoduor Jan 10 '21 at 16:33
0

Add these packages to your dependencies:

android_intent_plus: ^2.0.0
permission_handler: ^8.1.4+2

Import them in your .dart file:

import 'package:android_intent_plus/android_intent.dart';
import 'package:permission_handler/permission_handler.dart';

Request for phone call permission in your main() function:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Permission.phone.request();
  runApp(const MyApp());
}

And finally you can make direct phone calls like this:

AndroidIntent intent = const AndroidIntent(
  action: 'android.intent.action.CALL',
  data: 'tel:2125551212',
);
await intent.launch();
Ercan Tomaç
  • 29
  • 1
  • 5