• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Call Service Development
2
3## Scenario Description
4
5You can implement the call service in either of the following ways:
6- For a system application, use the **dialCall** API to make a voice or video call. The call will be displayed on the application page.
7- For a third-party application, use the **makeCall** API to start the system call application. Users can then make calls as needed.
8
9## Basic Concepts
10
11- Call status code
12  A code used to report the current call status to the application, so that the application can then take appropriate logic processing. For example, if there is no ongoing call, the application allows you to make a new call.
13
14  | Name              | Value  | Description                                                        |
15  | ------------------ | ---- | ------------------------------------------------------------ |
16  | CALL_STATE_UNKNOWN | -1   | The call status fails to be obtained and is unknown.                        |
17  | CALL_STATE_IDLE    | 0    | No call is in progress.                                    |
18  | CALL_STATE_RINGING | 1    | The call is in the ringing or waiting state.                                    |
19  | CALL_STATE_OFFHOOK | 2    | At least one call is in dialing, active, or on hold, and no new incoming call is ringing or waiting.|
20
21## Constraints
22
231. The call service is available only on standard-system devices.
242. An available SIM card must be present on the device.
25
26
27## Available APIs
28
29> **NOTE**
30> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [call API Reference](../reference/apis/js-apis-call.md).
31
32|                                  Name                                            | Description                                                        |
33| ----------------------------------------------------------------------------------- | ------------------------------------------------------------ |
34| hasVoiceCapability(): boolean;                                                      | Checks whether the voice function is available.                                       |
35| dialCall(phoneNumber: string, callback: AsyncCallback<void>): void                   | Makes a call. This is a system API.                                     |
36| makeCall(phoneNumber: string, callback: AsyncCallback<void>): void                  | Redirects to the dial screen and displays the called number.                                 |
37
38The **observer** module provides the functions of subscribing to and unsubscribing from the call service status. For details about the APIs, see [observer API Reference](../reference/apis/js-apis-observer.md).
39
40| Name                                                      | Description              |
41| ------------------------------------------------------------ | ------------------ |
42| on(type: 'callStateChange', options: { slotId: number }, callback: Callback<{ state: CallState, number: string }>): void | Listens to call status changes.|
43
44## How to Develop
45
46### Making a Call by Using the dialCall API (for System Applications Only)
47
481. Declare the required permission: **ohos.permission.PLACE_CALL**.
49This permission is of the **system\_basic** level. Before applying for the permission, ensure that the [basic principles for permission management](../security/accesstoken-overview.md#basic-principles-for-permission-management) are met. Then, declare the corresponding permission by following instructions in [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
502. Import the **call** and **observer** modules.
513. Invoke the **hasVoiceCapability** API to check whether the device supports the voice call function.
52   If the voice call function is supported, the user will be redirected to the dial screen and the dialed number is displayed.
534. Invoke the **dialCall** API to make a call.
545. (Optional) Register the observer for call service status changes.
55   ```ts
56    // Import the required modules.
57    import call from '@ohos.telephony.call'
58    import observer from '@ohos.telephony.observer'
59    import { BusinessError } from '@ohos.base';
60
61    // Check whether the voice call function is supported.
62    let isSupport = call.hasVoiceCapability();
63    if (isSupport) {
64        // If the device supports the voice call function, call the following API to make a call.
65        call.dialCall("13xxxx", (err: BusinessError) => {
66            console.log(`callback: dial call err->${JSON.stringify(err)}`)
67        })
68
69        // (Optional) Register the observer for call service status changes.
70        class SlotId {slotId: number = 0}
71        class CallStateCallback {
72            state: call.CallState = call.CallState.CALL_STATE_UNKNOWN;
73            number: string = ""
74        }
75        let slotId: SlotId = {slotId: 0}
76        observer.on("callStateChange", slotId, (data: CallStateCallback) => {
77            console.log("call state change, data is:" + JSON.stringify(data));
78        });
79    }
80   ```
81
82### Making a Call by Using the makeCall API
83
841. Import the **call** and **observer** modules.
852. Invoke the **hasVoiceCapability** API to check whether the device supports the voice call function.
86   If the voice call function is supported, the user will be redirected to the dial screen and the dialed number is displayed.
873. Invoke the **makeCall** API to start the system call application and make a call.
884. (Optional) Register the observer for call service status changes.
89
90   ```ts
91    // Import the required modules.
92    import call from '@ohos.telephony.call'
93    import observer from '@ohos.telephony.observer'
94    import { BusinessError } from '@ohos.base';
95
96    // Check whether the voice call function is supported.
97    let isSupport = call.hasVoiceCapability();
98    if (isSupport) {
99        // If the voice call function is supported, the user will be redirected to the dial screen and the dialed number is displayed.
100        call.makeCall("13xxxx", (err: BusinessError) => {
101            if (!err) {
102                console.log("make call success.");
103            } else {
104                console.log("make call fail, err is:" + JSON.stringify(err));
105            }
106        });
107        // (Optional) Register the observer for call service status changes.
108        class SlotId {slotId: number = 0}
109        class CallStateCallback {
110            state: call.CallState = call.CallState.CALL_STATE_UNKNOWN;
111            number: string = ""
112        }
113        let slotId: SlotId = {slotId: 0}
114        observer.on("callStateChange", slotId, (data: CallStateCallback) => {
115            console.log("call state change, data is:" + JSON.stringify(data));
116        });
117    }
118   ```
119
120