Get Started

Before you begin

Before you start to code using the Nearby Connections API:

Once the system connects the GoogleApiClient, your app can use the Nearby Connections API.

Initialize your Google API client for Nearby Connections

To use Nearby Connections, you must first initialize the Google Play Services API client and establish a connection to the Nearby Connections API. Connect the GoogleApiClient in your activity’s onStart() and disconnect it in onStop():


public class MainActivity extends Activity implements
    GoogleApiClient.OnConnectionFailedListener {

 private GoogleApiClient mGoogleApiClient;

  protected void onCreate(Bundle savedInstanceState) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)

  public void onStart() {

  public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {


Request permissions

Before using Nearby Connections, your app must request the appropriate permissions for the Strategy you plan to use. See the section on Strategies and the documentation for Strategy for more information.

For example, in order to use the P2P_STAR Strategy, add the specified permissions to your AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

  <!-- Required for Nearby Connections -->
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


Since ACCESS_COARSE_LOCATION is considered to be a dangerous system permission, in addition to adding it to your manifest, you must request the permission at runtime, as described in Requesting Permissions.

If the user does not grant all required permissions for the Strategy you plan to use, the Nearby Connections API will refuse to allow your app to start advertising or discovering.