
Android Wear Docs, Release 1.1
the buffer, convert it to a DataMapItem, convert that to a DataMap object, and then get the original handheld data.
A WearableListenerService is not the only way to receive data, but it is easy to implement because Android Wear
manages its life-cycle.
7.1 First Wearable Data
If you have not already done so, Create a Project . The new project wizard in Android Studio beta creates a project
with two main activities, one for the handheld device and another for the wearable. These two activities use the same
package name, which is essential for the wearable data layer to work.
Data layer transfers can originate from either a handheld or wearable. For bidirectional data both the handheld and
wearable should implement the code in this section.
7.1.1 Add Metadata for Google Play Services
Add Google Play services metadata statement to the manifest of the sending device:
<application>
...
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
7.1.2 Add a Data Sender
To send a data object , update the code in the main Activity of the sending device.
1. Build a Google Play Services client that includes the Wearable API.
public class Handheld extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient googleClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handheld);
// Build a new GoogleApiClient
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Data layer and lifecycle implementation (Step 2)
...
}
2. Add callback methods for the data layer and lifecycle events. For simplicity, send a data object in the onCon-
nected callback method. In this example the path of the data object is “wearable_data” and the data is a DataMap
32 Chapter 7. Data Layer DataMap Objects
Comentarios a estos manuales