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:xxx@iptel.org", "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:8355@ideasip.com", 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,247

luke2012

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
If call.PeerUri <> "" Then
Dim r As Reflector
r.Target = call
r.Target = r.RunMethod("getPeerProfile")
Log(r.RunMethod("getDisplayName")
End If

I tried the code and the return value is null.
 

fotosettore

Member
Licensed User
Longtime User
HI Erel
i have 3 devices . first is enabled (not rooted) and your demo software works fine
2 does not work and says "sip not supported"
i read that in this case we need to unlock the phone (root priviliges)
but i downloaded the app called "sipdroid" and it works fine in the devices locked
what is the difference in library about the check in your demo about availability of voip and the check of sipdroid ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I tried the code and the return value is null.
This is the only method available to get the display name. It means that the name is not available.

i have 3 devices . first is enabled (not rooted) and your demo software works fine
2 does not work and says "sip not supported"
i read that in this case we need to unlock the phone (root priviliges)
but i downloaded the app called "sipdroid" and it works fine in the devices locked
what is the difference in library about the check in your demo about availability of voip and the check of sipdroid ?
I do not know whether rooting will help in this case. You might need to install a custom ROM that supports the built-in SIP service.

sipdroid probably doesn't rely on the installed SIP service.
 

luke2012

Well-Known Member
Licensed User
Longtime User
@Erel now I'm able to handle SIP calls! Very good :)

I have only a question : when a Caller make a call how to reproduce the classic calling phone tone ?
 

luke2012

Well-Known Member
Licensed User
Longtime User
You can use RingtoneManager.GetDefault (Phone library).

Another issue that the user could have is the mobile provider data cut-off.
Actually I'm using a Internet Mobile promotion that give me 1 GB of data traffic per month.

When the 1GB threshold is reached the provider could limit the data traffic and the VOIP traffic could be influenced.
There is a way to catch this limit of data traffic in order to advice the user?
 

Aidan Ruff

Member
Licensed User
Longtime User
I have a "SIP NOT SUPPORTED" problem as well. on a number of cheap Chinese phones with 4.2.2.
I have installed Acrobits Softphone which works perfectly well, but the B4E module says that SIP isn't supported when it clearly is!

Any ideas?
 

Aidan Ruff

Member
Licensed User
Longtime User
Thanks for the reply. When I look in /system/etc/permissions I see the 'andoird.software.sip.voip' fxml file, so it allows SIP, but you reckon that the OS, whilst allowing it, doesn't actually implement it?

I wonder if you know about any way of implementing SIP in B4A then?
thx
 

Giuliano Cucchiarini

Member
Licensed User
Longtime User
Hi
I'm trying to use the SIP library with a Lenovo IdeaPad100. I get the message 'SIP not supported'.
Since I need to develop an application with SIP an a tablet, is there someone who knows with tablet on the market have SIP native Support?
Thank You
Giuliano C.
 

Aidan Ruff

Member
Licensed User
Longtime User
None of the cheap Chinese tablets appear to have the SIP stack support - there are a number of SIP libraries out there for java, but I haven't yet had a chance to write a wrapper for B4A. You can use third party SIP clients if you like...I've successfully used the Skype library, but it's not as elegant as the direct SIP calls.

However, you can use mainstream tablets like the Nexus 7 which work just fine with the B4A SIP library. I've also used the Samsung S4 and S5 phones.
 
Top