HarmonyOS

更新时间:
复制 MD 格式

This topic describes how to integrate the software development kit (SDK) for HarmonyOS.

Environment requirements

For more information about the environment requirements for HarmonyOS, see Limits.

Integrate the SDK

Method 1: Integrate using ohpm (Recommended)

  1. Add the SDK dependencies to the `oh-package.json5` file in the root directory:

    {
      "name": "demo",
      "version": "1.0.0",
      "description": "Please describe the basic information.",
      "main": "",
      "author": "",
      "license": "",
      "dependencies": {
        "@dingrtc/dingrtc": "3.5.0",
      }
    }
    Note

    The ohpm dependency version shown here is for reference only. For the latest ohpm dependency, see SDK Download.

  2. Add the following code to the app/src/main/module.json5 file to obtain the required device permissions.

    {
      "module": {
        "name": "Demo",
        "type": "entry",
        "description": "$string:module_desc",
        "mainElement": "DemoAbility",
        "deviceTypes": [
          "phone",
          "tablet",
          "2in1"
        ],
        "deliveryWithInstall": true,
        "installationFree": false,
        "pages": "$profile:main_pages",
        "abilities": [
          {
            "name": "DemoAbility",
            "srcEntry": "./ets/demoability/DemoAbility.ets",
            "description": "$string:DemoAbility_desc",
            "icon": "$media:layered_image",
            "label": "$string:DemoAbility_label",
            "startWindowIcon": "$media:startIcon",
            "startWindowBackground": "$color:start_window_background",
            "exported": true,
            "backgroundModes": [
              "audioRecording",
              "audioPlayback",
              "dataTransfer"
            ],
            "skills": [
              {
                "entities": [
                  "entity.system.home"
                ],
                "actions": [
                  "action.system.home"
                ]
              }
            ]
          }
        ],
        "requestPermissions": [
          {
            "name": "ohos.permission.INTERNET" // Permission to use the network
          },
          {
            "name": "ohos.permission.GET_WIFI_INFO" // Permission to get WLAN information
          },
          {
            "name": "ohos.permission.GET_NETWORK_INFO" // Permission to get the network status
          },
          {
            "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" // Demo permission for long-running tasks, used for background audio capture and playback
          },
          {
            "name": "ohos.permission.MICROPHONE",
            "reason": "$string:microphone",
            "usedScene": {
              "abilities": [
                "DemoAbility"
              ],
              "when": "inuse"
            }
          },
          {
            "name": "ohos.permission.CAMERA",
            "reason": "$string:camera",
            "usedScene": {
              "abilities": [
                "DemoAbility"
              ],
              "when": "inuse"
            }
          },
        ],
    
      }
    }

    Note: Real-Time Communication (RTC) services typically require long-running tasks to be enabled. This allows calls to be maintained even when the application is running in the background.

    To enable long-running tasks, refer to the backgroundModes and requestPermissions settings for long-running tasks in the preceding JSON file. You also need to call startContinuousTask on the call page to start the background task. For more information, see the background task section in the sample code on GitHub at https://github.com/aliyun/AliRTCSample/blob/master/Ohos/BasicVideoCall/Demo/src/main/ets/pages/Chat.ets, or see the official HarmonyOS documentation at https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/continuous-task#%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4.

      startContinuousTask() {
        let wantAgentInfo: wantAgent.WantAgentInfo = {
          // A list of actions to execute after the notification is tapped
          // Add the bundleName and abilityName of the application to launch
          wants: [
            {
              bundleName: "com.ding.rtc.demo",
              abilityName: "DemoAbility"
            }
          ],
          // Specifies that the action after tapping the notification message is to launch an ability
          actionType: wantAgent.OperationType.START_ABILITY,
          // A user-defined private value
          requestCode: 0,
          // Properties for executing the action after the notification is tapped
          actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
        };
    
        // Gets the WantAgent object using the getWantAgent method of the wantAgent module
        wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
          backgroundTaskManager.startBackgroundRunning(this.context,
            backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj).then(() => {
            // Execute the specific long-running task logic here, such as audio playback.
            console.info(`Succeeded in starting background running.`);
          }).catch((err: BusinessError) => {
            console.error(`Failed to start background running. Code is ${err.code}, message is ${err.message}`);
          });
        });
      }
    
      stopContinuousTask() {
        backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
          console.info(`Succeeded in stopping background running.`);
        }).catch((err: BusinessError) => {
          console.error(`Failed to stop background running. Code is ${err.code}, message is ${err.message}`);
        });
      }

Method 2: Manual integration

  1. Download and decompress the HarmonyOS SDK. For the download link, see SDK Download.

  2. Copy the `dingrtc.har` SDK file to the `libs` folder in the App module.

    {
      "name": "demo",
      "version": "1.0.0",
      "description": "Please describe the basic information.",
      "main": "",
      "author": "",
      "license": "",
      "dependencies": {
        "@dingrtc/dingrtc": "file:./src/main/libs/dingrtc.har",
      }
    }

What to do next

After you integrate the SDK, you can implement the basic features of Real-Time Communication. For more information, see HarmonyOS Basic Features.