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

mrodriguez

Member
Licensed User
Longtime User
Jain Sip Stack Library B4A

Hi Erel,
Because Sip API is blocked by carriers (that way you should always call isVoipSupported to verify that the device supports VOIP), we needs a third party Jain Sip library for B4A.

Information about why Sip API is blocked:
Enable Android Gingerbread native android SIP Stack | smartphone and tablet review, tips,tweak and hack
You need a root user, that way it is hard to implement.

Jain Sip information:
a) Jain sip (JSIP: JAVA API for SIP Signaling — Java.net) is a JAVA SIP STACK.
b) Android SDK's default implementation (API > 9) is based on Jain sip.
c) Jain Sip has been installed on Android 2.3+ with the following issues:
Android 2.3 using external JAIN-SIP(J-SIP) Stack | Classpath - Stack Overflow

Can yo make?
1. Jain Sip Android Installer (With rename the base packages like the issue says).
2. B4A library

Our goal is to use sip registration, call and hungup without carriers prohibits using our own Jain Sip Stack.

We can contribute with 2.000 usd. It should be very close to the SIP B4A Library due Android Sip is based on Jain Sip too.

Thanks
 

padvou

Active Member
Licensed User
Longtime User
B4X:
android.net.sip.SipException: build SipProfile


   at android.net.sip.SipManager.makeAudioCall(SipManager.java:364)
   at anywheresoftware.b4a.objects.SIP.MakeCall(SIP.java:199)
   at anywheresoftware.b4a.samples.sip.main._btnmakecall_click(main.java:300)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
   at android.view.View.performClick(View.java:4084)
   at android.view.View$PerformClick.run(View.java:16966)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.text.ParseException: sip:*04179: Missing host name (at offset 4) (at offset 0)
   at gov.nist.javax.sip.address.AddressFactoryImpl.createURI(AddressFactoryImpl.java:223)
   at android.net.sip.SipProfile$Builder.<init>(SipProfile.java:123)
   at android.net.sip.SipManager.makeAudioCall(SipManager.java:359)
   ... 19 more
android.net.sip.SipException: build SipProfile
Any ideas?


@zritholtz: Looks like a codec mismatch
 

padvou

Active Member
Licensed User
Longtime User
B4X:
android.net.sip.SipException: build SipProfile


   at android.net.sip.SipManager.makeAudioCall(SipManager.java:364)
   at anywheresoftware.b4a.objects.SIP.MakeCall(SIP.java:199)
   at anywheresoftware.b4a.samples.sip.main._btnmakecall_click(main.java:300)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
   at android.view.View.performClick(View.java:4084)
   at android.view.View$PerformClick.run(View.java:16966)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.text.ParseException: sip:*04179: Missing host name (at offset 4) (at offset 0)
   at gov.nist.javax.sip.address.AddressFactoryImpl.createURI(AddressFactoryImpl.java:223)
   at android.net.sip.SipProfile$Builder.<init>(SipProfile.java:123)
   at android.net.sip.SipManager.makeAudioCall(SipManager.java:359)
   ... 19 more
android.net.sip.SipException: build SipProfile
Any ideas?


@zritholtz: Looks like a codec mismatch

I also get many of these:
B4X:
Failed, ErrorCode=-3, Message=transaction terminated
 

giga

Well-Known Member
Licensed User
Longtime User
I would pay close attention to this area:

Caused by: java.text.ParseException: sip:*04179: Missing host name

It looks like the SIP is not building correctly (android.net.sip.SipException: build SipProfile). Its missing your host server name ie. 99.99.99.99

sip:*04179@99.99.99.99

Hope this helps: GOOD LUCK.
 

padvou

Active Member
Licensed User
Longtime User
Alright, I changed the uri to "numberdialed"@pbxIP ex: 2333333@192.168.1.1

Any ideas on this??:
B4X:
Failed, ErrorCode=-3, Message=transaction terminated
 

giga

Well-Known Member
Licensed User
Longtime User
Alright, I changed the uri to "numberdialed"@pbxIP ex: 2333333@192.168.1.1

Any ideas on this??:
B4X:
Failed, ErrorCode=-3, Message=transaction terminated

You could also try it with the specified port:
some sips require a number dialed first like "9"
i.e 92333333@192.168.1.1:5060
 

devindia

Member
Licensed User
Longtime User
error during compilation

No resource identifier found for attribute 'installLocation' in package 'android'--
after downloading sip.zip, execution of sip.b4a gives error, and 'gen' folder in Objects folder dont have R file or layout dont have any xml file in res folder.
 

devindia

Member
Licensed User
Longtime User
error during compilation

No resource identifier found for attribute 'installLocation' in package 'android'--
after downloading sip.zip, execution of sip.b4a gives error, and 'gen' folder in Objects folder dont have R file or layout dont have any xml file in res folder.
 

giga

Well-Known Member
Licensed User
Longtime User
No resource identifier found for attribute 'installLocation' in package 'android'--
after downloading sip.zip, execution of sip.b4a gives error, and 'gen' folder in Objects folder dont have R file or layout dont have any xml file in res folder.

If you are using eclipse R.java can be a nightmare. I have seen others report the R.java issue and in some cases the import android.R is the wrong one for you package. I have dealt with this R problem as well with no luck. Good Luck.
 

devindia

Member
Licensed User
Longtime User
query about audio codec

do this lib support g729 and alaw(g711) ulaw(g711) audio codec?
 

devindia

Member
Licensed User
Longtime User
log info- "Not Supported."

using samsung duos version is 2.3.6
android.jar is of platform android-10, as mentioned
what is the way out? thanks in advance.
 

aostano

New Member
Licensed User
Longtime User
Error in register SIP session

Hi,
when i try to execute

Sip.Initialize("SIP","100","192.168.0.1:4460","XXXX")

i receive this error:

Failed, ErrorCode=-10, Message=no data connection
or
Failed, ErrorCode=-9, Message=0

why ?

Thank's @ all
 

giga

Well-Known Member
Licensed User
Longtime User
Hi,
when i try to execute

Sip.Initialize("SIP","100","192.168.0.1:4460","XXXX")

i receive this error:

Failed, ErrorCode=-10, Message=no data connection
or
Failed, ErrorCode=-9, Message=0

why ?

Thank's @ all

try this
B4X:
 Sip.Initialize2("SIP", "100@192.168.0.1:4460", "xxx")

works for me. Also verify 4460 as your SIP port. (not sure about that port) usually 5060 Good Luck
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
"A better solution is to manage the Sip services from a Service module".

So you mean to migrate the code from Activity_Create to a service module and run it at startup ?
 
Top