Skip to content

Most visited

Recently visited

navigation
DataLayer / Wearable / src / com.example.android.wearable.datalayer /

DataLayerListenerService.java

1
/*
2
 * Copyright (C) 2014 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.wearable.datalayer;
18
 
19
import android.content.Intent;
20
import android.net.Uri;
21
import android.util.Log;
22
 
23
import com.google.android.gms.common.ConnectionResult;
24
import com.google.android.gms.common.api.GoogleApiClient;
25
import com.google.android.gms.wearable.DataEvent;
26
import com.google.android.gms.wearable.DataEventBuffer;
27
import com.google.android.gms.wearable.MessageEvent;
28
import com.google.android.gms.wearable.Wearable;
29
import com.google.android.gms.wearable.WearableListenerService;
30
 
31
import java.util.concurrent.TimeUnit;
32
 
33
/**
34
 * Listens to DataItems and Messages from the local node.
35
 */
36
public class DataLayerListenerService extends WearableListenerService {
37
 
38
    private static final String TAG = "DataLayerService";
39
 
40
    private static final String START_ACTIVITY_PATH = "/start-activity";
41
    private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
42
    public static final String COUNT_PATH = "/count";
43
    public static final String IMAGE_PATH = "/image";
44
    public static final String IMAGE_KEY = "photo";
45
    GoogleApiClient mGoogleApiClient;
46
 
47
    @Override
48
    public void onCreate() {
49
        super.onCreate();
50
        mGoogleApiClient = new GoogleApiClient.Builder(this)
51
                .addApi(Wearable.API)
52
                .build();
53
        mGoogleApiClient.connect();
54
    }
55
 
56
    @Override
57
    public void onDataChanged(DataEventBuffer dataEvents) {
58
        LOGD(TAG, "onDataChanged: " + dataEvents);
59
        if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
60
            ConnectionResult connectionResult = mGoogleApiClient
61
                    .blockingConnect(30, TimeUnit.SECONDS);
62
            if (!connectionResult.isSuccess()) {
63
                Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient, "
64
                        + "error code: " + connectionResult.getErrorCode());
65
                return;
66
            }
67
        }
68
 
69
        // Loop through the events and send a message back to the node that created the data item.
70
        for (DataEvent event : dataEvents) {
71
            Uri uri = event.getDataItem().getUri();
72
            String path = uri.getPath();
73
            if (COUNT_PATH.equals(path)) {
74
                // Get the node id of the node that created the data item from the host portion of
75
                // the uri.
76
                String nodeId = uri.getHost();
77
                // Set the data of the message to be the bytes of the Uri.
78
                byte[] payload = uri.toString().getBytes();
79
 
80
                // Send the rpc
81
                Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, DATA_ITEM_RECEIVED_PATH,
82
                        payload);
83
            }
84
        }
85
    }
86
 
87
    @Override
88
    public void onMessageReceived(MessageEvent messageEvent) {
89
        LOGD(TAG, "onMessageReceived: " + messageEvent);
90
 
91
        // Check to see if the message is to start an activity
92
        if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
93
            Intent startIntent = new Intent(this, MainActivity.class);
94
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95
            startActivity(startIntent);
96
        }
97
    }
98
 
99
    public static void LOGD(final String tag, String message) {
100
        if (Log.isLoggable(tag, Log.DEBUG)) {
101
            Log.d(tag, message);
102
        }
103
    }
104
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)