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. Ubuntu14.04下Sublime Text 3解决无法输入中文

    在Ubuntu 14.04中安装了SublimeText 3之后发现既然不支持输入中文,于是在网上搜罗一下,发现很多人遇到了同样的问题,但是解决办法大该就只有一个.下面根据自身的安装及解决办法总结如下 ...

  2. 马士兵Spring-声明式事务管理-XML

    1.com.cy.model中User.java  和 Log.java 实体 和上一节一样: 2.DAO的实现类com.cy.dao.impl中的UserDAOImpl.LogDAOImpl.jav ...

  3. Bootstrap-CL:进度条

    ylbtech-Bootstrap-CL:进度条 1.返回顶部 1. Bootstrap 进度条 本章将讲解 Bootstrap 进度条.在本教程中,您将看到如何使用 Bootstrap 创建加载.重 ...

  4. 面试总结之Linux/Shell

    Linux Linux cshrc文件作用 Linux如何起进程/查看进程/杀进程 Linux 文件755 代表什么权限 Linux辅助线程 Linux进程间通信方法 pipeline,msgq... ...

  5. 条件随机场(CRF)-IIS学习算法

    改进的迭代尺度法(Improved Iterative Scaling),在很多模型求解中用到,比如最大熵.CRFs等,对模型是对数线性模型的似然都适用.这个算法的思想也很简单,通俗的理解就是通过两个 ...

  6. 十一.jQuery源码解析之.pushStack()

    pushStack()顾明思意,就是像桟中添加东西呗,现在看看他是如何添加东西的. 创建一个空的jQuery对象,然后把Dom元素集合放入这个jQuery对象中, 并保留对当前jQuery对象的引用. ...

  7. SqlServer快速获得表总记录数(大数据量)

    --第1种 执行全表扫描才能获得行数 SELECT count(*) FROM BUS_tb_UserGradePrice --第2种 执行扫描全表id不为空的,获得行数 select count(u ...

  8. 通过日志查看Web Api详细运行过程

    1. 通过Nuget安装System.Web.Http.Tracing. 2. 通过HttpConfiguration,注册SystemDiagnosticsTraceWriter public st ...

  9. 网络通信和TCP详解

    交换机.路由器.服务器组网 1. 通信过程(pc+switch+router+server) 较为复杂的通信过程如:访问 www.baidu.com 注意:一定要配置 PC:IP.NETMASK.DF ...

  10. 使用WebLogic时控制台输出中文乱码解决方法

    使用WebLogic时控制台输出中文乱码解决方法 1.找到weblogic安装目录,当前项目配置的domain 2.找到bin下的setDomainEnv.cmd文件 3.打开文件,从文件最后搜索第一 ...