Skip to content
Branch: master
Find file History
Type Name Latest commit message Commit time
Failed to load latest commit information.
rockpaperscissors Migrate rockpaperscissors to Android Pie, androidx Sep 26, 2018
walkietalkie Merge branch 'master' into refactor Mar 2, 2019 Update compile to implementation Nov 11, 2018

Nearby Connections

Nearby Connections is a peer-to-peer networking API that allows apps to easily discover, connect to, and exchange data with nearby devices in real-time, regardless of network connectivity.

Where to Download

dependencies {
  implementation ''


The following permissions are required in your AndroidManfiest.xml

<!-- 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" />


Devices can advertise themselves to other nearby devices discovering with the same service id and strategy.

    /* endpointName= */ "Device A",
    /* serviceId= */ "com.example.package_name",
    new AdvertisingOptions(Strategy.P2P_CLUSTER));


A discovering device will be notified of nearby advertisers through their EndpointDiscoveryCallback.

    /* serviceId= */ "com.example.package_name",
    new DiscoveryOptions(Strategy.P2P_CLUSTER));


When ready, the discoverer can request a connection to the advertiser.

    /* endpointName= */ "Device B",

Both devices are then asked to confirm the connection. An authentication token is provided that can be manually (typically visually) confirmed.

private final ConnectionLifecycleCallback mConnectionLifecycleCallback =
  new ConnectionLifecycleCallback() {
    public void onConnectionInitiated(String endpointId, ConnectionInfo connectionInfo) {
      // Automatically accept the connection on both sides.
      Nearby.getConnectionsClient(this).acceptConnection(endpointId, mPayloadCallback);

    public void onConnectionResult(String endpointId, ConnectionResolution result) {
      switch (result.getStatus().getStatusCode()) {
        case ConnectionsStatusCodes.STATUS_OK:
          // We're connected! Can now start sending and receiving data.
        case ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED:
          // The connection was rejected by one or both sides.
          // The connection was broken before it was accepted.

    public void onDisconnected(String endpointId) {
      // We've been disconnected from this endpoint. No more data can be
      // sent or received.

Transfer Payloads

Once connected, devices can send payloads to each other.

Nearby.getConnectionsClient(this).sendPayload(endpointId, payload);

The payload will appear on the other device via the PayloadCallback passed in during acceptConnection.

private final PayloadCallback mPayloadCallback =
  new PayloadCallback() {
    public void onPayloadReceived(String endpointId, Payload payload) {
      // A new payload is being sent over.

    public void onPayloadTransferUpdate(String endpointId, PayloadTransferUpdate update) {
      // Payload progress has updated.
You can’t perform that action at this time.