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. 对类 sizeof

    sizeof一个类的时候,都什么会被计算?静态成员会被计算进来么?如果这是一个子类,它的父类成员会被计算么? #include <iostream> using namespace std ...

  2. mac下搭建appium记录

    要安装的东西: jdk(要配置环境) , sdk(要配置环境) ,node(要配置环境), python(要配置环境) ,appium(要配置环境),appium-python-client ,xco ...

  3. Python基础之爬虫(持续更新中)

    python通过urllib.request.urlopen("https://www.baidu.com")访问网页 实战,去网站上下载一只猫的图片 import urllib. ...

  4. hadoop学习day1环境配置笔记(非完整流程)

    hdfs的工作机制: 1.客户把一个文件存入hdfs,其实hdfs会把这个文件切块后,分散存储在N台linux机器系统中(负责存储文件块的角色:data node)<准确来说:切块的行为是由客户 ...

  5. Spring Cloud Eureka的基础架构

    基础架构 服务注册中心:Eureka提供的服务端,提供服务注册于发现的功能,也就是在上一节中我们实现的eureka-server 服务提供者:提供服务的应用,可以是springBoot应用,也可以是其 ...

  6. 现象级AR营销助力“口碑双十二”,蚂蚁特工在全国数万商户掀起“AR捉四宝”

    领取阅读奖励金 今年双十二,全国人民吃喝玩乐放飞自我,嗨出了新纪元.除了见证你国人民的财力,这个“双十二”还诞生了教科书级的“AR营销”.无论是在口碑商户门口,还是在各大购物广场,都能看到举着手机,正 ...

  7. CImage 往Picture Control贴图 图像显示不正常

    在使用CImage 往vc控件 picture Control 上贴图的时候图像显示不太正常如图: 已知原始图片的宽高为640*640  而我上面picture Control  控件宽高小于原始图像 ...

  8. Node.js 项目打包

    Node项目基于Electron打包 Electron打包打包后项目.exe程序包含在文件夹中,基于Electron打包的基础上直接打包成exe程序 参考一 参考二 需求的软件环境: NSIS 2.4 ...

  9. Notepad++中的高级查找

      准备以下字符串用来演示 abcdeab cdeabcde abcd eabcde   基于扩展的查找 基于扩展的查找不能算是真正的正则表达式搜索,因此这种查找方式仅是提供了支持转义字符.主要常用的 ...

  10. 迷你MVVM框架 avalonjs 1.3.3发布

    大家可以在仓库中看到,多出了一个叫avalon.observe的东西,它是基于Object.observe,dataset, Promise等新API实现.其中,它也使用全新的静态收集依赖的机制,这个 ...