目录

VoIP Call Control from Peripherals

Use Cases

Starting from API version 26.0.1, VoIP call control from peripherals is supported.

A user can control VoIP calls on the local device using a remote device, such as a third-party wearable. In this scenario, an app running on the remote device can answer or reject an incoming call, or end the current call on the local device. The system automatically selects the VoIP call to be controlled, prioritizing the first call. The app does not need to specify a callId.

The following operations are currently supported:

Constraints

[h2]Usage Constraints

Bluetooth or WLAN must be enabled on both the local and remote devices. The local and remote devices must also be successfully paired and connected.

[h2]Device Constraints

Local devices: phones, tablets, PCs/2-in-1 devices, and wearables that run HarmonyOS 7 or later.

API Description

The APIs for controlling VoIP calls from peripherals are provided by voipCallControl. For details about the APIs, please refer to the API Reference

API Description
answerCall(videoState?: VideoStateType): Promise<void> Answers an incoming call. Use videoState to specify whether to answer the call as a voice call or a video call.
rejectCall(): Promise<void> Rejects an incoming call.
hangUpCall(): Promise<void> Ends the current call.

Development Procedure

  1. Before development, declare the ohos.permission.MANAGE_CALL_FOR_DEVICES permission in the app configuration file. For details, please refer to Declaring Permissions.

  2. Import dependencies.

    import { voipCallControl } from '@kit.CallServiceKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { observer } from '@kit.TelephonyKit';
  3. Call observer.onVoIPStateChange to listen for VoIP call state changes.

  4. Answer an incoming call.

    Answer as a voice call: Pass TYPE_VOICE.

    The sample code is as follows:

    // Answer an incoming call as a voice call.
    voipCallControl.answerCall().then(() => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in answering the voice call.');
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'CallDemo', 'Failed to answer the voice call: %{public}d %{public}s', err.code, err.message);
    });

    Answer as a video call: Pass TYPE_VIDEO.

    The sample code is as follows:

    // Answer an incoming call as a video call.
    voipCallControl.answerCall(voipCallControl.VideoStateType.TYPE_VIDEO).then(() => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in answering the video call.');
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'CallDemo', 'Failed to answer the video call: %{public}d %{public}s', err.code, err.message);
    });
  5. Reject an incoming call.

    The sample code is as follows:

    // Reject an incoming call.
    voipCallControl.rejectCall().then(() => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in rejecting the call.');
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'CallDemo', 'Failed to reject the call: %{public}d %{public}s', err.code, err.message);
    });
  6. End the current call.

    The system automatically selects the call to hang up. It prioritizes the active call. If there is no active call while an incoming call is being received, the system hangs up the incoming call.

    The sample code is as follows:

    // End the current call.
    voipCallControl.hangUpCall().then(() => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in hanging up the call.');
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'CallDemo', 'Failed to hang up the call: %{public}d %{public}s', err.code, err.message);
    });