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,087

kahvesoft

New Member
Licensed User
Longtime User
I wanted to test the SIP application. error given by the register while

Failed, errorCode = -9, Message = 0



I'm using the mini sip server. They're also not be a problem for another client registers

I used part of the register

Sip.ınitialize2 ("SIP", "sip: [email protected]", "102")
sip.regist is

What could be the problem.


mini sip server debug:

2014-11-08 17:34:53 | Recv SIP-UDP message address=192.168.2.69, port=58119 (len=375):
REGISTER sip:192.168.2.122 SIP/2.0
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:53 | sip_reg_fac[000003F5] _procRegister
2014-11-08 17:34:53 | sip_reg_fac[000003F5] oCheckAuthRegister
2014-11-08 17:34:53 | sip_reg_fac[000003F5] receive message from 192.168.2.69:58119 :
2014-11-08 17:34:53 | sip_reg_fac[000003F5] REGISTER sip:192.168.2.122
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:53 | Send SIP-UDP message to (ip=192.168.2.69, port=58119):
2014-11-08 17:34:53 | SIP/2.0 407 Proxy Authentication Required
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;received=192.168.2.69;rport=58119
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>;tag=3752773d
CSeq: 2078 REGISTER
Call-ID: [email protected]
Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, REFER, SUBSCRIBE
User-Agent: miniSIPServer V17 (20 clients) build 20141105
Proxy-Authenticate: Digest realm="192.168.2.122",algorithm=MD5,nonce="3D00503133B3311B038A24F94C667123",stale=FALSE
Content-Length: 0


2014-11-08 17:34:53 | sip_reg_fac[000003F5] fail to check authorization.
2014-11-08 17:34:53 | Recv SIP-UDP message address=192.168.2.69, port=58119 (len=375):
REGISTER sip:192.168.2.122 SIP/2.0
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:53 | sip_reg_fac[000003F5] _procRegister
2014-11-08 17:34:53 | sip_reg_fac[000003F5] oCheckAuthRegister
2014-11-08 17:34:53 | sip_reg_fac[000003F5] receive message from 192.168.2.69:58119 :
2014-11-08 17:34:53 | sip_reg_fac[000003F5] REGISTER sip:192.168.2.122
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:53 | Send SIP-UDP message to (ip=192.168.2.69, port=58119):
2014-11-08 17:34:53 | SIP/2.0 407 Proxy Authentication Required
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;received=192.168.2.69;rport=58119
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>;tag=3106443e
CSeq: 2078 REGISTER
Call-ID: [email protected]
Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, REFER, SUBSCRIBE
User-Agent: miniSIPServer V17 (20 clients) build 20141105
Proxy-Authenticate: Digest realm="192.168.2.122",algorithm=MD5,nonce="3D00503133B3311B038A24F94C667123",stale=FALSE
Content-Length: 0


2014-11-08 17:34:53 | sip_reg_fac[000003F5] fail to check authorization.
2014-11-08 17:34:54 | Recv SIP-UDP message address=192.168.2.69, port=58119 (len=375):
REGISTER sip:192.168.2.122 SIP/2.0
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:54 | sip_reg_fac[000003F5] _procRegister
2014-11-08 17:34:54 | sip_reg_fac[000003F5] oCheckAuthRegister
2014-11-08 17:34:54 | sip_reg_fac[000003F5] receive message from 192.168.2.69:58119 :
2014-11-08 17:34:54 | sip_reg_fac[000003F5] REGISTER sip:192.168.2.122
Call-ID: [email protected]
CSeq: 2078 REGISTER
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;rport
Max-Forwards: 70
User-Agent: SIPAUA/0.1.001
Contact: *
Expires: 0
Content-Length: 0


2014-11-08 17:34:54 | Send SIP-UDP message to (ip=192.168.2.69, port=58119):
2014-11-08 17:34:54 | SIP/2.0 407 Proxy Authentication Required
Via: SIP/2.0/UDP 192.168.2.69:58119;branch=z9hG4bK22bb265f955d4d733eeeadac943bc5e7353539;received=192.168.2.69;rport=58119
From: <sip:[email protected]>;tag=2683354394
To: <sip:[email protected]>;tag=014b7cb4
CSeq: 2078 REGISTER
Call-ID: [email protected]
Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, REFER, SUBSCRIBE
User-Agent: miniSIPServer V17 (20 clients) build 20141105
Proxy-Authenticate: Digest realm="192.168.2.122",algorithm=MD5,nonce="3D00503133B3311B038A24F94C667123",stale=FALSE
Content-Length: 0


2014-11-08 17:34:54 | sip_reg_fac[000003F5] fail to check authorization.
 

kahvesoft

New Member
Licensed User
Longtime User
7 different android sip-voip same server or even PC-based software works even in the voip software and autorized. There's something wrong about this. And password and username correct. Tested.
 

spacebuddy

Member
Licensed User
Longtime User
I get undeclared variable 'sip'. I checked the Sip checkbox under libraries. Anyone know why this is happening?
 

spacebuddy

Member
Licensed User
Longtime User
Thank you that really helped. I now get different error

java.lang.NullPointerException

on this line of code
Sip.Register
 

spacebuddy

Member
Licensed User
Longtime User
Here it is

Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.sip
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
Error occurred on line: 31 (main)
java.lang.NullPointerException
at android.net.sip.SipManager.open(SipManager.java:224)
at anywheresoftware.b4a.objects.SIP.Register(SIP.java:169)
at anywheresoftware.b4a.samples.sip.main._activity_create(main.java:346)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.samples.sip.main.afterFirstLayout(main.java:98)
at anywheresoftware.b4a.samples.sip.main.access$100(main.java:16)
at anywheresoftware.b4a.samples.sip.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4697)
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:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
** Activity (main) Resume **
 

spacebuddy

Member
Licensed User
Longtime User
I just tried it on a Motorola Moto G and it works.

I get a lot of echo, not very good quality but it works.
 

billyn_hk

Member
Licensed User
Longtime User
We have just try the sip sample, and tested with 2 sip providers, iptel.org and sip2sip.info
the registering process is not stable testing on Samsung S4 and some other android 4.4 phones.

mostly we get:
error code -10, err msg: no data connection
error code -9, err msg:0

we only able to get successful registration with the Sip.Initialize some of the time, and totally not able to connect with with Sip.Initialize2 , wondering whats the different that may causing this ?

1 out of 30~50 tried, that we may get success registration.
upon successful registration, we were able to make calls from sip1 to sip2 and both party can talk without problem. so the problem lies on the initial connection ... ?

and for success registration, we observe, the event: Registering fire up again after the event: registerationDone. (i.e. the Registering event fires up 2 times.)

any clue of this unstable registration ? or is there any other prequestite environment required ?

1. B4A4.0 -> tools -> configure Path -> android.jar
C:\Program Files\Android\android-sdk\platforms\android-15\android.jar

2.
manifest used:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" />
  <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.MODIFY_AUDIO_SETTINGS" />  
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <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" />  
<supports-screens android:largeScreens="true"
  android:normalScreens="true"
  android:smallScreens="true"
  android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

We try to download Play market sip client, most of them works without problem.
So if we were to develop apps using sip voip, was B4A a good start point ?

if anybody happen to smoothly using this sip library , please shed a light , we are more than happy to offer credit and donations to have this solved.

thanks in advance
 
Last edited:

billyn_hk

Member
Licensed User
Longtime User
This library uses the native SIP features. There are other apps that implement the SIP protocol themselves.

I tested it on several devices (mostly Nexus devices) and didn't encounter such issues.


Thanks Erel for the update. We understand it is not related to B4A.

however, when ppl seeing B4A advertising shows Sip/VoIP as new feature, they might doubt it when they experience unstable issues. Even the SipPhone demo does fail in China phones.

was it possible for us to use this http://www.portsip.com/voipsdk.html inside B4A ?
could you advise how to include this.

Was there anyone whom using B4A and implement their own SIP protocol, we are willing to pay for the code.
Please let us know. thanks
 
Top