Android Tutorial Android Sip / Voip tutorial

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.

Registration
The first step is to register to the server.
B4X:
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)
      Else
         'Register with the VOIP service
         Sip.Initialize2("SIP", "sip:[email protected]", "xxx")
         Sip.Register
      End If
   End If
   Activity.LoadLayout("1")
End Sub

Sub SIP_Registering
   Log("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 = True
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:
B4X:
Sub btnMakeCall_Click
   CurrentCall = Sip.MakeCall("sip:[email protected]", 30)
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.
B4X:
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

   CurrentCall.StartAudio '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:
B4X:
'Incoming call
Sub SIP_CallRinging (IncomingCall As SipAudioCall)
   Log("CallRinging")
   ToastMessageShow("Ringing from: " & IncomingCall.PeerUri, True)
   CurrentCall = IncomingCall
End Sub

'Answers an incoming call
Sub btnAnswer_Click
   CurrentCall.AnswerCall(30)
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 iptel.org, the IP Telecommunications Portal | iptel.org. 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: http://www.b4x.com/forum/additional-libraries-official-updates/13089-sip-voip-library.html#post73694
 

Attachments

  • Sip.zip
    6.9 KB · Views: 11,212

deantangNYP

Active Member
Licensed User
Longtime User
may i know what error (ErrorCode=-3) is this? i am using Linphone and trying to Register using the sample app provided.
I did not encounter this error with IPTEL, only with Linphone. But i am required to use Linphone. Please advise.

(My Raspberry Pi has no issue connecting to the SIP server using a separate linphone account)

B4X:
Sip.Initialize2("SIP", "sip:[email protected]", "password")
Sip.Register

B4X:
Failed, ErrorCode=-3, Message=transaction terminated
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User

deantangNYP

Active Member
Licensed User
Longtime User

deantangNYP

Active Member
Licensed User
Longtime User
Is there any new update on the SIP native library? Recently, i encountered issues Registering and Establishing calls with another SIP client.
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear Erel,

earlier I wrote about free TEK sip software and you gave me an advice -
if the firewall have allow the connection on the port
(page 8)

I allow every connection in firewall, but I get message from the APP : "Transaction terminated"
I cant register the app to teksip software.


Please this Android SIP/VOIP Library is usable only for web SIP projects?
Or it is usable for some local sip service = free TEKsip software and only using my wifi (without internet connection)?

I am using this for registering:
B4X:
Sip.Initialize2("SIP", "sip:[email protected]:5060", "")

Thank you very much
Best regards
p4ppc
 

AndyW999

Member
Licensed User
Longtime User
It should work with a local connection. The problem is somewhere else. Maybe the native SIP implementation doesn't support this SIP server.

Maybe you need to supply as password in the last "" field?

Or you do not need the "sip: bit?

This works with FreeSwitch - Sip.Initialize2("SIP", "[email protected]", "1234")
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear Andy,

thank you very much for your reply.

Excuse me, but I dont understand what you wrote: "Or you do not need the "sip: bit?"

I am trying all of this:
B4X:
Sip.Initialize2("SIP", "sip:[email protected]:5060", "1234")

        '    Sip.Initialize("SIP", "", "192.168.1.108:5060", "")
'             Sip.Initialize2("SIP", "[email protected]:5060", " ")
'            Sip.Initialize2("SIP", "sip:[email protected]", "")
  '          Sip.Initialize2("SIP", "sip:[email protected]:5060", "")

I am sending settings of Tsip - please can you give me advice?
Best regards
p4ppc
 

Attachments

  • 1.jpg
    1.jpg
    57.8 KB · Views: 619
  • 2.jpg
    2.jpg
    21.8 KB · Views: 532
  • 3.jpg
    3.jpg
    36.1 KB · Views: 509
  • 4.jpg
    4.jpg
    32 KB · Views: 538
  • 5.jpg
    5.jpg
    32.8 KB · Views: 488
  • 6.jpg
    6.jpg
    15.4 KB · Views: 582
Last edited:

AndyW999

Member
Licensed User
Longtime User
If you are using an Android phone then I would suggest installing Zoiper and making sure it works with TEK sip first.

Then I would try your application.

If TEK sip has logging I would turn that on or use Wireshark with SIP filtering to check what is actually being sent and make sure it is the same from both Zoiper and your application.

I know nothing about TEK sip but I do know that FreeSwitch works very well with my B4A SIP application over WiFi so I would suggest installing FreeSwitch as it actually does everything!
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Andy,

thank you very much, I understand, I will start with installing : FreeSwitch. It will be better.
I have read, that tek SIP have many limitations and exeptions.

Thank you very much
Best regards
p4ppc
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

I had 3 success calls, but after this calls I cant repeat it. It is possible, that is mistake in Windows, router, or somewhere but not in the APP? I have turn OFF firewall, antivirus...

Wireshark write this:
1.jpg


I not understand why I had 2 or 3 success calls and after this I cant do the call. I have done registraton successfuly, but I cant do the call.
Thank you very much
Best regards
p4ppc
 
Last edited:

petr4ppc

Well-Known Member
Licensed User
Longtime User
It was my mistake - after restarting computer, router, tablets FreeSwitch looks good Andy (for this moment)! thank you
 
Top