Configure and connect configuration and connectivity
Note:This section assumes you is familiar with the Android Activity Lifecycle. Note: This section assumes that you are already familiar with the activity cycle of Android. Overview overview
You'll need certain API calls regardless of your use case. These is related to configuring, connecting to, and disconnecting from the Tango service. Whatever your use case, you'll need some API calls.
Before calling any and functions of the Tango API, you must do the following:
- Declare
Tango
and TangoConfig
objects.
- Initialize the instance of the
Tango
object. This binds your app to the Tango service and defines the configuration.
- Begin tracking data.
Each step was detailed below, along with the additional task of disconnecting from the service. The code snippets is based on the project structures used in the tango example projects provided by Google's Tango team. We recommend that download the examples (what you can learn do on the "Getting Started with the Tango Java API" page) and T Hen examine at least one's them to get a better the idea of the "all" this works within the context of a functioning Tango app. Keep in mind that the example project structures is guides, not requirements; 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 wher E to put each operation in the Android Activity Lifecycle.
Declare Tango and Tangoconfig objects
private Tango mTango;
private TangoConfig mConfig;
Initialize the instance of the Tango object
mTango = new Tango(MainActivity.this, new Runnable(){
// Operations performed by the Runnable object
}
As you can see, a new Runnable object was 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 + detail in a moment.
Key Point:We recommend the Your app leaves the active state and it disconnects from the Tango service. Because Configuring and connecting to the service takes place in the Runnable object if 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 a activity.bind to the service
You don ' t need to implement this; The code to bind to the service are 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 Runnable
for the object, in context:
Mtango= New Tango(Mainactivity.This, New Runnable() {
@Override
Public voidRun() {
Synchronized (Mainactivity.This) {
Try {
Mconfig=Setuptangoconfig(Mtango);
Mtango.Connect(Mconfig);
Startuptango();
} Catch (TangooutofdateexceptionE) {
Log.E(TAG,GetString(R.String.Exception_out_of_date),E);
} Catch (TangoerrorexceptionE) {
Log.E(TAG,GetString(R.String.Exception_tango_error),E);
} Catch (Tangoinvalidexception E) {
log. (tag, Getstringr.. Exception_tango_invalid E }
}
}
}); Span class= "PLN" >
The mConfig
object would contain the configuration. You initialize it by calling and setupTangoConfig()
passing it the instance for you Tango
created earlier:
mConfig = setupTangoConfig(mTango);
setupTangoConfig()
in the method, you create a new TangoConfig
object, initialize it with the default configuration, and then continue to add Configuration parameters you want. Here are the full code snippet:
Private TangoconfigSetuptangoconfig(TangoTango) {
tangoconfig config Span class= "pun" >= Tango. (tangoconfig. Config_type_default
Config. (tangoconfig. Key_boolean_autorecovery, true
return Config; }
/span>
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 this configuration, call getConfig()
the method on tango
, which are the Tango
object you passed in to setupTangoConfig()
. getConfig()
returns A configuration from the Tango service, CONFIG_TYPE_DEFAULT
the default configuration, and assigns it to config
. This was the standard-of-the TangoConfig
-Initialize a object before defining custom parameters and locking that configuration.
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
. KEY_BOOLEAN_MOTIONTRACKING
true
with set to, if motion tracking enters a invalid state, it attempts to recover by immediately returning to th E initializing state in the pose lifecycle.
return config;
The config
instance returns and is assigned to 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. Before you can use the service again, the connect()
method must is called:
mTango.connect(mConfig);
This should occur if connect()
onResume()
the method is located in the callback method, as suggested earlier.
Key Point:Pager
TangoService_disconnect
From the
onPause()
callback method.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and Co De samples is licensed under the Apache 2.0 License. For details, see our Site policies. Java is a registered trademark of the Oracle and/or its affiliates.
Last updated: December 10, 2016
Google Tango Java SDK Development: Configure and connect configuration and connectivity