Device-to-Phone WeChat Calls

View Markdown

Last edited on 2026-11-12

After a user grants authorization to the device, the device can initiate audio and video calls to the authorized user, who then answers by opening Mini Program within WeChat.

Hardware developers must establish a connection between the Mini Program user’s openId, Mini Program appId, and the hardware device. The device can make calls only after the user has authorized it on their mobile phone.

To capture various events during the call process, you can use the plugin’s onVoipEvent interface.

1. Initiating a Call from the Device Side (Direct Connection on Android)

Before initiating a call, users generally need to select the recipient and the call type (audio/video).

Depending on the specific business scenario, the steps taken prior to calling—such as selecting contacts and room types—can be carried out on another page within Mini Program or within the Android app.

1.1 Transitioning from the Mini Program Page to the Call Page

This applies when the page where the user initiates a call (such as contact selection) is the Mini Program page.

When initiating a call, the device side needs to call the plugin’s initByCaller interface on the previous page, and then navigate to the plugin’s call initiation page.

const wmpfVoip = requirePlugin('wmpf-voip').default

try {
 // For versions prior to 2.4.0, roomId is equivalent to groupId
 const {roomId, isSuccess} = await wmpfVoip.initByCaller({
 caller: {
 id: 'sn', // Device SN
 // The "name" field is not supported; what is displayed is "deviceName" plus the device model corresponding to "modelId" during authorization.
},
 listener: {
 // Refer to the documentation below for details.
 id: 'openId' // The openId of the recipient user
 name: 'xxxxxx', // Name of the recipient, shown only for display.
},
 roomType: 'video', // Room type. voice: audio room; video: video room
 businessType: 1, // 1 indicates a device calling a WeChat mobile number
 voipToken: 'xxxxxxxxxx', // Devices registered using the device authentication SDK should provide the deviceToken; devices registered via the WMPF RegisterMiniProgramDevice interface do not need to provide this (supported starting from plugin version 2.3.0).
 miniprogramState: 'formal', // Specifies the Mini Program version used by the recipient.
})

 if (isSuccess) {
 // If the Mini Program page launches directly into the plugin page, there is no need to call wx.redirectTo.
 wx.redirectTo({
 url: wmpfVoip.CALL_PAGE_PATH,
 // CALL_PAGE_PATH has been supported since plugin version 2.3.9; for older versions, use 'plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin'.
})
} else {
 wx.showToast({
 title: 'Call failed',
 icon: 'error',
})
}
} catch (e) {
 // Parameter errors will be thrown as exceptions.
 wx.showToast({
 title: 'Call failed',
 icon: 'error',
})
}

Mini Program Login

1.2 Direct Entry to the Call Page from Android Apps

This applies when the page where the user initiates a call (such as contact selection) is an Android app page.

When initiating a call, the Android app needs to call the WMPF launchMiniProgram interface to launch Mini Program, using the plugin’s call page path plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin directly. Custom parameters can be appended to this path, such as &a=1.

In this case, developers can directly call initByCaller within Mini Program ’s App.onShow method. The plugin will immediately enter call mode without needing or being able to navigate to the plugin’s main page again.

It is highly recommended that developers include anti-replay parameters in the launch parameters, for example callSeq=1703741306977. The parameter name can be customized, and its value can be a timestamp or another unique identifier. These parameters serve the following purposes:

  • Preventing duplicate calls if the user clicks to re-enter Mini Program.
  • Handling scenarios where Mini Program switches from background to foreground (such as when entering the settings page, another native page, or due to user actions), as App.onShow and callPageOnShow will be triggered multiple times, which helps avoid redundant calls.
  • Indicating that a call is about to be initiated, facilitating certain logical judgments.

Developers are advised to use the “ Mini Program Preheating” capability) to speed up the startup of Mini Program.

const wmpfVoip = requirePlugin('wmpf-voip').default

// Assume the preheating launch parameter is?isPreLaunch=1
// Assume the call initiation parameter is?callSeq=1703741306977&..other parameters used for calling

function checkCallSeq(seq) {
 if (!seq) return false
 // Re-entering Mini Program will restart it, so the seq value must be stored persistently rather than only in a variable
 const lastCallSeq = wx.getStorageSync('WMPF_CALL_SEQ')
 if (seq!== lastCallSeq) {
 // This is a simple comparison example; developers can add additional conditions based on business needs, such as parstInt(seq) > parseInt(lastCallSeq)
 wx.setStorageSync('WMPF_CALL_SEQ', seq)
 return true
} else {
 // A duplicate call attempt
 return false
}
}

function call(options) {
 try {
 wmpfVoip
.initByCaller({
 /* Parameters can be retrieved from options; details are omitted here */
})
.catch(e => {
 wx.showToast({
 title: 'Call failed',
 icon: 'error',
})
})
} catch (e) {
 // Parameter errors will be thrown as exceptions
 wx.showToast({
 title: 'Call failed',
 icon: 'error',
})
}
}

/**
 * When calling WMPF LaunchMiniProgram,
 * - If Mini Program is not in the foreground (not launched or in background), App.onShow will be triggered.
 * - If Mini Program is already in the foreground, App.onShow will not trigger, but the plugin’s callPageOnShow will be triggered.
 */

App({
 onShow() {
 const {query} = wmpfVoip.getPluginEnterOptions()
 if (query.isPreLaunch) {
 // In the Mini Program preheating scenario, no action is required
 return
}

 if (!query.callSeq) {
 // This launch is not intended for initiating a call, so no action is needed
 return
}

 if(!checkCallSeq(query.callSeq)) {
 // It’s a duplicate call attempt, so ignore it
 return
}

 call(query)
},
})

wmpfVoip.onVoipEvent(event => {
 if (event.eventName === 'callPageOnShow') {
 // Only handle cases where WMPF LaunchMiniProgram is called while Mini Program is in the foreground and triggers a reLaunch of Mini Program 
 const query = wmpfVoip.getPluginOnloadOptions()
 if (checkCallSeq(query.callSeq)) {
 // Here, cases already handled by App.onShow can be filtered out
 call(query)
}
}
})

Notes

  • The call acceptance reminder pushed to the user will be delivered directly by WeChat’s backend after calling initByCaller; developers do not need to use additional interfaces to send messages to the server.
  • Parameters such as roomType can be passed to Mini Program through the query in the path used to launch Mini Program.
  • If the device experiences delays in launching Mini Program via the app or answering calls, please refer to the Performance and Experience Optimization Guide.

2. Initiating Calls from the Device Side (Direct Linux Connection)

Please refer to the Initiating Calls section in 5.4.1 of 《 Mini Program Audio/Video Call SDK (Linux)》.

3. Answering Calls via WeChat on Mobile Devices

Users on mobile devices receive a strong notification in the form of both ringing sounds and vibrations. After tapping the answer button, Mini Program is launched, and the user is directly directed to the “VOIP Call” plugin page to answer the call.

Once the call is completed, the WeChat client displays information about the call along with a “Close” button. Tapping this button redirects the user to the page specified by the developer using the setVoipEndPagePath method. If no such page is set by the developer, Mini Program closes directly.

Developers can customize the buttons on the answer page as well as the redirect page after the call ends. For more details, please refer to the Plugin Documentation.

4. Handling Call End on the Device Side (Android Direct Connection)

After a call ends on the device side, developers need to handle page navigation or closing of Mini Program on their own. There are generally the following approaches:

  • Navigating to another page after completion: Developers must use the plugin’s setVoipEndPagePath interface to specify the page to navigate to upon call end. If no setting is made, the app will remain on the call history page.
  • Putting Mini Program in the background after completion: Developers can listen for the plugin endVoip or finishVoip events and use the communication channel (Invoke Channel) provided by WMPF to notify the mobile application, which can then use closeWxaApp with keepRunning=true to move Mini Program to the background.
  • Closing Mini Program after completion: Developers can listen for the plugin endVoip or finishVoip events and call [wx.exitMiniProgram] to close Mini Program.