Configure and Connect


You will need certain API calls regardless of your use case. These are related to configuring, connecting to, and disconnecting from the Tango service.

Before calling any other functions of the Tango API, you must do the following:

  1. Declare Tango and TangoConfig objects.
  2. Initialize the instance of the Tango object. This binds your app to the Tango service and defines the configuration.
  3. Begin tracking data.

Each step is detailed below, along with the additional task of disconnecting from the service. The code snippets are based on the project structures used in the Tango example projects provided by Google's Tango team. We recommend that you download the examples (you can learn how on the "Getting Started with the Tango Java API" page) and then examine at least one of them to get a better idea of how all this works within the context of a functioning Tango app. Keep in mind that the example project structures are guides, not requirements; you are free to structure your project however you see fit.

Because the operations detailed here relate to how your application starts, runs, and exits, the "Key Points" suggest where to put each operation in the Android Activity Lifecycle.

Declare Tango and TangoConfig objects

private Tango mTango;
private TangoConfig mConfig;

Initialize the instance of the Tango object

mTango = new Tango(MainActivity.this, new Runnable(){
  // Operations performed by the Runnable object

As you can see, a new Runnable object is created on the spot and passed as an argument. The Runnable object contains the code that defines the configuration and connects the app to the service. You'll examine those operations in more detail in a moment.

Bind to the service

You don't need to implement this; the code to bind to the service is in the constructor for the Tango object.

Define the configuration

After the app binds to the service, the Runnable object runs on a new thread. Here's the full code snippet for the Runnable object, in context:

mTango = new Tango(MainActivity.this, new Runnable() {
    public void run() {
        synchronized (MainActivity.this) {
            try {
                mConfig = setupTangoConfig(mTango);
            } catch (TangoOutOfDateException e) {
                Log.e(TAG, getString(R.string.exception_out_of_date), e);
            } catch (TangoErrorException e) {
                Log.e(TAG, getString(R.string.exception_tango_error), e);
            } catch (TangoInvalidException e) {
                Log.e(TAG, getString(R.string.exception_tango_invalid), e);

The mConfig object will contain the configuration. You initialize it by calling setupTangoConfig() and passing it the instance of Tango you created earlier:

mConfig = setupTangoConfig(mTango);

In the setupTangoConfig() method, you create a new TangoConfig object, initialize it with the default configuration, and then continue to add configuration parameters you want. Here is the full code snippet:

private TangoConfig setupTangoConfig(Tango tango) {
    TangoConfig config = tango.getConfig(TangoConfig.CONFIG_TYPE_DEFAULT);
    config.putBoolean(TangoConfig.KEY_BOOLEAN_AUTORECOVERY, true);
    return config;

This method works as follows:

TangoConfig config = tango.getConfig(TangoConfig.CONFIG_TYPE_DEFAULT);

Create a new TangoConfig object and initialize it with the default configuration. To get that configuration, call the getConfig() method on tango, which is the Tango object you passed in to setupTangoConfig(). getConfig() returns a configuration from the Tango service (in this case, CONFIG_TYPE_DEFAULT, the default configuration) and assigns it to config. This is the standard way to initialize a TangoConfig object before defining custom parameters and locking that configuration.

config.putBoolean(TangoConfig.KEY_BOOLEAN_MOTIONTRACKING, true);

Now you can add other configuration parameters, such as this one. The putBoolean() method adds a boolean parameter to config. With KEY_BOOLEAN_MOTIONTRACKING set to true, if motion tracking enters an invalid state, it attempts to recover by immediately returning to the initializing state in the pose lifecycle.

return config;

The config instance returns and is assigned to mConfig.

Begin tracking data


The data is available through polling and callbacks.

Disconnect from the service

Call mTango.disconnect(). This frees the Tango service for other applications to use. Before you can use the service again, the connect() method must be called:


This should occur if the connect() method is located in the onResume() callback method, as suggested earlier.

Enviar comentarios sobre…