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. RDLC报表系列二

    ---------------------ReportServer数据库权限表说明------------------ --用户表 select * from Users --角色表 select * ...

  2. bzoj 4885: [Lydsy2017年5月月赛]长方体

    Description 给定一个a*b*c的长方体,定义其表面上两个点的距离为沿着长方体的表面走的最短路径的长度,请找到距离最远的点对,你需要保证找到的两个点里至少有一个是长方体顶点. Input 第 ...

  3. 学习笔记之C# 教程 | 菜鸟教程

    C# 教程 | 菜鸟教程 http://www.runoob.com/csharp/csharp-tutorial.html 菜鸟教程在线编辑器 http://www.runoob.com/try/r ...

  4. python3.3.5x64+win2003x64+aliyun oss sdk安装步骤

    参考文章:https://help.aliyun.com/document_detail/32026.html?spm=5176.doc31890.6.690.S6ZrRn 1.安装python3.3 ...

  5. php ini_set更改php.ini配置,通过它修改php.in达到php上传文件大小限制是不行的,除非修改.htaccess文件

    PHP ini_set() 无效的原因:如题:我租的是虚拟主机,php.ini里的upload_max_filesize是默认的2M,要直接修改php.ini文件是不可能的.我如果想上传超过2M的怎么 ...

  6. 详解Oracle的几种分页查询语句

    转载自:http://database.51cto.com/art/200904/118737.htm 分页查询格式: SELECT * FROM (SELECT A.*, ROWNUM RN FRO ...

  7. 6.1课堂笔记—DML(数据操作语言),DQL查询语句

    一.DML(数据操作语言) InnoDB MyISAM 支持事务 不支持事务 不支持全文索引 支持全文索引 支持外键约束 不支持 命令查看默认存储引擎 show variables like '%st ...

  8. 【POJ】1062 昂贵的聘礼 (最短路)

    题目 传送门:QWQ 分析 最短路显然,但不好搞地位等级..... 地位等级不好搞?那么就暴力.. 枚举我们允许的地位等级,跑最短路. 所以$ n^2logn $出100什么鬼啊,很有迷惑性啊 还有4 ...

  9. 修改eclipse 代码字体大小以及文档字体大小

    1..点击[window]在弹出的窗口中选择[preferences] 2.在弹出窗口中找到依次点击General(常规)——Apprearance(外观)——Colors and Fonts(颜色和 ...

  10. 「小程序JAVA实战」小程序数据缓存API(54)

    转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxushujuhuancunapi52/ 刚开始写小程序的时候,用户信息我 ...