# Device calling mobile phone WeChat
After is authorized , the device can initiate a voice and video call to an authorized user, who opens WeChat within WeChat to answer.
Hardware developers need to establish Weixin Mini Program user openId, Mini Program appId, hardware device association.The device can make a call only after the user has authorized it on the phone.
If you want to get various events during the call, you can use the plug-in
onVoipEventInterface.
# 1. Launch a call on the device (Android directly)
Before initiating a call, the user is generally required to select the user to whom to call and the type of call (audio / video).
Depending on the business scenario, the process before initiating the call (such as selecting the connection person and room type) can be done in a separate page on Weixin Mini Program or in the Android app.
# 1.1 Weixin Mini Program Page to call page
Applies when the page before the user initiates the call (such as Associate Selection, etc.) is the Weixin Mini Program page.
To initiate a call, the device needs to call the plug-in's initByCaller The interface, then jump to the plugin's initiated call page.
const wmpfVoip = requirePlugin('wmpf-voip').default
try {
// 2.4.0 以下版本 roomId 为 groupId
const { roomId, isSuccess } = await wmpfVoip.initByCaller({
caller: {
id: 'sn', // 设备 SN
// 不支持传 name,显示的是授权时「deviceName」+「modelId 对应设备型号」
},
listener: {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 获取
id: 'openId' // 接听方 用户 openId
name: 'xxxxxx', // 接听方名字,仅显示用
},
roomType: 'video', // 房间类型。voice: 音频房间;video: 视频房间
businessType: 1, // 1 为设备呼叫手机微信
voipToken: 'xxxxxxxxxx', // 使用设备认证 SDK 注册的设备传入 deviceToken,使用 WMPF RegisterMiniProgramDevice 接口注册的设备无需传入(插件 2.3.0 支持)
miniprogramState: 'formal', // 指定接听方使用的小程序版本
})
if (isSuccess) {
// 如果小程序启动直接进入插件页面,则不要调用 wx.redirectTo
wx.redirectTo({
url: wmpfVoip.CALL_PAGE_PATH,
// 插件 2.3.9 开始支持 CALL_PAGE_PATH, 低版本请传入 'plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin',
})
} else {
wx.showToast({
title: '呼叫失败',
icon: 'error',
})
}
} catch (e) {
// 参数错误的情况会通过异常抛出
wx.showToast({
title: '呼叫失败',
icon: 'error',
})
}
# 1.2 The Android app goes directly to the call page
This applies when the page before a user initiates a call (such as contact selection, etc.) is an Android app page.
Android app calls WMPF launchMiniProgram Interface pulls up Weixin Mini Program, path directly using the plug-in to dial the pageplugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin。Path can be followed by custom parameters such as& a = 1
In this case, the developer can directly call Weixin Mini ProgramApageonShow initByCaller ,The plug-in will go directly to the dialing state, does not need to and cannot jump to the plug-in page .
Developers are strongly encouraged to add an anti-replay parameter to the startup parameter, such ascallSeq = 1703741306977.The parameter name can be customized, and the value can be a timestamp or other unique ID. It mainly functions as follows
- If the user clicks Reenter Weixin Mini Program, avoid making repeated calls.
- Weixin Mini Program cut the background and cut the foreground (such as entering the Mini Program settings page, entering other native pages, user manual operation, etc.),
ApageonShow、callPageOnShowwill trigger multiple times to avoid repeated calls. - Identifying the current start will initiate a call to facilitate some logical judgment.
Developers are advised to use the "Weixin Mini Program preheat" capability to speed up the startup of Mini Programs.
const wmpfVoip = requirePlugin('wmpf-voip').default
// Suppose the preheat start parameter is?isPreLaunch=1
// Suppose that the startup parameter when initiating the call is?CallSeq = 1703741306977 &.. Other call parameters
function checkCallSeq(seq) {
if (!seq) return false
// 重新进入小程序会重启小程序,因此 seq 需要持久化存储,不能仅存变量
const lastCallSeq = wx.getStorageSync('WMPF_CALL_SEQ')
if (seq !== lastCallSeq) {
// 示例仅做简单的比较,开发者可以根据业务需要增加其他判断条件,如 parstInt(seq) > parseInt(lastCallSeq)
wx.setStorageSync('WMPF_CALL_SEQ', seq)
return true
} else {
// 重复发起的通话
return false
}
}
function call(options) {
try {
wmpfVoip
.initByCaller({
/* 参数可从 options 中获取,此处省略 */
})
.catch(e => {
wx.showToast({
title: '呼叫失败',
icon: 'error',
})
})
} catch (e) {
// 参数错误的情况会通过异常抛出
wx.showToast({
title: '呼叫失败',
icon: 'error',
})
}
}
/**
* 调用 WMPF LaunchMiniProgram 时,
* - 如果小程序不在前台(未启动或在后台),会触发 App.onShow
* - 如果小程序在前台,App.onShow 不会触发,但会触发插件的 callPageOnShow
*/
App({
onShow() {
const { query } = wmpfVoip.getPluginEnterOptions()
if (query.isPreLaunch) {
// 小程序预热场景,无需处理
return
}
if (!query.callSeq) {
// 当前启动不是发起通话,无需处理
return
}
if(!checkCallSeq(query.callSeq)) {
// 重复发起通话,直接忽略
return
}
call(query)
},
})
wmpfVoip.onVoipEvent(event => {
if (event.eventName === 'callPageOnShow') {
// 仅处理小程序在前台时,调用 WMPF LaunchMiniProgram 触发小程序 reLaunch 的情况
const query = wmpfVoip.getPluginOnloadOptions()
if (checkCallSeq(query.callSeq)) {
// 此处可以过滤掉已被 App.onShow 处理的情况
call(query)
}
}
})
Be careful
- Pushed reminders to the user are sent directly by the WeChat background after calling
initByCaller, without the need for the developer to call the server-side messaging interface additionally. Parameters such as roomTypecan be passed to the Mini Program by pulling up the query in the path of Weixin Mini Program.- On your device, when the app pulls up Weixin Mini Program or receives a call slowly, refer to the Performance and Experience Optimization Guide .
# 2. Device-side calls (Linux direct connection)
Please refer to < Weixin Mini Program Audio and Video Calling SDK (Linux) > 5.4.1 Initiating Calls
# 3. Mobile phone WeChat side to answer calls
On the phone, users can receive a strong reminder of "ringing + vibrating" and, after clicking the answer button, Weixin Mini Program will be activated and the VOIP call plugin page will be directed to answer the call.
After completing the call, WeChat will display the message of the call and the "Close" button in the passenger terminal. The user clicks the "Close" button and then jumps to the developer to call WeChat setVoipEndPagePath Set the page.The developer closes Weixin Mini Program directly if it is not set.
Developers can customize the answer page button and the end of call jump page.Please refer to plugin documentation for details
# 4. Processing the end of the call on the device (Android directly)
After the end of the device call, the developer needs to handle the page jump or close Weixin Mini Program.There are generally the following ways:
- After the end, jump to other pages: Developers need to pass the plugin
setVoipEndPagePathThe interface sets the page where the call ends jumping. When the developer does not set this up, it remains on the call log page. - After the end Weixin Mini Program cut background: developers can listen plug-in endVoip or finishVoip event , Invoke Channel](https://developers.weixin.qq.com/doc/oplatform/Miniprogram_Frame/invoke-channel.html) notification Mobile App, using [ closeWxaApp (keepRunning = true) Cut the Mini Program into the background.
- Close Weixin Mini Program after the end: developers can listen to plug-in endVoip or finishVoip events and call wx.exitMiniProgram to close the Mini Program.