Пример #1
0
  private void startAdvertising() {
    SystemUtils.toast(ServerActivity.this, "startAdvertising()");
    if (!NetworkUtils.isConnectedToNetwork(this)) {
      SystemUtils.toast(ServerActivity.this, "!isConnectedToNetwork()");
      // return;
    }

    // Identify that this device is the host
    mIsHost = true;

    // Advertising with an AppIdentifer lets other devices on the
    // network discover this application and prompt the user to
    // install the application.
    List<AppIdentifier> appIdentifierList = new ArrayList<>();
    appIdentifierList.add(new AppIdentifier(getPackageName()));
    AppMetadata appMetadata = new AppMetadata(appIdentifierList);

    // The advertising timeout is set to run indefinitely
    // Positive values represent timeout in milliseconds
    long NO_TIMEOUT = 0L;

    String name = null;
    Nearby.Connections.startAdvertising(mGoogleApiClient, name, appMetadata, NO_TIMEOUT, this)
        .setResultCallback(
            new ResultCallback<Connections.StartAdvertisingResult>() {
              @Override
              public void onResult(Connections.StartAdvertisingResult result) {
                if (result.getStatus().isSuccess()) {
                  SystemUtils.toast(ServerActivity.this, "Device is advertising");
                } else {
                  int statusCode = result.getStatus().getStatusCode();
                  SystemUtils.toast(
                      ServerActivity.this, "Advertising failed - see statusCode for more details");
                }
              }
            });
  }