a shot of dev knowledge

RELATED TAGS

What is SIP in Android?

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.

Requirements to use SIP

  1. Android 2.3 mobile device or higher.
  2. Internet connection via a mobile data service or WiFi. AVD(Android Virtual Device) cannot be used.
  3. A SIP account from a reliable SIP provider.

How SIP works

Set up the application’s manifest

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>
Android Manifest Permissions

These permissions:

  • Ensure that the application can only be installed on devices that are capable of supporting SIP.
  • Can filter the application from any devices that don’t support SIP.

If an application is designed to receive calls, it must also define a receiver.

How to create SIP manager

The application must create a SipManager object to use the SIP API. The SipManager is responsible for:

  1. Initiating and receiving calls.
  2. Registering and unregistering with a SIP provider.
  3. Verifying session connectivity.

The line of code below creates sipManager, an instance of the SipManager.

//create an object of the SipManager
val sipManager: SipManager? by lazy(LazyThreadSafetyMode.NONE) {
    SipManager.newInstance(this)
}
Create an instance of SipManager

Register with SIP server

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 SipProfile
private var sipProfile: SipProfile? = null

val builder = SipProfile.Builder(username, domain)
        .setPassword(password)
sipProfile = builder.build()

//set intent and open local profile for making and receiving SIP calls
val 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 sent
        updateStatus("Registering with SIP Server...")
    }

    override fun onRegistrationDone(localProfileUri: String, expiryTime: Long) {
         //Called when the registration is successful
        updateStatus("Ready")
    }

    override fun onRegistrationFailed(
        //Called when the registration fails
            localProfileUri: String,
            errorCode: Int,
            errorMessage: String
    ) {
        updateStatus("Registration failed. Please check settings.")
    }
})
Create SipProfile, register it with SIP provider and track events.

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.

SIP API classes and interfaces

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

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring