Courtesy: GitHub

Android DataLayer Sample

This sample demonstrates how to work with a WearableListenerService, to produce and consume DataEvents and effectively work with the DataLayer.

Introduction

This sample demonstrates how to make a handheld and an Android Wear device communicate using the DataApi. It does this by sending a picture between connected devices.

An Activity is being used for both the connected devices which implement their parts of the required interfaces.

It showcases how to use an WearableListenerService to consume DataEvents as well as implementations for various required listeners when using the DataApi, MessageApi and NodeApi.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-DataLayer

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:support-v13:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compile 'com.google.android.gms:play-services-wearable:11.4.0'

compile 'com.android.support:support-v13:26.1.0'

compileSdkVersion 26

minSdkVersion 18

targetSdkVersion 25

compile 'com.google.android.gms:play-services-wearable:11.4.0'

compile 'com.android.support:support-v13:26.1.0'

compile 'com.google.android.support:wearable:2.0.5'

compileSdkVersion 26

versionCode 1

versionName "1.0"

minSdkVersion 18

targetSdkVersion 25

package com.example.android.wearable.datalayer

package com.example.android.wearable.datalayer

MainActivity

Receives its own events using a listener API designed for foreground activities. Updates a data item every second while it is open. Also allows user to take a photo and send that as an asset to the paired wearable.

Sets up UI components and their callback handlers.

Sends an RPC to start a fullscreen Activity on the wearable.

Dispatches an {@link android.content.Intent} to take a photo. Result will be returned back in onActivityResult().

Builds an {@link com.google.android.gms.wearable.Asset} from a bitmap. The image that we get back from the camera in "data" is a thumbnail size. Typically, your image should not exceed 320x320 and if you want to have zoom and parallax effect in your app, limit the size of your image to 640x400. Resize your image before transferring to your wearable device.

Sends the asset that was created from the photo we took by adding it to the Data Item store.

As simple wrapper around Log.d

A View Adapter for presenting the Event objects in a list

Generates a DataItem based on an incrementing count.

MainActivity

The main activity with a view pager, containing three pages:

  • Page 1: shows a list of DataItems received from the phone application
  • Page 2: shows the photo that is sent from the phone application
  • Page 3: includes two buttons to show the connected phone and watch devices

Find the connected nodes that provide at least one of the given capabilities

Switches to the page {@code index}. The first page has index 0.

DataLayerListenerService

DataLayerListenerService

Listens to DataItems and Messages from the local node.