Courtesy: GitHub

Google APIs for Mobile: Quickstarts

Demonstrates use of long-lived background subscription with the Nearby.Messages API to scan for beacons using Bluetooth Low Energy (BLE).

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.nearbybackgroundbeacons for the package name.

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

Discovering Beacons

By default, background subscription discovers all messages published by this application and other applications in the same Google Developers Console project. When attaching messages to a beacon, you must be authenticated as a certain Developer Console project; if you then use the API key associated with that project in this app, you should find all the messages you attached.

Support

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

For providing feedback about Nearby.Messages, use the SEND FEEDBACK link on the Nearby Messages API for Android page.

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.nearbybackgroundbeacons"

minSdkVersion 9

targetSdkVersion 23

versionCode 1

versionName "1.0"

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

testCompile 'junit:junit:4.12'

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

compile 'com.android.support:design:23.3.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.nearbybackgroundbeacons

uses-permission

  • android.permission.ACCESS_FINE_LOCATION

MainActivity

Copyright 2016 Google Inc. All Rights Reserved.

Licensed 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.

The entry point to Google Play Services.

The container {@link android.view.ViewGroup} for the minimal UI associated with this sample.

Tracks subscription state. Set to true when a call to {@link Messages#subscribe(GoogleApiClient, MessageListener)} succeeds.

Adapter for working with messages from nearby beacons.

Backing data structure for {@code mNearbyMessagesArrayAdapter}.

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}.

Calls {@link Messages#subscribe(GoogleApiClient, MessageListener, SubscribeOptions)}, using a {@link Strategy} for BLE scanning. Attaches a {@link ResultCallback} to monitor whether the call to {@code subscribe()} succeeded or failed.

Displays {@link Snackbar} instructing user to visit Settings to grant permissions required by this application.

Displays {@link Snackbar} with button for the user to re-initiate the permission workflow.

BackgroundSubscribeIntentService

Copyright 2016 Google Inc. All Rights Reserved.

Licensed 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.

While subscribed in the background, this service shows a persistent notification with the current set of messages from nearby beacons. Nearby launches this service when a message is found or lost, and this service updates the notification, then stops itself.