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.basic4ppc.com/forum/addi...updates/13089-sip-voip-library.html#post73694
 

Attachments

  • Sip.zip
    6.9 KB · Views: 10,783

billyn_hk

Member
Licensed User
Longtime User
Last edited:

Julien Brunelle

Member
Licensed User


I think you need to fix your object lib loader... like are you still trying to run 2.6 objects in api 15-24 area.... see api 2.6 has small lib object..... sip works off tcipi like mail.... all your new androids wont work you sill load old object as 15 or 24 api.... load in the android.jar 15 in eclipse dig into it and you will see..... its not just 2 class that make it work in new android apps..... you have like 6 class that work in conjunction of each other...... you need ways to connect and load the correct object model API

see what I need to learn is how to wrap a android jar class api at 15 and up old phones are almost gone now...


web hosting CEO Julien@ http://3taccount.com
 

Julien Brunelle

Member
Licensed User
your sip library will not work.... it works off a socket lib...old phone did it all probably new phones wont.... API versions.. the socket never stared..so it fails

I was trying to set it up.... I've just started in B4A......
the android.jar exposes 100% of the lower API object libraries
in VB I would call that a dill file or OS API.....
I should be able to wrap the library in eclipse somehow then expose it to B4A
I did make your temperature sample work from eclipse....
but I'm not sure on how to wrap a low level library properly....more time for me...
please you need better samples....with a proper index off your website toolbar....
one thing you should do is have the ability to create ActiveX controls this way...
like I could create a library control in eclipse an it shows up as an insert control......
just like a button.... or as a hidden data binding control....
user drags the new control on the form and bingo....ActiveX library base on an android.jar api version

B4A should create a control library folder and then retrieve the control folder....
now I just dump the new ActiveX control in the folder...
data grid and data binding text box..... your the container let the developers expand its reusability via AxtiveX
now you can offer features that can expand returns for developers...buy my control page...version 2-17-20-24//// version 2.6 is still good I believe its used in phone watches small foot print

you just need good samples to show how this can be done or it wont expand as your container control feature

web hosting CEO Julien@ http://3taccount.com
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User

Julien Brunelle

Member
Licensed User
This is not the place to post general suggestions.


Have you tried it? Which error did you get?

This library is based on SipManager: http://developer.android.com/reference/android/net/sip/SipManager.html
I don't see any mention of this API only working on "old phones".

yep I did with API 15 ....like I said its the socket its not starting up.... if you use eclipse and scan android.jar package you will see its more then just 2 class libraries that need to work conjunction with each other......
SipAudioCall
SipAudioCall.Listener
SipErrorCode
SipManager
SipProfile
SipProfile.Builder
SipSession
SipSession.Listener
SipSession.State

see your dealing with API levels ....you have 2 libs only..... not sure how you even got it to work... it could be because you used an active phone that had the socket opened your 2 libs jump on the tcpi listener.....yet sip is like mail headers...

best way to get it going is to use a none phone android... like a tablet

you can also build one from scratch in b4a or b4j

web hosting CEO Julien@ http://3taccount.com
 
Last edited:

Julien Brunelle

Member
Licensed User
Please post the exact error message from the logs.

it wont give you an error..... its from your test samples.... connection not available basicly... so the socket never started.... simple deductions....
your dealing with TCPIP socket ... it why you see the listener object

like its not made for video... yet it can do more then one call at a time its a socket .. its why it works in some phones and not in the other.... the android has an active listener open.. like i said i just started in b4x.....

its why i would like to see better libs wraps from eclipse and understand your core.jar work...... if i understood this then i could make the sip work....

make a simple sample that wraps a simple lib out the android.jar like a button lib... remember there's reason why we have api versions... its only backward compatible for so many versions.... its why you need a good reference library to cross check with...
understand a button lib is not complex as the sip package

web hosting CEO Julien@ http://3taccount.com
 

Julien Brunelle

Member
Licensed User
I'm sorry but I don't see how I can help you.

To avoid confusion for other readers, this library is supported by Android 2.3+.

So what your saying is point the B4A project to android.jar version 2.3 and grant support up API 15 will make the phone app work...... because older version are not like the new ones....

this way other readers wont get confused right....

web hosting CEO Julien@ http://3taccount.com
 

jefflynn1974

Member
Licensed User
Longtime User
Hello!

I have a problem. After I give the logon details the SIP_Registering sub and the others (SIP_RegistrationFailed or SIP_RegistrationDone) isn't called. Nothing happens. I have tried with other SIP applications too and I can login without any problem.
 

Julien Brunelle

Member
Licensed User
This question was pointed to @jefflynn1974 .
my question was directed at you...... fix your sample..... i pick up your software because of this sample..... and it wont work

like did you setup the Manifest permissions and feature requests: its your sample.....

<!-- Permissions -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- ...and features -->
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />

i moved on to different way... its your wrap class that wont work....

web hosting CEO Julien@ http://3taccount.com
 
Last edited:
Top