Configure and Connect 配置和连接
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:在调用任何Tango API的函数之前,你必须做以下这些事:
- Declare
Tango
andTangoConfig
objects.声明Tango和TangoConfig对象。 - Initialize the instance of the
Tango
object. This binds your app to the Tango service and defines the configuration. 初始化Tango对象实例。这将你的程序和Tango服务绑定到一起,并定义配置信息。 - 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.
每一步都在下面详细描述,伴随着额外的断开服务连接的任务。代码片段是基于由Goole Tango团队提供的用在Tango例子项目中的项目结构。我建议你下载这些例子然后至少分析一个来获取更好的理解。记住例子项目结构是指导,而非要求;你可以自由地组织你的项目。
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 声明Tango和TangoConfig对象
private Tango mTango;
private TangoConfig mConfig;
Initialize the instance of the Tango object 初始化Tango对象
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.正如你所看到的,一个新的Runnable对象被创建并被作为一个参数传递。Runnable对象包含定义配置信息并连接到服务的代码。过会你将更详细地测试这些操作。
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 initializemTango
, 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.You don't need to implement this; the code to bind to the service is in the constructor for the Tango
object. 你不需要实现这个;绑定到服务的代码的操作在Tango对象的构造器中实现了。
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:在应用绑定到服务之后,Runnable对象运行在一个新的线程上。这里是Runnable对象的完整代码片段。
mTango = new Tango(MainActivity.this, new Runnable() {
@Override
public void run() {
synchronized (MainActivity.this) {
try {
mConfig = setupTangoConfig(mTango); //设置配置信息
mTango.connect(mConfig); //mTango根据配置信息连接
startupTango(); //启动Tango
} 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()函数然后传递前面创建的Tango实例来初始化它。
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: 在setupTangoConfig()函数中,你创建一个新的TangoConfig对象,并用默认的配置信息初始化它,然后继续添加你想要的配置参数。这里是完整的代码片段:
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.创建一个新的TangoConfig对象,然后使用默认的配置初始化它。为了得到该配置,调用tango中的getConfig()方法,它是你传递到setupTangoConfig()中的tango对象。getConfig()返回一个来自Tango服务(该例中CONFIG_TYPE_DEFAULT是默认的配置)的配置信息,然后将它赋值给config。这是在定义自定义参数和锁定该配置信息之前初始化一个TangoConfig对象的标准方式。
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.现在你可以添加其他的配置参数,如这一个所示。putBoolean()方法添加一个布尔类型的参数到config。将KEY_BOOLEAN_MOTIONTRACKING设为true,如果运动追踪输入一个无效状态,它将通过快速返回到位姿生命周期的初始状态来恢复。
return config;
The config
instance returns and is assigned to mConfig
.该config实例返回,并且赋值给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.disconnect()。这会释放Tango服务让其他应用使用。在你再次使用该服务之前,connect()方法必须被调用:
mTango.connect(mConfig);
This should occur if the connect()
method is located in the onResume()
callback method, as suggested earlier.如果connect()函数定位在onResume()回调函数中,如前面建议。
TangoService_disconnect
from the onPause()
callback method.在onPause()回调函数中调用TangoService_disconnect。