WebRTC Calling

Bit6 calling capabilities are accessed via CallClient.

CallClient callClient = bit6.getCallClient();

Active Calls

When there are ongoing active calls, your app will need to present some form of UI to control the calls (end, mute etc) and render video streams if applicable. The following steps happen:

  1. Bit6 SDK determines that a new active call has been initiated, either incoming or outgoing. The SDK broadcasts your.package.name.BIT6_CALL_ADDED intent.

  2. The application code needs to handle this intent to create or update the in-call UI.

  3. The in-call UI is notified about changes to the active calls and can control them as well. For example, when all the active calls end, the app may decide to hide it.

Steps 2 and 3 can be implemented by either (a) using Bit6 UI Library components or (b) developing custom code. Sample code for both options is available in Bit6 samples repo and is also briefly described below.

Note that for this example we assume that the in-call UI is displayed as an Activity but it can be easily implemented as a Fragment or a View.

In both cases the app code will need to have a container (CallActivity in our example) that will display the UI elements and the video streams. It also handles your.package.name.BIT6_CALL_ADDED intent. In your Manifest.xml:

<activity
    android:name=".CallActivity"
    android:configChanges="orientation|screenSize"
    android:launchMode="singleTask"
    android:theme="@style/AppTheme.NoActionBar.FullScreen">
        <intent-filter>
            <action android:name="your.package.name.BIT6_CALL_ADDED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

(a) Use Bit6 UI Library

Step 1. In layout.xml file for CallActivity:

Add InCall UI component from Bit6 UI Library. It embeds video view, timer and controls.

<com.bit6.ui.InCallView
    android:id="@+id/incall_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Step 2. In CallActivity.java:

Initialize InCallView in onCreate() method and add call state listener on CallClient

InCallView inCallView = (InCallView) findViewById(R.id.incall_view);
inCallView.init(null);
// Get updates about calls. For example, when call ends.
callClient.addStateListener(this);

(b) Custom In-Call UI

Step 1. In layout.xml file for CallActivity:

Add MediaView component for rendering video streams

<com.bit6.sdk.ui.RtcMediaView android:id="@+id/media_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- Add your own UI for controlling the calls -->

Step 2. In CallActivity.java:

Initialize MediaView in onCreate() method

RtcMediaView mediaView = (RtcMediaView)findViewById(R.id.media_view);
// Get updates about calls. For example, when call ends.
callClient.addStateListener(this);

Voice & Video

// Initiate an outgoing voice or video call
Address to = Address.parse("usr:john");
callClient.startCall(to, isVideo, mediaMode);

After the call has been initiated with startCall() method, the SDK will broadcast your.package.name.BIT6_CALL_ADDED intent to notify the application UI about a new ongoing call.

Phone/PSTN

Bit6 interconnects with the phone networks (PSTN) and allows making outgoing phone calls.

Phone numbers must be in E164 format, prefixed with +. So a US (country code 1) number (555) 123-1234 must be presented as +15551231234.

Note, that for the demo purposes you can only make 1 minute free phone calls to the US and Canada numbers.

// Initiate an outgoing phone call
callClient.startPhoneCall("+15551231234");

After the call has been initiated, the application will be notified about a new call in progress via the same intent mechanism described in the previous section.

Incoming Calls

Bit6 SDK has a seamless integration with Android push notification systems for receiving incoming calls. The process of handling an incoming call consists of the following steps:

  1. Incoming call push notification arrives to the device and is handled by Bit6 SDK.

  2. SDK broadcasts your.package.name.BIT6_INCOMING_CALL intent.

  3. The application code can handle this intent to present the incoming call information to the user allowing to answer or reject the call.

  4. If the call is answered, SDK will notify the app to create or update its InCall UI via your.package.name.BIT6_CALL_ADDED intent. This is done in exactly the same way as for the outgoing calls.

Steps 2 and 3 can be implemented by either (a) re-using Bit6 UI Library components or (b) implementing own solution. Sample code is available in Bit6 samples repo.

Note that in the code below assumes that the incoming call UI is displayed as an Activity but it can be easily implemented as a View or a Fragment.

(a) Use Bit6 UI Library

Add the following items to your Manifest.xml:

<!-- Register a receiver that will launch the Incoming Call Activity -->
<receiver android:name="com.bit6.ui.IncomingCallReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="your.package.name.BIT6_INCOMING_CALL" />
    </intent-filter>
</receiver>

<!-- Display basic incoming call information with 'answer' and 'reject' buttons -->
<activity android:name="com.bit6.ui.IncomingCallActivity"
    android:launchMode="singleTop"
    android:theme="@style/Theme.AppCompat.NoActionBar" />

(b) Custom Incoming Call UI

Step 1. Implement BroadcastReceiver to handle incoming call intents.

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, IncomingCallActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Send Call information to the IncomingCallActivity
        i.putExtras(intent);
        context.startActivity(i);
    }
}

Register it in Manifest.xml or in the app code.

<receiver android:name=".IncomingCallReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="your.package.name.BIT6_INCOMING_CALL" />
    </intent-filter>
</receiver>

Step 2. Implement Incoming Call UI

You can see the full source code of IncomingCallActivity in the samples repo. The information below highlights the key points:

Get call controller and display UI for answering / rejecting a call

// Get RtcDialog (call controller) for this call
RtcDialog dialog = callClient.getDialogFromIntent(getIntent());
callClient.addStateListener(this);

// Get caller identity
String caller = dialog.getOther();
// Is this a Video call
boolean video = dialog.hasVideo();

Implement CallClient.StateListener to receive call state changes - if the caller hangs up the call before the user had a chance to answer or reject it.

@Override
public void onStateChanged(RtcDialog d, int state) {
    if (state == RtcDialog.END) {
        // TODO: Close the Incoming Call UI
    }
}

Step 3.a. Reject a call

If the user clicks on a Reject button in the UI, you can decline the incoming call request.

// Reject / hangup this call
dialog.hangup();

Step 3.b. Accept the call and show In-Call UI

If the user accepts the call:

// Answer the call with or without video
dialog.answer(true);
// TODO: Close the Incoming Call UI

Once the call is accepted and Bit6 SDK processes that, it will broadcast the usual your.package.name.BIT6_CALL_ADDED intent.