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调用,而不考虑您的用例。 这些与配置,连接和断开Tango服务有关。
Before calling any other functions of the Tango API, you must do the following:
1.DeclareTangoandTangoConfigobjects.
2.Initialize the instance of theTangoobject. Thisbinds your app to the Tango serviceand defines the configuration.
3.Begin tracking data.
在调用Tango API的任何其他函数之前,必须执行以下操作:
1、声明Tango和TangoConfig对象。
2、初始化Tango对象的实例。 这会将您的应用程序绑定到Tango服务并定义配置。
3、开始跟踪数据。
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.
下面详细说明每个步骤,以及断开与服务的其他任务。 代码段基于Google Tango团队提供的Tango示例项目中使用的项目结构。 我们建议您下载示例(您可以了解如何使用“Tango Java API入门”页面),然后检查其中至少一个,以更好地了解所有这些在功能Tango的上下文中如何工作 app。 请记住,示例项目结构是指南,而不是要求; 你可以自由地构造你的项目,但你认为合适。
因为这里详细介绍的操作涉及应用程序如何启动,运行和退出,“关键点”建议将每个操作放置在Android Activity Lifecycle中。
Declare Tango and TangoConfig objects
定义Tango和TangoConfig objects
privateTangomTango;
privateTangoConfigmConfig;
Initialize the instance of the Tango object
初始化Tango对象的实例
mTango=newTango(MainActivity.this,newRunnable(){
// 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 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.
要点:我们建议,当应用程序退出Activity时,它将与Tango服务断开连接。 因为在初始化mTango时在Runnable对象中配置和连接服务,所以应该在onStart()或onResume()回调方法中初始化mTango,并在onPause()回调方法中断开服务。 有关更多信息,请参阅启动Activity。
Bind to the service 绑定到服务
You don't need to implement this; the code to bind to the service is in the constructor for theTangoobject.
你不需要实现这个; 绑定到服务的代码在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 theRunnableobject, in context:
定义配置
应用程序绑定到服务后,Runnable对象在新线程上运行。 这里是Runnable对象的完整代码片段,在上下文中:
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实例来初始化它:
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对象,使用默认配置初始化它,然后继续添加所需的配置参数。 这里是完整的代码片段:
This method works as follows:
此方法的工作原理如下:
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,默认配置)返回配置并将其分配给配置。 这是在定义自定义参数并锁定该配置之前初始化TangoConfig对象的标准方法。
Now you can add other configuration parameters, such as this one. TheputBoolean()method adds a boolean parameter toconfig. WithKEY_BOOLEAN_MOTIONTRACKINGset totrue, 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时,如果运动跟踪进入无效状态,它将尝试通过立即返回到姿势生命周期中的初始化状态来恢复。
The config instance returns and is assigned to mConfig.
配置实例返回并分配给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:
调用mTango.disconnect()。 这释放了Tango服务以供其他应用程序使用。 在可以再次使用服务之前,必须调用connect()方法:
This should occur if theconnect()method is located in theonResume()callback method, as suggested earlier.
Key Point:Call TangoService_disconnect from the onPause() callback method.