SIP, stands for Session Initiation Protocol, was developed by Mark Handley, Henning Schulzrinne, Eve Schooler, and Jonathan Rosenberg. SIP was initially developed to improve the way IP-based calls were handled, and was eventually accepted as a permanent element of the IP Multimedia Subsystem. SIP was standardized as RFC 2543 in 1999.
A protocol is a set of universally accepted standards for computer communications. SIP is a signaling protocol that is broadly used to establish, connect, and disconnect communication paths. It defines the messages passed between the caller and receiver, and manages the actual elements of a call. Android provides an API that supports SIP and allows one to add SIP-based internet telephony features to applications.
Android has a structured call management service and, together with its SIP API, it lets applications easily set up voice calls without having to manage sessions directly. In other words, it provides a platform on which other protocols render services. Video conferencing and Instant messaging applications generally use the Android SIP API.
To set up the application’s manifest, add the following permissions:
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.software.sip.voip" android:required="true" />
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.androidsip">//defines BroadcastReceiver subclass to receive calls<receiver android:name=".IncomingCallReceiver" android:label="Call Receiver" />//defines Android version that use SIP<uses-sdk android:minSdkVersion="9" />//permission to use SIP<uses-permission android:name="android.permission.USE_SIP" />//permission to have internet access<uses-permission android:name="android.permission.INTERNET" />//ensures app is only accessible to devices that support SIP<uses-feature android:name="android.software.sip.voip" android:required="true" />...</manifest>
These permissions:
If an application is designed to receive calls, it must also define a receiver.
The application must create a SipManager
object to use the SIP API. The SipManager is responsible for:
The line of code below creates sipManager
, an instance of the SipManager
.
//create an object of the SipManagerval sipManager: SipManager? by lazy(LazyThreadSafetyMode.NONE) {SipManager.newInstance(this)}
In an Android SIP application, an instance of the SipProfile
defines a SIP profile that includes a SIP account, domain, and server information. The profile associated with the SIP account on the device that runs the application is called the local profile, while the profile that the session is connected to is called the peer profile. When a SIP application logs into the SIP server with the local profile, it effectively registers the device as the location to send SIP calls for the given SIP address.
The following code snippet shows how to create a SipProfile
, register it with a SIP provider, and track registration events.
// create and define a SipProfileprivate var sipProfile: SipProfile? = nullval builder = SipProfile.Builder(username, domain).setPassword(password)sipProfile = builder.build()//set intent and open local profile for making and receiving SIP callsval intent = Intent("android.AndroidSip.INCOMING_CALL")val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA)sipManager?.open(sipProfile, pendingIntent, null)//set a listener on the sipManager to if the sipProfile was successfully registered with the SIP provider.//No effect if the profile is not open to receive calls.sipManager?.setRegistrationListener(sipProfile?.uriString, object : SipRegistrationListener {override fun onRegistering(localProfileUri: String) {//Called when a registration request is sentupdateStatus("Registering with SIP Server...")}override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {//Called when the registration is successfulupdateStatus("Ready")}override fun onRegistrationFailed(//Called when the registration failslocalProfileUri: String,errorCode: Int,errorMessage: String) {updateStatus("Registration failed. Please check settings.")}})
The aforementioned steps are the foundation upon which an application is set to create functions that enable it to make and receive calls on the SIP platform.
The table below is a summary of the classes and interface in the Android SIP API that are required in the handling of SIP events.
SipProfile | Defines a SIP Profile that includes a SIP account, domain and server name. |
SipProfile.Builder | Helper class for creating a SipProfile. |
SipManager | Provides APIs for SIP tasks. |
SipRegistrationListener | It is an interface that serves as a listener for SIP registration |
SipAudioCall | Handles internet audio call over SIP |
SipAudioCall.Listener | Listener for SIP call-related events |
SipErrorCode | Defines error codes during SIP events |
SipSession | Represents a SIP session |
SipSession.Listener | Listener for SIP session-related events |
SipSession.State | Defines SIP session states |