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:在调用任何Tango API的函数之前,你必须做以下这些事:

  1. Declare Tango and TangoConfig objects.声明Tango和TangoConfig对象。
  2. Initialize the instance of the Tango object. This binds your app to the Tango service and defines the configuration. 初始化Tango对象实例。这将你的程序和Tango服务绑定到一起,并定义配置信息。
  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.

每一步都在下面详细描述,伴随着额外的断开服务连接的任务。代码片段是基于由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 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.

关键点:我们建议当你的程序离开活动状态时,它与Tango服务断开连接。因为当你初始化mTango时,配置和连接服务会占据Runnable对象,你应该在onStart()或onResume()回调方法中初始化mTango,然后在onPause()回调方法中断开连接。更多信息,看开始一个活动
 
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. 你不需要实现这个;绑定到服务的代码的操作在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()回调函数中,如前面建议。

Key Point: Call TangoService_disconnect from the onPause() callback method.在onPause()回调函数中调用TangoService_disconnect。

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.

上次更新日期:十二月 10, 2016

Google Tango Java SDK开发:Configure and Connect 配置和连接的更多相关文章

  1. Google Tango Java SDK开发:Motion Tracking 运动追踪

    Java API Motion Tracking Tutorial运动追踪教程 This page describes how the Java API handles motion tracking ...

  2. Apache Beam入门及Java SDK开发初体验

    1 什么是Apache Beam Apache Beam是一个开源的统一的大数据编程模型,它本身并不提供执行引擎,而是支持各种平台如GCP Dataflow.Spark.Flink等.通过Apache ...

  3. [No0000105]java sdk 开发环境变量powershell 自动配置脚本

    # 设置Java SDK 环境变量 $softwares = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Unin ...

  4. 25-ESP8266 SDK开发基础入门篇--控制WIFI连接路由器

    https://www.cnblogs.com/yangfengwu/p/11324411.html 说个事情,现在SDK的版本已经出到3.0了,但是我还是使用2.0 如果只是为了学习研究   选择3 ...

  5. Google Tango Java实例程序

    Java API:https://developers.google.com/tango/apis/java/reference/ 1. java_augmented_reality_example ...

  6. 【Java Web开发学习】Spring配置数据源

    Spring配置数据源 转载:https://www.cnblogs.com/yangchongxing/p/10027495.html =============================== ...

  7. java web开发环境tomcat安装配置

    1.下载jdk8并安装 2.下载tomcat windows环境下的免安装版zip包 3.设置两个环境变量 4.在tomcat的bin路径下双击startup.bat 启动tomcat服务器 5.使用 ...

  8. Google Tango初学者教程

    Getting Started with the Tango Java API In this tutorial, we'll go through setting up your build env ...

  9. 【linux开发】Linux下配置java环境 安装eclipse

    配置JDK环境 本文转自:http://www.cnblogs.com/fnng/archive/2013/01/30/2883815.html,有修改 下载 登录oracle的网站去下载JDK1.8 ...

随机推荐

  1. linux下配置python环境 django创建helloworld项目

    linux下配置python环境 1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstal ...

  2. IE11浏览器卸载

    点击卸载程序,然后选择查看已安装的更新. 在当前安装的更新里找到IE11的更新,然后直接右击卸载:这里告诉大家一小窍门哈,我们在搜索栏输入IE就会查找更新啦,不用一个一个去找的哦. 卸载完我们重启一下 ...

  3. Monkey记录

    1.查找包名方法 方法一: sdk里面的appt 以ES文件浏览器为例,命令行中切换到aapt.exe目录执行:aapt dump badging E:\apk\es3.apk 方法二: 查看data ...

  4. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  5. javascript 判断空数组

    javascript里判断空数组不能用 []==[] 这样来判断,因为数组也是个对象,普通对象通过指针指向的内存中的地址来做比较 所以 []==[]结果为false,因此判断数组是否为空 用 [].l ...

  6. C++ sort使用自定义函数的一些坑

    先看代码: 解释:使用自定义比较函数时,如果用了返回值恒为$true$或者恒为$false$的比较函数,就会这样子. 原因: https://stackoverflow.com/questions/4 ...

  7. 云计算与虚拟化KVM深度实践

    徐亮伟, 江湖人称标杆徐.多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作.擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作. 个人博客"徐亮伟架构师之路&quo ...

  8. RAD C++Builder xe7 std::map xtree BUG

    c++Builder 6 下的std::map还能用,有代码提示. 换到xe7,代码提示出来就一个tt.operator [](),代码没法往下写了. 最后把Target Platforms切换到64 ...

  9. Git 软件开发过程

    一.关于Git与Subversion的区别 二.目前我们用Subversion是怎么执行软件过程的 三.优势与缺点 架构 * Git:分布式,所有的teammates本地可以clone一份独立完整的仓 ...

  10. docker redis

    https://www.cnblogs.com/cgpei/p/7151612.html 重启docker >systmctl restart docker >mkdir -p ~/red ...