Handling User Consent

The Nearby Messages API has the potential to be battery-intensive due to the way it uses Bluetooth and other device resources to detect and communicate with nearby devices. To ensure that users are in control of the experience, an opt-in dialog is presented the first time the user connects to the Nearby Messages API. The user must provide consent for Nearby to utilize the required device resources. As a developer, the easiest way to handle showing the dialog is to use enableAutoManage().

Use enableAutoManage()

If you can, we recommend using enableAutoManage(). The following code snippet is everything you need to get started. For more details about the publish, subscribe, unpublish, and unsubscribe methods, see Publish and Subscribe.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Nearby.MESSAGES_API)
            .addConnectionCallbacks(this)
            .enableAutoManage(this, this)
            .build();
}

@Override
public void onConnected(Bundle connectionHint) {
    publish("Hello World");
    subscribe();
}

@Override
public void onStop() {
    unpublish();
    unsubscribe();
    ...
    super.onStop();
}

BLE Only

You can avoid the opt in dialog if your app has been granted the ACCESS_FINE_LOCATION permission and only uses BLE during publishes and subscribes.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        == PackageManager.PERMISSION_GRANTED) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Nearby.MESSAGES_API, new MessagesOptions.Builder()
            .setPermissions(NearbyPermissions.BLE)
            .build())
        .addConnectionCallbacks(this)
        .enableAutoManage(this, this)
        .build();
}

Manually connect to GoogleAPIClient

Depending on how your app is set up, it may be simpler to manually connect to the GoogleAPIClient instead of calling enableAutoManage(), which requires a FragmentActivity and calls connect() in onStart() and disconnect() in onStop().

The following code example shows you how to manually build the GoogleAPIClient and connect. Note that an Activity context is still needed.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Nearby.MESSAGES_API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
}

@Override
public void onStart() {
    super.onStart();
    ...
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO
}

@Override
public void onConnectionSuspended(int cause) {
    // TODO
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO
}

@Override
public void onStop() {
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    ...
    super.onStop();
}

Handle errors

The first time a user connects to the Nearby Messages API, an error with the status code APP_NOT_OPTED_IN will result since the user has not given consent yet. In this case, the error handler calls startResolutionForResult, which results in displaying a consent prompt to the user. After handling the error, use onActivityResult to connect.

private static final int REQUEST_RESOLVE_ERROR = 1001;

@Override
public void onConnectionSuspended(int cause) {
    Log.e(TAG, "GoogleApiClient disconnected with cause: " + cause);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (result.hasResolution()) {
        result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
    } else {
        Log.e(TAG, "GoogleApiClient connection failed");
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_RESOLVE_ERROR) {
        if (resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            Log.e(TAG, "GoogleApiClient connection failed. Unable to resolve.");
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Publish and subscribe

Once we've connected, we can start our publishes and subscribes.

@Override
public void onConnected(Bundle connectionHint) {
    publish("Hello World");
    subscribe();
}

@Override
public void onStop() {
    unpublish();
    unsubscribe();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    ...
    super.onStop();
}

Enviar comentarios sobre…

Nearby Messages API for Android
Nearby Messages API for Android