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:
- Clone the repository
- Open the cloned repository in Android Studio and hit the build button to build both the sdk and sample app:
- Connect a device or start an emulated device and hit the run button
Usage
- Add Jitpack.io as a repository within your root level build file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency within the app level build file:
dependencies {
implementation 'com.github.team-telnyx:telnyx-webrtc-android:Tag'
}
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.*
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()
Logging into Telnyx Client
To log into the Telnyx WebRTC client, you'll need to authenticate using a Telnyx SIP Connection. Follow this 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: 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()
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)
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()
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.
}
}
}
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)
Methods:
.newInvite(callerName, callerNumber, destinationNumber, clientState)
Initiates a new call invitation, sending out an invitation to the destinationNumber via the Telnyx Socket Connection
Name | Type | Required | Description |
callerName | String | required | Your caller name |
callerNumber | String | required | Your caller number |
destinationNumber | String | required | The person you are calling. Either their number or SIP username |
clientState | String | required | A state you would like to convey to the person you are calling. |
telnyxClient.call?.newInvite(callerName, callerNumber, destinationNumber, clientState)
.acceptCall(callId, destinationNumber) Accepts an incoming call. Local user responds with both local and remote SDPs
Name | Type | Required | Description |
callId | UUID | required | ID of Call to respond to, this will be in a JSON |
destinationNumber | String | required | Number 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)
.endCall(callID) Ends an ongoing call with a provided callID, the unique UUID belonging to each call
Name | Type | Required | Description |
callId | UUID | required | ID of Call to end. Each instance of a call has a callId parameter. |
telnyxClient.call.endCall(callId)
.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()
.onMuteUnmutePressed() Either mutes or unmutes the AudioManager based on the current muteLiveData value
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
currentCall.onMuteUnmutePressed()
.getIsMuteStatus() Returns mute state live data. This can either be true or false.
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
var isMute = currentCall.getIsMuteStatus()
.onHoldUnholdPressed(callID) Either places a call on hold, or unholds a call based on the current holdLiveData value.
Name | Type | Required | Description |
callId | UUID | required | ID of Call to hold or unhold. |
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
currentCall.onMuteUnmutePressed(callID)
.getIsOnHoldStatus() Returns hold state live data. This can either be true or false.
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
var isOnHold = currentCall.getIsOnHoldStatus()
.onLoudSpeakerPressed() Either enables or disables the AudioManager loudspeaker mode based on the current loudSpeakerLiveData value.
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
currentCall.onLoudSpeakerPressed()
.getIsOnLoudSpeakerStatus() Returns loudspeaker state live data. This can either be true or false.
var calls = telnyxClient.getActiveCalls()
currentCall = calls[callID]
var isLoudSpeaker = currentCall.getIsOnLoudSpeakerStatus()
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
}
}
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.** { *; }
Questions? Comments? Building something rad? Join our Slack channel and share.