这是我的登录功能:
fun login(
userName: String, password: String, serverHost: String?, serverPort: Int, transportType: Int, displayName: String, authName: String, userDomain: String,
srtpPolicy: Int, localP: Int?, loginHandler: SIPLoginHandler?) {
App.retryRegistrationOnFailure = true
isLoginInProgress = true
this.userName = null
broadcastAuthUpdate()
this.loginHandler = loginHandler
val rm = Random()
var localPort = 5060 + rm.nextInt(60000)
if (localP != null && localP != -1) {
localPort = localP
}
val prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this@SIPManager)
val editor = prefs.edit()
editor.putInt(getString(R.string.keySIPLocalPort), localPort)
editor.apply()
var result = 0
try {
portSipSdk!!.DeleteCallManager()
} catch (e: Exception) {
FL.e(TAG, "handleIncomingCall Error trying to delete the call manager. Was not registered? Error: " + e.message)
}
portSipSdk!!.CreateCallManager(this)
portSipSdk!!.setOnPortSIPEvent(this)
val dataPath = getExternalFilesDir(null)!!.absolutePath
result = portSipSdk!!
.initialize(transportType, "0.0.0.0", localPort, PortSipEnumDefine.ENUM_LOG_LEVEL_NONE, dataPath, MAX_SESSIONS, agentName, 0, 0, dataPath, "",
false, null)
if (result != PortSipErrorcode.ECoreErrorNone) {
FL.d(TAG, "handleIncomingCall Error initializing, code = $result")
val message = "Error initializing, code = $result"
portSipSdk!!.DeleteCallManager()
sessions.clear()
isLoginInProgress = false
loginHandler?.onLoginFailure(message)
broadcastAuthUpdate()
return
}
val stunServerPort = 0
val stunServer = ""
result = portSipSdk!!.setLicenseKey(licenceKey)
if (result != PortSipErrorcode.ECoreErrorNone) {
FL.d(TAG, "handleIncomingCall Failed to set licence, code = $result")
}
if (result == PortSipErrorcode.ECoreWrongLicenseKey) {
val message = "Failed to set licence, code = $result"
portSipSdk!!.DeleteCallManager()
sessions.clear()
isLoginInProgress = false
loginHandler?.onLoginFailure(message)
broadcastAuthUpdate()
return
}
result = portSipSdk!!.setUser(userName, displayName, authName, password, userDomain, serverHost, serverPort, stunServer, stunServerPort, "", 0)
if (result != PortSipErrorcode.ECoreErrorNone) {
FL.d(TAG, "handleIncomingCall setUser failure ErrorCode = $result")
val message = "setUser failure ErrorCode = $result"
portSipSdk!!.DeleteCallManager()
sessions.clear()
isLoginInProgress = false
loginHandler?.onLoginFailure(message)
broadcastAuthUpdate()
return
}
portSipSdk!!.setVideoDeviceId(1)
portSipSdk!!.setSrtpPolicy(srtpPolicy)
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
ConfigPreferences(this, preferences, portSipSdk!!)
portSipSdk!!.enable3GppTags(false)
initialiseKeepAlive()
portSipSdk!!.setInstanceId(instanceId)
FL.d(TAG, "*******************************DATA is: nUN $userName, nPASS $password, nSERVERHOST $serverHost, nSERVERPORT $serverPort ,nTransortTYPE $transportType, nDisplay $displayName, nAUTH $authName, nuserDpm $userDomain, nsrtp $srtpPolicy, nhandler $loginHandler, nlocalPort $localPort, nstunserver $stunServer, nstunport $stunServerPort")
if (!portSIPRegisterRegisterServer(loginHandler))
//if it fails, I return, not setting the username
{
FL.d(TAG, "*******************************FAILED TO REREGISTER SERVERL ")
return
}
this.userName = userName
}
portSIPREgisterRegisterServer函数为:
private fun portSIPRegisterRegisterServer(loginHandler: SIPLoginHandler?): Boolean {
val result: Int
result = portSipSdk!!.registerServer(3600, 3)
FL.d("handleIncoming portSIPRegisterRegisterServer Called")
if (result != PortSipErrorcode.ECoreErrorNone) {
val message = "handleIncomingCall registerServer failure ErrorCode =$result"
portSipSdk!!.unRegisterServer()
portSipSdk!!.DeleteCallManager()
sessions.clear()
isLoginInProgress = false
loginHandler?.onLoginFailure(message)
broadcastAuthUpdate()
FL.d(TAG, "*******************************FAILED portSIPRegisterRegisterServer" + result)
return false
}
registrationTimePassed = System.currentTimeMillis()
registrationType = SIPREGISTRATION.LOGIN
return true
}
我用这个参数调用:
SIPManager.instance!!
.login(userName!!, password!!, hostName, portNumber, transportType, "", "", "", srtpPolicy, localPortNumber, object : SIPLoginHandler {
override fun onLoginSuccess(message: String) {
FL.d(TAG, "handleIncomingCall Login TRIED and SUCCESS: $message")
sipLoginHandler.onLoginSuccess(message)
}
override fun onLoginFailure(errorMessage: String) {
FL.d(TAG, "handleIncomingCall Login TRIED and FAILED: $errorMessage")
SIPManager.instance!!.loginToXelionServer(sipLoginHandler)
}
})
这是我发送到登录的数据:
DATA is:
UN <myuser>,
PASS <mypass>,
SERVERHOST <myserver>,
SERVERPORT 5060 ,
TransortTYPE 0,
Display ,
AUTH ,
userDpm ,
srtp 0,
handler com.xelion.android.pushmessages.IncomingPhoneCallPushHandler$tryToReRegisterBecauseRefreshFailed$1@cb36a8d,
localPort 29292,
stunserver ,
stunport 0
通常我会得到这个回复:
handleIncomingCall Login TRIED and SUCCESS: Status: none code: 200
但当我尝试Pixel 3时,没有连接到PC(没有调试(,屏幕关闭超过4-5分钟,它会调用寄存器功能,我在日志中看到这一点:
FL.d("handleIncoming portSIPRegisterRegisterServer Called")
但我没有收到注册处的回复,无论是成功还是失败。
知道怎么解决这个问题吗?我的清单上有这个:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
我觉得它忽略了寄存器调用,而屏幕是关闭的?如果我打开屏幕,我会得到登录访问
我注意到它只是等待,只有在我收到未接来电通知时才真正拨打网络电话。因此,我从FCM创建了一个基本通知,让用户知道VOIP服务正在注册。在用户有视觉输入后,注册voip服务可以完美地工作,它在大约200-300ms内收到回复。