Courtesy: GitHub

Google APIs for Mobile: Quickstarts

Demonstrates use of the Nearby.Messages API for communicating between devices within close proximity of each other.

Introduction

This sample allows a user to publish a message to nearby devices, and subscribe to messages published by those devices.

To run this sample, use two or more devices to publish and subscribe messages.

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.

To use this sample, follow the following steps:

  1. Create a project on Google Developer Console. Or, use an existing project.

  2. Click on APIs & auth -> APIs, and enable Nearby Messages API.

  3. Click on Credentials, then click on Create new key, and pick Android key. Then register your Android app's SHA1 certificate fingerprint and package name for your app. Use com.google.android.gms.nearby.messages.samples.nearbydevices for the package name.

  4. Copy the API key generated, and paste it in AndroidManifest.xml.

Support

If you've found an error in these samples, please file an issue in this repo.

Patches are encouraged, and may be submitted according to the instructions in CONTRIBUTING.md.

How to make contributions?

Please read and follow the steps in the CONTRIBUTING.md

License

See LICENSE

compileSdkVersion 23

applicationId "com.google.android.gms.nearby.messages.samples.nearbydevices"

minSdkVersion 9

targetSdkVersion 23

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:23.4.0'

compile 'com.android.support:design:23.4.0'

compile 'com.google.code.gson:gson:2.4'

compile 'com.google.android.gms:play-services-nearby:9.0.0'

package com.google.android.gms.nearby.messages.samples.nearbydevices

MainActivity

An activity that allows a user to publish device information, and receive information about nearby devices.

The UI exposes a button to subscribe to broadcasts from nearby devices, and another button to publish messages that can be read nearby subscribing devices. Both buttons toggle state, allowing the user to cancel a subscription or stop publishing.

This activity demonstrates the use of the {@link Messages#subscribe(GoogleApiClient, MessageListener, SubscribeOptions)}, {@link Messages#unsubscribe(GoogleApiClient, MessageListener)}, {@link Messages#publish(GoogleApiClient, Message, PublishOptions)}, and {@link Messages#unpublish(GoogleApiClient, Message)} for foreground publication and subscription.

a We check the app's permissions and present an opt-in dialog to the user, who can then grant the required location permission.

Using Nearby for in the foreground is battery intensive, and pub-sub is best done for short durations. In this sample, we set the TTL for publishing and subscribing to three minutes using a {@link Strategy}. When the TTL is reached, a publication or subscription expires.

Sets the time in seconds for a published message or a subscription to live. Set to three minutes in this sample.

Creates a UUID and saves it to {@link SharedPreferences}. The UUID is added to the published message to avoid it being undelivered due to de-duplication. See {@link DeviceMessage} for details.

The entry point to Google Play Services.

The {@link Message} object used to broadcast information about the device to nearby devices.

A {@link MessageListener} for processing messages from nearby devices.

Adapter for working with messages from nearby publishers.

Builds {@link GoogleApiClient}, enabling automatic lifecycle management using {@link GoogleApiClient.Builder#enableAutoManage(FragmentActivity, int, GoogleApiClient.OnConnectionFailedListener)}. I.e., GoogleApiClient connects in {@link AppCompatActivity#onStart}, or if onStart() has already happened, it connects immediately, and disconnects automatically in {@link AppCompatActivity#onStop}.

Subscribes to messages from nearby devices and updates the UI if the subscription either fails or TTLs.

Publishes a message to nearby devices and updates the UI if the publication either fails or TTLs.

Stops subscribing to messages from nearby devices.

Stops publishing message to nearby devices.

Logs a message and shows a {@link Snackbar} using {@code text}; * @param text The text used in the Log message and the SnackBar.