Google Tango API之Configure and C

2017-01-05  本文已影响56人  60916fc63567

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.

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 theTangoobject. This binds your app to the

Tango serviceand defines the configuration.

3.Begin tracking data.

您将需要某些API调用,而不考虑您的用例。这些是相关的

以配置,连接和断开Tango服务。

在调用Tango API的任何其他函数之前,必须执行以下操作:

1.Declare Tango和TangoConfig对象。

2.初始化Tangoobject的实例。这会将您的应用程式与

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

Initialize the instance of the Tango 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, itdisconnects from the Tango service. Because configuring and connecting to theservice takes place in the Runnable object when you initializemTango, youshould initializemTangoin either theonStart()oronResume()callbackmethod, and disconnect from the service in theonPause()callback method. Formore information, seeStarting 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:

应用程序绑定到服务后,Runnable对象在新线程上运行。这里是Runnable对象的完整代码片段,在上下文中:

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

Config对象将包含配置。您可以通过调用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 ontango, which is the Tango object you passed in to setupTangoConfig().getConfig() returns a configuration from the Tangoservice (in this case,CONFIG_TYPE_DEFAULT,the default configuration) and assigns it toconfig. This is the standard wayto initialize aTangoConfigobject before defining custom parameters andlocking that configuration.

创建一个新的TangoConfig对象并使用默认配置初始化它。要获得该配置,请调用tango上的getConfig()方法,这是您传递到setupTangoConfig()的Tango对象。getConfig()从Tango服务(在这种情况下,CONFIG_TYPE_DEFAULT,默认配置)返回配置并将其分配给配置。这是在定义自定义参数并锁定该配置之前初始化TangoConfig对象的标准方法。

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时,如果运动跟踪进入无效状态,它将尝试通过立即返回到姿势生命周期中的初始化状态来恢复。

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:

调用mTango.disconnect()。这释放了Tango服务以供其他应用程序使用。在可以再次使用服务之前,必须调用connect()方法:

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

上一篇下一篇

猜你喜欢

热点阅读