Failed to create SipSession; network unavailable a

2019-02-15 17:14发布

问题:

I am developing a simple application that allows user to initiate call using sip protocol. The problem is inability to create SipSession in some cases (e.g. deleting application with active sip-session, and installing it again).

In this case, I am getting error:

android.net.sip.SipException: Failed to create SipSession; network unavailable?

And it works only after physical device reboot.

My Sip class:

public class SipDataManager {

private Context context;
private SipManager sipManager;
private SipProfile sipProfile;
private SipSession sipSession;
private UserProfile userProfile;

public SipDataManager(Context context, UserProfile userProfile) {
    this.context = context;
    this.userProfile = userProfile;
}

public void initialize() throws SipException {
    Log.d("mylog", "initialize manager");
    if (sipManager == null) {
        Log.d("mylog", "sip manager is not null");
        sipManager = SipManager.newInstance(context);
    }
    initializeProfile();
}

private void initializeProfile() throws SipException {
    if (sipManager == null)
        return;
    if (sipProfile != null) {
        close();
    }
    sipProfile = userProfile.build();
    Intent intent = new Intent();
    intent.setAction("ru.tenet.sipclient.INCOMING_CALL");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, Intent.FILL_IN_DATA);
    sipManager.open(sipProfile, pendingIntent, null);
    sipSession = sipManager.createSipSession(sipProfile, new MySipSessionListener());
    sipManager.setRegistrationListener(sipProfile.getUriString(), new MySipRegistrationListener());
}

public void close() {
    try {
        if (sipProfile != null) {
            sipManager.close(sipProfile.getUriString());
        }
    } catch (Exception ee) {
        Log.e("mylog", "Failed to close local profile.", ee);
    }
}
//getters and setters

I tried to remove this

sipSession = sipManager.createSipSession(sipProfile, new MySipSessionListener());

In this case I will not get any Exception, but SipRegistrationListener callbacks will not getting call.

And only reboot helps..

Someone faced this problem? I didn't find any correct solution for this..

SOLUTION The problem is in my device or firmware (Samsung Galaxy s4, Android 5.0.1 official, but some integrated apps were deleted with root). Checked on Samsung Galaxy s4 with Android 4.3.1 with cyanogen - no problem.

回答1:

Create your SipSession before attempting to open the profile for sending or receiving calls.

    sipSession = sipManager.createSipSession(sipProfile, new MySipSessionListener());
    sipManager.open(sipProfile, pendingIntent, null);


回答2:

I have the same problem and have not been able to programmatically correct it. Current solution is to add a delayed handler of 10 seconds to check if the OnRegistrationDone and OnRegistering events have fired during that time. The following code is placed immediately before the manager.setRegistrationListener method in the initializeLocalProfile function. When this error condition is detected, the SIP preferences activity is launched and the user notified with SIP Library Error, requesting them to restart the device. The same handler also detects problems connecting to the defined SIP server with the Server Not Found notification to the user. Both of these error conditions normally trigger the "registration timed out" error, but you cannot differentiate the two error types in that event, and it takes 30 seconds to time out.

            if (!manager.isOpened(me.getUriString())) {
                if (me_listener == null) {
                    manager.open(me, pi, null);
                } else {
                    manager.open(me, pi, me_listener);
                }
            }
            // This listener must be added AFTER manager.open is called,
            // Otherwise the methods aren't guaranteed to fire.

            if (me_listener == null) {
                bConnecting = true;
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (sentConnecting == false && bLocalOpen == false) { //never connected or connecting
                            updateStatus("SIP Library Error");
                            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.remove("namePref");
                            editor.remove("domainPref");
                            editor.putString("statusPref", "SIP Library Error - Please Restart Device and Try Again");
                            editor.commit();
                            updatePreferences();
                            return;
                        } else if (bConnecting == true && bLocalOpen == false) { //never connected
                            updateStatus("Server Not Found");
                            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.remove("namePref");
                            editor.remove("domainPref");
                            editor.putString("statusPref", "Server Not Found, Please Edit and Try Again");
                            editor.commit();
                            updatePreferences();
                            return;
                        }
                    }
                }, 10000);


标签: android sip