Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SIP API-接收来电_Android_Broadcastreceiver_Asterisk_Sip_Voip - Fatal编程技术网

Android SIP API-接收来电

Android SIP API-接收来电,android,broadcastreceiver,asterisk,sip,voip,Android,Broadcastreceiver,Asterisk,Sip,Voip,我使用Android SIP API()创建了一个软电话应用程序,我已经成功注册到我的Asterisk服务器,我可以在另一个软电话上呼叫另一个对等方。我能听到两边的声音,一切都很完美 现在,我在接听另一部软电话时遇到问题。永远不会执行OnRing方法。在另一个软电话上,我收到状态铃声,但在我的应用程序中,从未收到该呼叫 我创建了IncomingCallReceiver类和WalkieTalkieActivity,但它们都没有完成它的工作(很可能我做错了什么,或者我没有启动其中的一些,或者调用或其

我使用Android SIP API()创建了一个软电话应用程序,我已经成功注册到我的Asterisk服务器,我可以在另一个软电话上呼叫另一个对等方。我能听到两边的声音,一切都很完美

现在,我在接听另一部软电话时遇到问题。永远不会执行OnRing方法。在另一个软电话上,我收到状态铃声,但在我的应用程序中,从未收到该呼叫

我创建了IncomingCallReceiver类和WalkieTalkieActivity,但它们都没有完成它的工作(很可能我做错了什么,或者我没有启动其中的一些,或者调用或其他…)

我一直在接电话。我也不清楚官方文件

如果有人在这里帮助我并指导我解决问题,我将非常感激

这是我的主要活动课:

class MainActivity: FlutterActivity() {
    var mSipProfile: SipProfile? = null
    var username: String? = "..." //I have it correct in my code
    var password: String? = "..." //I have it correct in my code
    var domain: String? = "..." //I have it correct in my code
    var builder: SipProfile.Builder? = SipProfile.Builder(username, domain).setPassword(password)
    private val CHANNEL = "samples.flutter.dev/registration"

    private val mSipManager: SipManager? by lazy(LazyThreadSafetyMode.NONE) {
        SipManager.newInstance(this)
    }

    override fun onStart() {
        super.onStart()
        println("SSSSSTTTTTTTTAAAAAAAAAAARRRRRRRRRRRRRTTTTTTTTTTTTTTTTTTT")
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        println("CCCCCCCCRRRRRREEEEEEEEEAAAAAAATTTTTTTTEEEEEEEEEEEEEEEEEEEEEE")
    }

    private var listenSipSession: SipSession.Listener = object : SipSession.Listener() {
        override fun onRinging(session: SipSession?, caller: SipProfile?, sessionDescription: String?) {
            super.onRinging(session, caller, sessionDescription)
            println("EVVVVVVVOOOOOOOOOOOOOOOO GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
        }
    }

    private var listener: SipAudioCall.Listener = object : SipAudioCall.Listener() {

        override fun onCalling(call: SipAudioCall?) {
            super.onCalling(call)
            println("111111 CALLING CALLING CALLING")
        }

        override fun onChanged(call: SipAudioCall?) {
            super.onChanged(call)
            println("CHANGED")
            println(call?.state)
        }

        override fun onError(call: SipAudioCall?, errorCode: Int, errorMessage: String?) {
            super.onError(call, errorCode, errorMessage)
            println("ERROR ERROR ERROR")
        }

        override fun onRingingBack(call: SipAudioCall?) {
            super.onRingingBack(call)
            println("RINGING BACK")
        }

        /*override fun onRinging(call: SipAudioCall, caller: SipProfile) {
            println("RINGING")
            try {
                call.answerCall(30)
            } catch (e: Exception) {
                println("ERROR 2: $e")
                e.printStackTrace()
            }
        }*/

        override fun onCallEstablished(call: SipAudioCall) {
            println("Call established")
            call.apply {
                startAudio()
                setSpeakerMode(false)
//                toggleMute()
            }
        }

        override fun onRinging(call: SipAudioCall?, caller: SipProfile?) {
            super.onRinging(call, caller)
            println("Ringing")
        }

        override fun onCallEnded(call: SipAudioCall) {
            println("Call ended " + call.state)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        closeLocalProfile()
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            // Note: this method is invoked on the main thread.

            mSipProfile = builder?.build()

            val intent = Intent("com.example.softphone.INCOMING_CALL")
            val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA)
            mSipManager?.open(mSipProfile, pendingIntent, null)

            if (call.method == "register") {
                val registrationStatus = register()
                result.success(registrationStatus)
            }

            if (call.method == "call") {
                call()
            }
        }
    }

    private fun closeLocalProfile() {
        try {
            mSipManager?.close(mSipProfile?.uriString)
            println("Local profile closed.")
        } catch (ee: Exception) {
            println("WalkieTalkieActivity/onDestroy. Failed to close local profile.")
        }
    }

    ///Method for making call
    private fun call(): String {
        var status = "Unknown"

        try {
            println("Making call")
            status = "In Call"
            val call: SipAudioCall? = mSipManager?.makeAudioCall(
                    mSipProfile?.uriString,
                    "...", //I have it correct in my code
                    listener,
                    30
            )
            println(call)
        } catch (e: SipException) {
            e.printStackTrace()
            println(e)
        }
        return status
    }

    ///Method for registering user
    private fun register(): String {
        var status = "Unknown"
        try {
            mSipManager?.setRegistrationListener(mSipProfile?.uriString, object : SipRegistrationListener {
                override fun onRegistering(localProfileUri: String) {
                    status = "Registering with SIP Server..."
                    println(status)
                }

                override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
                    status = "Ready"
                    println(status)
                }

                override fun onRegistrationFailed(
                        localProfileUri: String,
                        errorCode: Int,
                        errorMessage: String
                ) {
                    status = "Registration failed. Please check settings."
                    println(status)
                }
            })
        } catch (e: SipException) {
            e.printStackTrace()
            println(e)
        }
        return status
    }
}
class WalkieTalkieActivity : Activity(), OnTouchListener {
    var sipAddress: String? = null
    var manager: SipManager? = null
    var me: SipProfile? = null
    var call: SipAudioCall? = null
    
    lateinit var callReceiver: IncomingCallReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val filter = IntentFilter().apply {
            addAction("com.example.softphone.INCOMING_CALL")
        }
        callReceiver = IncomingCallReceiver()
        this.registerReceiver(callReceiver, filter)
    }

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        TODO("Not yet implemented")
    }
}
这是我的IncomingCallReceiver类:

class IncomingCallReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        var incomingCall: SipAudioCall? = null
        try {
            val listener: SipAudioCall.Listener = object : SipAudioCall.Listener() {
                override fun onRinging(call: SipAudioCall, caller: SipProfile) {
                    try {
                        call.answerCall(30)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
            val wtActivity: WalkieTalkieActivity = context as WalkieTalkieActivity
            incomingCall = wtActivity.manager?.takeAudioCall(intent, listener)
            incomingCall?.setListener(listener)
            incomingCall?.answerCall(30)
            incomingCall?.startAudio()
            incomingCall?.setSpeakerMode(true)
            if (incomingCall?.isMuted!!) {
                incomingCall?.toggleMute()
            }
        } catch (e: Exception) {
            incomingCall?.close()
        }
    }
}
这是我的Walkietalkie活动课:

class MainActivity: FlutterActivity() {
    var mSipProfile: SipProfile? = null
    var username: String? = "..." //I have it correct in my code
    var password: String? = "..." //I have it correct in my code
    var domain: String? = "..." //I have it correct in my code
    var builder: SipProfile.Builder? = SipProfile.Builder(username, domain).setPassword(password)
    private val CHANNEL = "samples.flutter.dev/registration"

    private val mSipManager: SipManager? by lazy(LazyThreadSafetyMode.NONE) {
        SipManager.newInstance(this)
    }

    override fun onStart() {
        super.onStart()
        println("SSSSSTTTTTTTTAAAAAAAAAAARRRRRRRRRRRRRTTTTTTTTTTTTTTTTTTT")
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        println("CCCCCCCCRRRRRREEEEEEEEEAAAAAAATTTTTTTTEEEEEEEEEEEEEEEEEEEEEE")
    }

    private var listenSipSession: SipSession.Listener = object : SipSession.Listener() {
        override fun onRinging(session: SipSession?, caller: SipProfile?, sessionDescription: String?) {
            super.onRinging(session, caller, sessionDescription)
            println("EVVVVVVVOOOOOOOOOOOOOOOO GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
        }
    }

    private var listener: SipAudioCall.Listener = object : SipAudioCall.Listener() {

        override fun onCalling(call: SipAudioCall?) {
            super.onCalling(call)
            println("111111 CALLING CALLING CALLING")
        }

        override fun onChanged(call: SipAudioCall?) {
            super.onChanged(call)
            println("CHANGED")
            println(call?.state)
        }

        override fun onError(call: SipAudioCall?, errorCode: Int, errorMessage: String?) {
            super.onError(call, errorCode, errorMessage)
            println("ERROR ERROR ERROR")
        }

        override fun onRingingBack(call: SipAudioCall?) {
            super.onRingingBack(call)
            println("RINGING BACK")
        }

        /*override fun onRinging(call: SipAudioCall, caller: SipProfile) {
            println("RINGING")
            try {
                call.answerCall(30)
            } catch (e: Exception) {
                println("ERROR 2: $e")
                e.printStackTrace()
            }
        }*/

        override fun onCallEstablished(call: SipAudioCall) {
            println("Call established")
            call.apply {
                startAudio()
                setSpeakerMode(false)
//                toggleMute()
            }
        }

        override fun onRinging(call: SipAudioCall?, caller: SipProfile?) {
            super.onRinging(call, caller)
            println("Ringing")
        }

        override fun onCallEnded(call: SipAudioCall) {
            println("Call ended " + call.state)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        closeLocalProfile()
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            // Note: this method is invoked on the main thread.

            mSipProfile = builder?.build()

            val intent = Intent("com.example.softphone.INCOMING_CALL")
            val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA)
            mSipManager?.open(mSipProfile, pendingIntent, null)

            if (call.method == "register") {
                val registrationStatus = register()
                result.success(registrationStatus)
            }

            if (call.method == "call") {
                call()
            }
        }
    }

    private fun closeLocalProfile() {
        try {
            mSipManager?.close(mSipProfile?.uriString)
            println("Local profile closed.")
        } catch (ee: Exception) {
            println("WalkieTalkieActivity/onDestroy. Failed to close local profile.")
        }
    }

    ///Method for making call
    private fun call(): String {
        var status = "Unknown"

        try {
            println("Making call")
            status = "In Call"
            val call: SipAudioCall? = mSipManager?.makeAudioCall(
                    mSipProfile?.uriString,
                    "...", //I have it correct in my code
                    listener,
                    30
            )
            println(call)
        } catch (e: SipException) {
            e.printStackTrace()
            println(e)
        }
        return status
    }

    ///Method for registering user
    private fun register(): String {
        var status = "Unknown"
        try {
            mSipManager?.setRegistrationListener(mSipProfile?.uriString, object : SipRegistrationListener {
                override fun onRegistering(localProfileUri: String) {
                    status = "Registering with SIP Server..."
                    println(status)
                }

                override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
                    status = "Ready"
                    println(status)
                }

                override fun onRegistrationFailed(
                        localProfileUri: String,
                        errorCode: Int,
                        errorMessage: String
                ) {
                    status = "Registration failed. Please check settings."
                    println(status)
                }
            })
        } catch (e: SipException) {
            e.printStackTrace()
            println(e)
        }
        return status
    }
}
class WalkieTalkieActivity : Activity(), OnTouchListener {
    var sipAddress: String? = null
    var manager: SipManager? = null
    var me: SipProfile? = null
    var call: SipAudioCall? = null
    
    lateinit var callReceiver: IncomingCallReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val filter = IntentFilter().apply {
            addAction("com.example.softphone.INCOMING_CALL")
        }
        callReceiver = IncomingCallReceiver()
        this.registerReceiver(callReceiver, filter)
    }

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        TODO("Not yet implemented")
    }
}
这是我的AndroidManifest文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.softphone">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->

    <uses-permission android:name="android.permission.USE_SIP" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <uses-feature android:name="android.software.sip.voip" android:required="true" />
    <uses-feature android:name="android.hardware.wifi" android:required="true" />
    <uses-feature android:name="android.hardware.microphone" android:required="true" />

    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="softphone"
        android:icon="@mipmap/ic_launcher">
        <receiver android:name=".IncomingCallReceiver" android:label="Call Receiver" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

提前谢谢大家