Skip to main content

Android Client SDK

Enable Telnyx real-time communication services on Android.

Project structure:

  • SDK project: sdk module, containing all Telnyx SDK components as well as tests.
  • Demo application: app module, containing a sample demo application utilizing the sdk module.

Project Setup:

  1. Clone the repository
  2. Open the cloned repository in Android Studio and hit the build button to build both the sdk and sample app:
  3. Connect a device or start an emulated device and hit the run button

Usage

  1. Add Jitpack.io as a repository within your root level build file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Note: After pasting the above content, Kindly check and remove any new line added

  1. Add the dependency within the app level build file:
dependencies {
implementation 'com.github.team-telnyx:telnyx-webrtc-android:Tag'
}

Note: After pasting the above content, Kindly check and remove any new line added

Tag should be replaced with the release version.

Then, import the WebRTC SDK into your application code at the top of the class:

import com.telnyx.webrtc.sdk.*

Note: After pasting the above content, Kindly check and remove any new line added

The ‘*’ symbol will import the whole SDK which will then be available for use within that class.

NOTE: Remember to add and handle INTERNET, RECORD_AUDIO and ACCESS_NETWORK_STATE permissions in order to properly use the SDK

Telnyx Client

To initialize the TelnyxClient you will have to provide the application context. Once an instance is created, you can call the .connect() method to connect to the socket. An error will appear as a socket response if there is no network available:

  telnyxClient = TelnyxClient(context)
telnyxClient.connect()

Note: After pasting the above content, Kindly check and remove any new line added

Logging into Telnyx Client

To log into the Telnyx WebRTC client, you'll need to authenticate using a Telnyx SIP Connection. Follow our quickstart guide to create JWTs (JSON Web Tokens) to authenticate. To log in with a token we use the tokinLogin() method. You can also authenticate directly with the SIP Connection username and password with the credentialLogin() method:

 telnyxClient.tokenLogin(tokenConfig)
//OR
telnyxClient.credentialLogin(credentialConfig)

Note: After pasting the above content, Kindly check and remove any new line added

Note: tokenConfig and credentialConfig are data classes that represent login settings for the client to use. They look like this:

sealed class TelnyxConfig

data class CredentialConfig(
val sipUser: String,
val sipPassword: String,
val sipCallerIDName: String?, // Your caller ID Name
val sipCallerIDNumber: String?, //Your caller ID Number
val ringtone: Int?, // Desired ringtone int resource ID
val ringBackTone: Int?, // Desired ringback tone int resource ID
val logLevel: LogLevel = LogLevel.NONE // SDK log level
) : TelnyxConfig()

data class TokenConfig(
val sipToken: String, // JWT login token
val sipCallerIDName: String?, // Your caller ID Name
val sipCallerIDNumber: String?, //Your caller ID Number
val ringtone: Int?, // Desired ringtone int resource ID
val ringBackTone: Int?, // Desired ringback tone int resource ID
val logLevel: LogLevel = LogLevel.NONE // SDK log level
) : TelnyxConfig()

Note: After pasting the above content, Kindly check and remove any new line added

Creating a call invitation

In order to make a call invitation, you need to provide your callerName, callerNumber, the destinationNumber (or SIP credential), and your clientState (any String value).

   telnyxClient.call.newInvite(callerName, callerNumber, destinationNumber, clientState)

Note: After pasting the above content, Kindly check and remove any new line added

Accepting a call

In order to be able to accept a call, we first need to listen for invitations. We do this by getting the Telnyx Socket Response as LiveData:

  fun getSocketResponse(): LiveData<SocketResponse<ReceivedMessageBody>>? =
telnyxClient.getSocketResponse()

Note: After pasting the above content, Kindly check and remove any new line added

We can then use this method to create a listener that listens for an invitation - in this example we assume getSocketResponse is a method within a ViewModel.

 mainViewModel.getSocketResponse()
?.observe(this, object : SocketObserver<ReceivedMessageBody>() {
override fun onConnectionEstablished() {
// Handle a succesfully established connection
}

override fun onMessageReceived(data: ReceivedMessageBody?) {
when (data?.method) {
SocketMethod.LOGIN.methodName -> {
// Handle a successfull login - Update UI or Navigate to new screen, etc.
}

SocketMethod.INVITE.methodName -> {
// Handle an invitation Update UI or Navigate to new screen, etc.
// Then, through an answer button of some kind we can accept the call with:
val inviteResponse = data.result as InviteResponse
mainViewModel.acceptCall(inviteResponse.callId, inviteResponse.callerIdNumber)
}

SocketMethod.ANSWER.methodName -> {
//Handle a received call answer - Update UI or Navigate to new screen, etc.
}

SocketMethod.BYE.methodName -> {
// Handle a call rejection or ending - Update UI or Navigate to new screen, etc.
}
}
}

Note: After pasting the above content, Kindly check and remove any new line added

When we receive a call we will receive an InviteResponse data class that contains the details we need to accept the call. We can then call the acceptCall method in TelnyxClient from our ViewModel:

 telnyxClient.call.acceptCall(callId, destinationNumber)

Note: After pasting the above content, Kindly check and remove any new line added


Methods:

.newInvite(callerName, callerNumber, destinationNumber, clientState)

Initiates a new call invitation, sending out an invitation to the destinationNumber via the Telnyx Socket Connection

NameTypeRequiredDescription
callerNameStringrequiredYour caller name
callerNumberStringrequiredYour caller number
destinationNumberStringrequiredThe person you are calling. Either their number or SIP username
clientStateStringrequiredA state you would like to convey to the person you are calling.
telnyxClient.call?.newInvite(callerName, callerNumber, destinationNumber, clientState)

Note: After pasting the above content, Kindly check and remove any new line added

.acceptCall(callId, destinationNumber) Accepts an incoming call. Local user responds with both local and remote SDPs

NameTypeRequiredDescription
callIdUUIDrequiredID of Call to respond to, this will be in a JSON
destinationNumberStringrequiredNumber or SIP username of the person calling, this will be in a JSON Object returned by the socket as an invitation
 telnyxClient.call.acceptCall(callId, destinationNumber)

Note: After pasting the above content, Kindly check and remove any new line added

.endCall(callID) Ends an ongoing call with a provided callID, the unique UUID belonging to each call

NameTypeRequiredDescription
callIdUUIDrequiredID of Call to end. Each instance of a call has a callId parameter.
telnyxClient.call.endCall(callId)

Note: After pasting the above content, Kindly check and remove any new line added

.getCallState() Returns call state live data. This can be used to update UI. CallStates can be as follows: NEW, CONNECTING, RINGING, ACTIVE, HELD or DONE.

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
var currentCallState = currentCall.getCallState()

Note: After pasting the above content, Kindly check and remove any new line added

.onMuteUnmutePressed() Either mutes or unmutes the AudioManager based on the current muteLiveData value

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

currentCall.onMuteUnmutePressed()

Note: After pasting the above content, Kindly check and remove any new line added

.getIsMuteStatus() Returns mute state live data. This can either be true or false.

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

var isMute = currentCall.getIsMuteStatus()

Note: After pasting the above content, Kindly check and remove any new line added

.onHoldUnholdPressed(callID) Either places a call on hold, or unholds a call based on the current holdLiveData value.

NameTypeRequiredDescription
callIdUUIDrequiredID of Call to hold or unhold.
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

currentCall.onMuteUnmutePressed(callID)

Note: After pasting the above content, Kindly check and remove any new line added

.getIsOnHoldStatus() Returns hold state live data. This can either be true or false.

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

var isOnHold = currentCall.getIsOnHoldStatus()

Note: After pasting the above content, Kindly check and remove any new line added

.onLoudSpeakerPressed() Either enables or disables the AudioManager loudspeaker mode based on the current loudSpeakerLiveData value.

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

currentCall.onLoudSpeakerPressed()

Note: After pasting the above content, Kindly check and remove any new line added

.getIsOnLoudSpeakerStatus() Returns loudspeaker state live data. This can either be true or false.

var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]

var isLoudSpeaker = currentCall.getIsOnLoudSpeakerStatus()

Note: After pasting the above content, Kindly check and remove any new line added

ProGuard changes

NOTE: In the case that you need to modify your application's proguard settings in order to obfuscate your code, such as we have done below:

app/build.gradle

buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
jniDebuggable true
}
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable true
jniDebuggable true
}
}

Note: After pasting the above content, Kindly check and remove any new line added

Please keep in mind that you will need to add the following rules to the proguard-rules.pro file in your app in order for the SDK to continue functioning

app/proguard-rules.pro

-keep class org.webrtc.** { *; }
-keep class com.telnyx.webrtc.sdk.** { *; }

Note: After pasting the above content, Kindly check and remove any new line added

Questions? Comments? Building something rad? Join our Slack channel and share.

On this page