Android Tutorial Android Sip / Voip tutorial

Discussion in 'Tutorials & Examples' started by Erel, Dec 7, 2011.

  1. Erel

    Erel Administrator Staff Member Licensed User

    With the new Sip library you can make audio calls using Voip (Voice over IP) services.
    Sip features were added in Android 2.3 (API level 9). Note that not all devices above Android 2.3 support Sip features.

    In order to use this library you will need to set android.jar in Tools > Configure Paths to platform-9 or above.

    The library includes two types of objects: Sip and SipAudioCall. Sip is the main object which manages the Sip services. Once you make a call or receive an incoming call you will get a SipAudioCall which represents the call.

    In this example the Sip code is written in the main Activity. A better solution is to manage the Sip services from a Service module. Otherwise you may lose incoming calls if the Activity is not in the foreground.

    The first step is to register to the server.
    Sub Activity_Create(FirstTime As Boolean)
    If Sip.IsInitialized = False Then
    'Check if SIP and VOIP are supported.
          If Sip.IsSipSupported = False OR Sip.IsVoipSupported = False Then
    Log("Not supported.")
    ToastMessageShow("SIP not supported."True)
    'Register with the VOIP service
    End If
    End If
    End Sub

    Sub SIP_Registering
    End Sub

    Sub SIP_RegistrationFailed (ErrorCode As Int, ErrorMessage As String)
    Log("Failed, ErrorCode=" & ErrorCode & ", Message=" & ErrorMessage)
    ToastMessageShow("Registration failed."True)
    End Sub

    Sub SIP_RegistrationDone (ExpiryTime As Long)
    Log("RegistrationDone, ExpiryTime=" & ExpiryTime)
    ToastMessageShow("SIP registered sucessfully"True)
       btnMakeCall.Enabled = 
    End Sub
    Registering is done by initializing the Sip object with your account Uri and password and calling Register.
    The actual registration is done in the background.
    RegistrationDone event is raised when registration completes successfully (with the expiry time measured in seconds). If there is a problem then RegistrationFailed will be raised.
    The error codes are available here.

    Making calls
    Now that the Sip is registered we can make audio calls.
    This is done by calling Sip.MakeCall.
    For example, this code calls a free directory service:
    Sub btnMakeCall_Click
       CurrentCall = 
    End Sub
    CurrentCall is of type SipAudioCall and it is declared in Process_Globals.
    You should now wait for the CallEstablished event (or CallError if there is a problem).
    CallEstablished event is raised when the other side answers the call.
    Sub SIP_CallEstablished
    'Check that we are not already in a call.
       'Seems like this event can be raised by the SIP service multiple times.
       If CurrentCall.IsInCall Then Return

    'Start the audio
       CurrentCall.SpeakerMode = True
    ToastMessageShow("Call established"True)
    End Sub
    When the call is established we need to call StartAudio and set SpeakerMode to True in order to start talking.

    As you can see in the above code, we first check if CurrentCall.IsInCall is false. It will be true after the StartAudio call. It seems that the internal Sip service raises the CallEstablished more than once for a single call. This way we make sure that our code only runs once for each call.

    Incoming calls
    When there is an incoming call, the CallRinging event will be raised:
    'Incoming call
    Sub SIP_CallRinging (IncomingCall As SipAudioCall)
    ToastMessageShow("Ringing from: " & IncomingCall.PeerUri, True)
       CurrentCall = IncomingCall
    End Sub

    'Answers an incoming call
    Sub btnAnswer_Click
    End Sub
    A SipAudioCall is passed in this event. We should hold a reference to this object. Calling AnswerCall will cause the call to be established and will raise the CallEstablished event.

    See the attached example for a simple program that calls a free directory service and handles incoming calls.

    In order to test it you can register to Welcome to, the IP Telecommunications Portal | I registered two accounts and then I was able to call from the computer to the device (to test the incoming calls).

    The library is available here:

    Attached Files:

      File size:
      6.9 KB
    Multiverse app likes this.
  2. NJDude

    NJDude Expert Licensed User

    Where's the library?
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. NJDude

    NJDude Expert Licensed User

    Thanks, I thought you forgot to put it in the ZIP file. :D
  5. Nizze

    Nizze Active Member Licensed User


    Hi Erel

    Great to hear about SIP
    I tryed on my Samsung S2 but on that one it did not work .
    But it worked on my Motorola XOOM.

    Can i do anything to make it work on my Samsung ??

    // Nizze
  6. Erel

    Erel Administrator Staff Member Licensed User

    How didn't it work? Did you check the logs? Are there any error message?
  7. Nizze

    Nizze Active Member Licensed User

    I have not checked the logs, But i get "sip not supported " with the test app
    And when i try the same apk in my xoom .. It works OK


  8. Erel

    Erel Administrator Staff Member Licensed User

    Not all devices support Sip services. I don't think that you can do anything about it. Maybe upgrading the OS will help.
  9. Nizze

    Nizze Active Member Licensed User

    Oki doki ..

    I have 2.3.5 in my Samsung S2 .
    So lets hope that Samsung can add this feature in some upcoming releases.

    I will take some other phones and try .


  10. johnthomcode

    johnthomcode Member Licensed User

    I keep getting "TRANSACTION_TERMINTED" (3), but phone is supported (LG LS670), WiFi and 3G are both active, and I've tried two different SIP carriers. Is anyone else seeing this or aware what may be causing it?

  11. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried it with
  12. johnthomcode

    johnthomcode Member Licensed User


    Yes, I am. I've tried repeatedly since posting with the same results. I wasn't sure if they were down? I also made sure WiFi was active in case Sprint was preventing data access.

    I also tried Any other suggestions that anyone is having success with?


  13. Ian Garton

    Ian Garton Member Licensed User

    Does this library support direct SIP calling? ie, SIP without having to register with a server?

    On other O/S platforms I can place and receive SIP calls by using the URI addresses, however I can't work out how to initialise the SIP object here for this purpose.
  14. Erel

    Erel Administrator Staff Member Licensed User

    I think that a server is always required. Maybe the other device uses a default server.
  15. selvamurali

    selvamurali Active Member Licensed User

    SIP Error

    Hi Erel

    I am try to run the sip program but i got this error

    B4A line: 20
    javac 1.7.0_03
    src\anywheresoftware\b4a\samples\sip\ error: cannot access SipException
    class file for not found
  16. Erel

    Erel Administrator Staff Member Licensed User

    Have you set android.jar correctly:
  17. ovt001

    ovt001 Member Licensed User

    Hi Erel,
    I have tried the source code on my mobile Motorola Defy Plus 2.3.5 but I receive SIP not supported.
    BUT other Sip client are working fine...
    Do you have an issue?
    What check really the function "IsSipSupported"??

    thank you
  18. Erel

    Erel Administrator Staff Member Licensed User

  19. zritholtz

    zritholtz New Member Licensed User

    No Audio

    Hi, I tried the sample code. The account registers. When I make an outbound call, I get no audio. When I receive a call, the audio works. Any suggestions?
  20. Erel

    Erel Administrator Staff Member Licensed User

    Is the CallEstablished event raised?
    luke2012 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice