Android Second Screen Client Quickstart

The Red5 Pro Second Screen Android SDK provides the ability to add Second Screen experiences to your Native Applications on Androir devices.

Download the Red5 Pro Android Second Screen SDKs Download the latest
Second Screen Native Client SDK for Android

Included in the Red5 Pro Android Second Screen SDK is an example project with Android Studio support.


Requirements

The Red5 Pro Android Second Screen SDK has a minimum support for Android 4.1 / API Level: 16.


Basic Setup

Once you have downloaded the Android SDK and the Red5 Pro iOS Second Screen SDK, open Xcode and start a create a new project or open an existing project you want to add Second Screen to. To add the Red5 Pro Second Screen libraries to your project:

Adding the Framework and Dependencies

Defining the Manifest Requirements

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<service android:name="com.infrared5.secondscreen.client.SecondScreenService"
    android:exported="false"/>

Configuration

The Second Screen Client must be configured before it is used. This is most simply done in the the Application's onCreate method. The registry server hostname and red5 application name are required, and other things can be optionally configured.

import com.infrared5.secondscreen.client.SecondScreen;
import com.infrared5.secondscreen.client.SecondScreenConfig;

import android.app.Application;

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();

    SecondScreenConfig config = SecondScreenConfig.createDefault();
    config.setRegistryHostname("REGISTRY_HOSTNAME_HERE");
    config.setRed5AppName("secondscreen");
    SecondScreen.init(config);
  }
}

Binding to the service

The SecondScreenService does the core of the work of the second screen client. While an activity is bound to it, it will maintain a connection to the registry server and to any host that it is connected to. One binds to it by creating a subclass of the SecondScreenConnection class, and overriding the onBound and onUnbound methods. One should add any needed listeners in onBound and remove them in onUnbound. The simplest way to bind the connection is to call bind in the activity's onStart method, and call unbind in onStop.

// in sample activity
private final SecondScreenConnection mConnection = new SecondScreenConnection() {
    @Override
    protected void onBound(SecondScreenService service) {
        // add listeners here
    }

    @Override
    protected void onUnbound(SecondScreenService service) {
        // remove listeners here
    }
};

@Override
protected void onStart() {
    super.onStart();
    mConnection.bind(this);
}

@Override
protected void onStop() {
    super.onStop();
    mConnection.unbind(this);
}

Connecting to a Host

Once it is bound, the service will begin listening for available hosts. One can register a HostListListener to be notified when the list of available hosts changes. Once you have a host that you want to connect to, call the call the connectToHost method.

SecondScreenService service = mConnection.getService();
service.connectToHost(host);

To be notified when a host connects, register a ConnectListener with the service. It will be notified when a Host connects, or fails to connect with an error. A DisconnectListener is notified when the host disconnects.


Binding to second screen view

The SecondScreenView is responsible for loading and displaying the controls specified by the host. A second screen view can be created in the layout xml, or programmatically through code. The xml allows one to specify the layout to use when loading the controls. If no loading layout is specified, none will be used.

<com.infrared5.secondscreen.client.SecondScreenView
    xmlns:secondscreen="http://infrared5.com/secondscreen"
    android:id="@+id/control_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    secondscreen:loading_layout="@layout/loading"/>

Once connected to a host, one can bind the view to the host session very easily:

SecondScreenView view = (SecondScreenView)findViewById(R.id.control_content);
session.bindToView(view);

Rpc Handling

Once connected to a host, one can also send rpc calls to and from the host. To send rpc calls to the host, simply call the sendRpc method on the HostSession. It takes the name of the method, and any combination of string, numeric, or boolean arguments.

session.sendRpc("foo");
session.sendRpc("testing", 1, 2, 3);
session.sendRpc("enableRobot", true);
session.sendRpc("seenAdjective", "spooky");

One handles rpc calls from the host by specifying a RpcTarget. All rpc methods on the target must be public and have the @RpcMethod annotation.

class TestRpcHandler implements RpcTarget {
    @RpcMethod
    public void foo() {
        // use your imagination
    }

    @RpcMethod
    public void testing(int a, int b, float c) {
        // use your imagination
    }

    @RpcMethod
    public void enableRobot(boolean enabled) {
        // use your imagination
    }

    @RpcMethod
    public void seenAdjective(String adjective) {
        // use your imagination
    }
}

// later on...
session.setRpcHandler(new TestRpcHandler());

API Documentation

For more in-depth documentation about the structure of the Android Second Screen Native Client SDK, please visit: Android Second Screen API Documentation


Native Client Example

We have created and shared an example of an Android Native Client project that incorporates the Live Streaming and Second Screen SDKS.

Download the Android Red Pro Native Client Example Download the latest
Native Client Example for Android