首页 > 代码库 > Google Tango Java SDK开发:Configure and Connect 配置和连接

Google Tango Java SDK开发:Configure and Connect 配置和连接

Configure and Connect 配置和连接

Note: This section assumes you are familiar with the Android Activity Lifecycle. 注意:该节假设你已经熟悉了安卓的活动周期。

Overview 概览

You will need certain API calls regardless of your use case. These are related to configuring, connecting to, and disconnecting from the Tango service.无论你是什么使用案例,你都需要某些API调用。

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.

Key Point: We recommend that when your app leaves the active state, it disconnects from the Tango service. Because configuring and connecting to the service takes place in the Runnable object when you initialize mTango, you should initialize mTango in either the onStart() or onResume() callback method, and disconnect from the service in the onPause() callback method. For more information, see Starting an Activity.

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() {
    @Override
    public void run() {
        synchronized (MainActivity.this) {
            try {
                mConfig = setupTangoConfig(mTango);
                mTango.connect(mConfig);
                startupTango();
            } 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

 
mTango.connect(mConfig);

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:

 
mTango.connect(mConfig);

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

Key Point: Call TangoService_disconnect from the onPause() callback method.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Google Tango Java SDK开发:Configure and Connect 配置和连接