# Device Call Mobile WeChat
After the user performs theTo grant authorizationAfter that, the device can initiate an audio and video call to an authorized user, and the user opens a Mini Program in WeChat to answer it.
Hardware developers need to create Mini Program users OpenId, Mini Program ApplId, the association between hardware devices. Users in the mobile terminal authorization device can call.
If you want to get various events during a call, you can use the plug-in's
onVoipEvent
Interface.
# 1. Device-initiated call (Android Direct)
Before initiating a call, the user is generally required to select the user to be dialed and the type of call (audio/Video).
Depending on the business scenario, the process before initiating a call (such as selecting contacts and room types) can take place on a separate page in the Mini Program or in the Android app.
# 1.1 Mini Program page to enter the call page
Applies when the page before the user initiates the call (such as contact selection, etc.) is the Mini Program page.
When initiating a call, the device side needs to call the plug-in's initByCaller
Interface, and then jump to the plug-in's Initiate Call page.
const wmpfVoip = requirePlugin('wmpf-voip' ).default
try {
// 2.4.0 The following version roomId for groupId
const { roomId, isSuccess } = await wmpfVoip.initByCaller({
caller: {
id: 'sn', // equipment SN
// Does not support transmission Name, which is displayed when the authorizationdeviceName+modelId Corresponding Equipment Model
},
listener: {
// See also https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/Login.html Obtain
id: 'openId' // Receiving party user openId
name: 'xxxxxx', // The recipient's name is displayed.
},
roomType: 'video', // Room type. voice: Audio Roomvideo: Video Room
businessType: 1, // 1 Call your phone WeChat for your device
voipToken: 'xxxx', // Use of Equipment Certification SDK Registered device incoming DeviceToken, use the WMPF RegisterMiniProgramDevice Interface registered devices do not need to be passed in (plug-in 2.3.0 Support)
miniprogramState: 'formal', // Specifies the version of the Mini Program used by the recipient
})
if (isSuccess) {
// If the Mini Program launches directly to the plugin page, do not call the wx.redirectTo
wx.redirectTo({
url: wmpfVoip.CALL_PAGE_PATH,
// plug-in 2.3.9 Start support CALL_PAGE_PATH, Lower version please. 'plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin',
})
} else {
wx.showToast({
title: Call failure,
icon: 'error',
})
}
} catch (e) {
// The case of an incorrect parameter will be thrown by an exception
wx.showToast({
title: Call failure,
icon: 'error',
})
}
# 1.2 Android app goes directly to the call page
Applies when the page before the user initiates the call (such as contact selection, etc.) is the Android app page.
When initiating a call, the Android app is required to call WMPF [launchMiniProgram
](https://developers.weixin.qq.com/doc/oplatform /Miniprogram_Frame/api/cli/miniprogram/launchMiniProgram.html) Interface pull up Mini Program, path Call page directly using the plugin plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin
The path can be followed with custom parameters such as &a=1
In this case, the developer can directly in the Mini Program App.onShow
When calling initByCaller
♪ the plugin goes straight to the dialing state ♪You do not need to and can no longer jump to the plugin page。
It is strongly recommended that developers add anti-replay parameters to the startup parameters, for examplecallSeq=1703741306977
The parameter name can be customized, the value can be a timestamp or other unique ID。Mainly the following functions
- If the user clicks to re-enter the Mini Program, avoid repeating the call.
- The Mini Program cuts the background and the foreground (such as entering the Mini Program settings page, entering other native pages, user manual operation, etc.),
App.onShow
、callPageOnShow
Will be triggered multiple times to avoid repeated calls. - Identify the current start is will initiate a call to facilitate some logical judgment.
Developers are advised to use[Small preheatingability](./performance.md#_2- Android - WMPF- Mini Programs Warm Up (Suggested))Speed up the start of Mini programs.
const wmpfVoip = requirePlugin('wmpf-voip' ).default
// The preheat start parameter is assumed to be ?isPreLaunch=1
// Assume that the startup parameter when the call is initiated is ? callSeq = 1703741306977 &.. Other call parameters
function checkCallSeq(seq) {
if (!seq) return false
// Re-entering the Mini Program restarts the appleset, so the seq Persistent storage is required and variables cannot be left alone
const lastCallSeq = wx.getStorageSync ('WMPF_CALL_SEQ')
if (seq !== lastCallSeq ) {
// The example is only a simple comparison, and developers can add other criteria according to business needs, such as parstInt (seq) > parseInt (lastCallSeq )
wx.setStorageSync ('WMPF_CALL_SEQ', seq)
return true
} else {
// Repeated calls
return false
}
}
function call(options) {
try {
wmpfVoip
.initByCaller({
/* Parameters are available from the options Obtained in, omitted here */
})
.catch(e => {
wx.showToast({
title: Call failure,
icon: 'error',
})
})
} catch (e) {
// The case of an incorrect parameter will be thrown by an exception
wx.showToast({
title: Call failure,
icon: 'error',
})
}
}
/**
* call WMPF LaunchMiniProgram When,
* - If the Mini Program is not in the foreground (not started or in the background), it triggers App.onShow
* - If the Mini Program is at the front desk, ApageonShow Will not trigger, but will trigger the plug-in's callPageOnShow
*/
App({
onShow() {
const { query } = wmpfVoip.getPluginEnterOptions()
if (query.isPreLaunch) {
// Mini Program to warm up the scene, no need to deal with
return
}
if (!query.callSeq) {
// The current startup is not initiating a call, no processing is required
return
}
if(!checkCallSeq(query.callSeq)) {
// Repeat the call and ignore it.
return
}
call(query)
},
})
wmpfVoip.onVoipEvent(event => {
if (event.eventName === 'callPageOnShow') {
// When only the Mini Program is in the foreground, call the WMPF LaunchMiniProgram Trigger Mini Program reLaunch The situation of
const query = wmpfVoip.getPluginOnloadOptions()
if (checkCallSeq(query.callSeq)) {
// You can filter it here. App.onShow Cases dealt with
call(query)
}
}
})
Be careful
- The answer reminder pushed to the user will be sent after the call
initByCaller
It is directly issued by the WeChat background, and does not require the developer to call the interface of the service side to issue messages. roomType
Such parameters can be obtained by pulling up the Mini Program's path to hit the target query Passed to the Mini Program.- On the device, app When you pull up an Mini Program or receive a call slowly, refer to thePerformance and Experience Optimization Guide。
# 2. Device side to initiate a call (Linux Direct connection)
Please refer to the [Mini Program audio and video calls SDK (Linux)》](./voip-sdk.md#_5-4 - Call) 5.4.1 Initiate Call Part
# 3. Mobile phone WeChat end to answer the call
The user can receive theRing + VibrationStrong reminder notification, click the answer button, will start the Mini Program and directly into theVOIP callThe plugin page answers the call.
After the call is completed, the WeChat client will display the information of the call and theStopButton, the user clicks on theStopButton and then jump to the developer callsetVoipEndPagePath
Set the page. When the developer does not set it, it directly closes the Mini Program.
Developers canCustom answer page button, and end of call jump pageFor details, please refer toPlugin Documentation
# 4. End of Call Handling on Device Side (Android Direct)
After the end of the device call, the developer needs to handle the page jump or close the Mini Program on his own. There are generally the following ways:
- Jump to other pages after the end: Developers need to go through the plugin
setVoipEndPagePath
Interface sets the page where the call ends. The developer stays on the call log page when it is not set. - After the end of the Mini Program cut the background: the developer can listenplug-in endVoip or finishVoip event, Through WMPF Provided[Communication channel(Invoke Channel)](https://developers.weixin.qq.com/doc/oplatform /Miniprogram_Frame/invoke-channel.html)Notify the mobile app, use the[closeWxaApp](https://developers.weixin.qq.com/doc/oplatform /Miniprogram_Frame/api/cli/miniprogram/closeWxaApp.html) (keepRunning=true) The Mini Program cut background.
- Close the Mini Program when it is over: the developer can listenplug-in endVoip or finishVoip event, Call [wx.exitMiniProgram ]((wx.exitMiniProgram )) Close the Mini Program.