1
votes

I have been developing an app that ables the user to call and receive calls using SIP inspired the by the SIPDemo, in which up until i unsinstalled the app from my phone it worked fine, it did the registration, showed the message "Ready" and then proceeded to do the call treatment. Now it does not enter the Sipregistrationlistener and displays the error "Error when trying to close manager 1 SipException:Failed to create SipSession; network unavailable?".

From what I understand I suspect that the problem is due to the fact that the previous SIP account is still linked and therefore automatically opened in the app, not letting any registration, as stated in the solution to this post "Android Native SIP Stack not registering client", but I have no clue on how to deal with this, introducing the closelocalprofile function on the onDestroy, onPause did no effect. Besides up until recently it did showed the messages "SipManager is ready for calls" and that it was opened, but now it doesn't despite not changing anything in the code so the problem might not be necessarily this.

In terms of printing the following messages are shown: -no message associated to the status are shown; -the log shows "Creating Manager" & "Building a new profile";

Furthermore i already have the permissions and the manifest coded to support SIP comunnications.

I know by now that this stack is not the best but I would like not to abandon this project so any help or tips would be much appreciated. In last case, in preparation if no solution/progress is found, if any of you could also give recomendations to an alternative stack that is similar that would be also appreciated.

Here is the code:

public SipManager sipManager = null;//SIPMANAGER
public SipProfile sipProfile = null;//SIPPROFILE
public SipAudioCall call = null;//SIPAUDIOCALL
public IncomingCallReceiver callReceiver;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    Permissions();
    Supported();

    initManager();

    MakeCallButton.setOnClickListener(new View.OnClickListener() /onclick event
    {
        @Override
        public void onClick(View view)
        {
           initCall();
        }
    });
    EndCallButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            try
            {
                call.endCall();
            }
            catch (SipException e)
            {
                status("Error when trying to end call. " + e);
            }
        }
    });
}

public void initManager()//SIPMANAGER
{
    if(sipManager == null)
    {
        sipManager = SipManager.newInstance(this); //Creates a manager instance. Returns null if SIP API is not supported
        Log.d("Manager", "Creating Manager");
    }

    initLocalProfile();
}
public void initLocalProfile()
{
    if (sipManager == null)
    {
        Log.d("Manager", "There is no manager");
        return;
    }
    if (sipProfile != null)
    {
        Log.d("Profile", "There is already a profile 1");
        closeLocalProfile();
    }

   //localprofiledata
   String user = "x";
   String domain = "xxx";
   String pass = "zzzz";

   try
   {
       Log.d("Profile", "Building a new profile");
       SipProfile.Builder builder = new SipProfile.Builder(user, domain); //user of the SIP account & the SIP server domain
       builder.setPassword(pass);//Sets the password of the SIP account
       builder.setOutboundProxy(domain);//Sets the outbound proxy of the SIP server
       builder.setPort(5060);//port number
       builder.setProtocol("UDP");
       builder.setAutoRegistration(false);

       sipProfile = builder.build();//Builds and returns the SIP profile object.

       Intent sipIntent = new Intent();//intent for the calls
       sipIntent.setAction("android.Login.INCOMING_CALL");
       PendingIntent pi = PendingIntent.getBroadcast(this, 0, sipIntent, Intent.FILL_IN_DATA);
       sipManager.open(sipProfile, pi, null);//Opens the profile for making calls and/or receiving generic SIP calls

       //Sets the listener to listen to registration events. No effect if the profile has not been opened to receive call

       sipManager.setRegistrationListener(sipProfile.getUriString(), new SipRegistrationListener()
        {
           public void onRegistering(String localProfileUri)
           {
               //Called when a registration request is sent
               status("Registering");
           }
           public void onRegistrationDone(String localProfileUri, long expiryTime)
           {
               //Called when the registration succeeded
                status("Ready");
           }
           public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage)
           {
               //Called when the registration failed
                status("Registration Failed " + localProfileUri + errorCode + errorMessage );
           }
       });
       if (sipManager.isRegistered(sipProfile.getUriString()))
       {
           Log.d("Profile","SipManager is ready for calls");
       }
       if (sipManager.isOpened(sipProfile.getUriString()))
       {
           Log.d("Profile","SipManager is open");
       }
   }
   catch (ParseException pe)
   {
        status("Connection Error");
   }
   catch (SipException sipe)//if calling the SIP service results in an error
   {
       status("Error with SIP " + sipe);
   }
   catch (SecurityException se)
   {
       status("Error with security" + se);
   }
   catch (RuntimeException re)
   {
       status("Error with runtime" + re);
   }
   catch (Exception e)
   {
       status("Error" + e);
   }
}
public void closeLocalProfile()
{
    if (sipManager == null)
    {
        Log.d("Manager", "There is no manager 1");
        return;
    }
    try
    {
        if (sipProfile != null)
        {
            Log.d("Profile", "There is already a profile 2");
            sipManager.close(sipProfile.getUriString()); //Closes the specified profile to not make/receive calls
        }
    }
    catch (SipException se)//if calling the SIP service results in an error
    {
        status("Error while closing SIP" + se);
    }
}

public void initCall()
{
    callstatus("Adress: " + sipAddress);

    try
    {
        SipAudioCall.Listener listener = new SipAudioCall.Listener() //Listener for events relating to a SIP call, such as when a call is being recieved ("on ringing") or a call is outgoing ("on calling")
        {
            @Override
            public void onCalling(SipAudioCall call)
            {
                Log.d("initCall", "Initiating session! " + sipAddress);
            }
            @Override
            public void onCallEstablished(SipAudioCall call)
            {
                Log.d("initCall", "Call started! " + sipAddress);
                call.startAudio();//Starts the audio for the established call. This method should be called after onCallEstablished(SipAudioCall) is called
                Enter();
            }
            @Override
            public void onRinging(SipAudioCall call, SipProfile caller)
            {
                Log.d("initCall", "Ringing " + sipAddress);
            }
            @Override
            public void onRingingBack(SipAudioCall call) //Called when a RINGING response is received for the INVITE request sent
            {
                Log.d("initCall", "Ringing back " + sipAddress);
            }
            @Override
            public void onCallBusy(SipAudioCall call)
            {
                Log.d("initCall", "Call busy " + sipAddress);
            }
            @Override
            public void onCallEnded(SipAudioCall call)
            {
                    Log.d("initCall", "Call Over ");
                    call.close();
            }
            @Override
            public void onError(SipAudioCall call, int errorCode, String errorMessage)
            {
                //super.onError(call, errorCode, errorMessage);
                Log.d("initCall", "Error! " + errorMessage + errorCode);
            }
        };
        //the call object that carries out the audio call
        call = sipManager.makeAudioCall(sipProfile.getUriString(), sipAddress, listener, 30);
    }
    catch (Exception e)
    {
        status("Error when trying to close manager 1. " + e);
        if (sipProfile != null)
        {
            try
            {
                sipManager.close(sipProfile.getUriString());
            }
            catch (Exception ee)
            {
                status("Error when trying to close manager 2. " + ee);
            }
        }
        if (call != null)
        {
            call.close();
        }
    }
}

public void status(final String status)//status about the program
{
    StatusTextView = (TextView) findViewById(R.id.StatusTextView);
    StatusTextView.setText(status);
}

public void callstatus(final String callstatus)//status about the call
{
    CallTextView = (TextView) findViewById(R.id.CallTextView);
    CallTextView.setText(callstatus);
}

Thanks for your time & attention.

1

1 Answers

1
votes

Update

Managed to circumnavigate these previous errors by a combination of rebooting the device after reinstalling the app and resending the code from Android Studio to the phone. My suspicions about the source of the error is, as I stated above, due to the bugs that this stack has, nevertheless it now works and I'm satisfied with the finished product.

Despite not getting any response here, I hope that this thread can be of help to somebody.

Thanks for your time & attention.