Once the user has granted all required permissions, your app can begin to
advertise and discover in order to find nearby devices.

First, [choose a `Strategy`](https://developers.google.com/nearby/connections/strategies) for your use case.
The `Strategy` you select determines the connection topology for your app (one
advertiser to N discoverers, or M advertisers to N discoverers).

On devices that will advertise, call `startAdvertising()` with the desired
`Strategy` and a `serviceId` parameter that identifies your app.

On devices that will discover nearby advertisers, call `startDiscovery()` with
the same `Strategy` and `serviceId`.

The `serviceId` value must uniquely identify your app. As a best practice, use
the package name of your app (for example, `com.google.example.myapp`).

The following example shows how to advertise:

```
private void startAdvertising() {
  AdvertisingOptions advertisingOptions =
      new AdvertisingOptions.Builder().setStrategy(STRATEGY).build();
  Nearby.getConnectionsClient(context)
      .startAdvertising(
          getLocalUserName(), SERVICE_ID, connectionLifecycleCallback, advertisingOptions)
      .addOnSuccessListener(
          (Void _) -> {
            // We're advertising!
          })
      .addOnFailureListener(
          (Exception e) -> {
            // We were unable to start advertising.
          });
}
```

The `ConnectionLifecycleCallback` parameter is the callback that will be invoked
when discoverers request to connect to the advertiser. See [Manage
Connections](https://developers.google.com/nearby/connections/android/manage-connections) for details about
defining this callback.

The following example shows how to discover:

```
private void startDiscovery() {
  DiscoveryOptions discoveryOptions =
      new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();
  Nearby.getConnectionsClient(context)
      .startDiscovery(SERVICE_ID, endpointDiscoveryCallback, discoveryOptions)
      .addOnSuccessListener(
          (Void _) -> {
            // We're discovering!
          })
      .addOnFailureListener(
          (Exception e) -> {
            // We're unable to start discovering.
          });
}
```

The `EndpointDiscoveryCallback` parameter is the callback that will be invoked
when nearby advertisers are discovered or lost. See [Manage
Connections](https://developers.google.com/nearby/connections/android/manage-connections) for details about
defining this callback.

Call `stopAdvertising()` when you no longer need to advertise, and
`stopDiscovery()` when you no longer need to discover.

> [!NOTE]
> **Note:** After calling `stopDiscovery()`, the discoverer can still request connections to advertisers that were discovered; however, the discoverer will not discover any new advertisers until it starts discovery again.

> [!CAUTION]
> **Caution:** Since most forms of discovery entail heavy radio operations that increase the odds of established connections getting broken, a very common pattern is to invoke `stopDiscovery()` once you find all the peers you want to connect to and exchange data with.