Get Started

This document explains how to start developing with the Nearby Messages API on Android. The Nearby Messages API is part of Google Play services.

Step 1: Get Google Play services

The Nearby Messages API is available on Android devices with Google Play services 7.8.0 or higher. Devices running Android 2.3 or higher that have the Google Play Store app automatically receive updates to Google Play services. To check which version of Google Play services is installed on your device, go to Settings > Apps > Google Play services.

Ensure that you have the latest client library for Google Play services on your development host:

  1. Open the Android SDK Manager.
  2. Under Appearance & Behavior > System Settings > Android SDK > SDK Tools, ensure that the following packages are installed:

    • Google Play services
    • Google Repository

Step 2: Get a Google account

To use the Nearby Messages APIs, you need a Google Account. This is so that you, the developer, can enable the Nearby API in the next step (your users will not need to have a Google account). If you already have an account, then you're all set. You may also want a separate Google Account for testing purposes.

Step 3: Get an API key

Take these steps to enable the Google Nearby Messages API for Android and get an API key:

  1. Go to the Google Developers Console.
  2. Create or select a project to register your application with.
  3. Click Continue to Enable the API.
  4. On the Credentials page, create a new Android key (and set the API Credentials).
    Note: If you have an existing Android key, you may use that key.
  5. In the resulting dialog, enter your app's SHA-1 fingerprint and package name. For example:
    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
    com.example.android.nearbyexample
  6. Your new Android API key appears in the list of API keys for your project. An API key is a string of characters, something like this:
    AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

Obtain the SHA1 fingerprint of your certificate

To create a new API key for your Android app, you need the SHA1 fingerprint of the certificate you use to sign your APK. Messages can only be exchanged between apps that are signed with API keys from the same project.

To obtain this fingerprint:

  1. Find the location of your keystore.
  2. In a terminal, run the keytool utility from the JDK. For example, if you are using the `debug` keystore:
  3. $ keytool -exportcert -alias \
    androiddebugkey -keystore \
    ~/.android/debug.keystore -list -v
    

    Note: For the debug keystore, the password is android. On Mac OS and Linux, the debug keystore is typically located at ~/.android/debug.keystore. On Windows, it is typically located at %USERPROFILE%\.android\debug.keystore.

  4. The output of the keytool command contains the SHA1 fingerprint for the certicate.

Step 4: Configure your project

Android Studio makes it easy to create a project for the Nearby Messages API. Follow the steps described in Creating a Project to create a new project. In Android Studio, open the build.gradle file for your module and add the Google Play services client library as a dependency:

apply plugin: 'android'
...

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

Then, configure your manifest with the API Key generated in the previous step:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.sample.app" >
    <application ...>
        <meta-data
            android:name="com.google.android.nearby.messages.API_KEY"
            android:value="API_KEY" />
        <activity>
        ...
        </activity>
    </application>
</manifest>

Step 5: Connect to the Nearby Messages service

In your app, create a GoogleApiClient and add the Nearby Messages API.

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Nearby.MESSAGES_API)
    .addConnectionCallbacks(this)
    .enableAutoManage(this, this)
    .build();

The Nearby Messages API requires user consent. By calling enableAutoManage(), the GoogleApiClient will show Nearby's opt in dialog before connecting to the api. If enableAutoManage cannot be used (for instance, you want to live outside of an Activity's lifecycle), see Handling User Consent for more information.

Send feedback about...

Nearby Messages API for Android
Nearby Messages API for Android