void Engine::OnAdvertiseButtonClick()

in NearbyConnectionsCpp/app/src/main/cpp/NearbyConnection.cpp [395:463]


void Engine::OnAdvertiseButtonClick(void) {
  if (nbc_state_ & nearby_connection_state::ADVERTISING) {
    return;
  }
  LOGV("OnAdvertiseButtonClick(): Listening");
  nbc_state_ |= nearby_connection_state::ADVERTISING;
  nbc_state_ &= ~nearby_connection_state::IDLE;

  /*
   * start advertising, and advertise forever until stop is called
   */
  std::vector<gpg::AppIdentifier> app_identifiers;
  gpg::AppIdentifier tmp;
  tmp.identifier = std::string(ndk_helper::JNIHelper::GetInstance()->GetAppName());
  app_identifiers.push_back(tmp);
  nearby_connection_->StartAdvertising(
        "",            // Let SDK generate the name
        app_identifiers,            // Package name is identifier
        gpg::Duration::zero(),      // Advertise forever
        [this](int64_t client_id, gpg::StartAdvertisingResult const &result) {
          LOGV("StartAdvertisingResult(%lld, %s)", static_cast<long long>(client_id),
               result.local_endpoint_name.c_str());
          switch (result.status) {
            case gpg::StartAdvertisingResult::StatusCode::SUCCESS:
              LOGI("advertising success!");
              break;
            case gpg::StartAdvertisingResult::StatusCode::
                ERROR_ALREADY_ADVERTISING:
              LOGI("advertising already in place");
              break;
            case gpg::StartAdvertisingResult::StatusCode::
                ERROR_NETWORK_NOT_CONNECTED:
              nbc_state_ |= nearby_connection_state::FAILED;
              nbc_state_ &= ~nearby_connection_state::ADVERTISING;
              LOGE("advertising failed: no network connection");
              break;
            case gpg::StartAdvertisingResult::StatusCode::ERROR_INTERNAL:
              LOGE("advertising failed as ERROR_INTERNAL");
              nbc_state_ |= nearby_connection_state::FAILED;
              nbc_state_ &= ~nearby_connection_state::ADVERTISING;
              break;
          }
          EnableUI(true);
        },
        [this](int64_t client_id, gpg::ConnectionRequest const &request) {
          LOGI("ConnectionRequest(%lld)", static_cast<long long>(client_id));
          LOGI(
              "remote info: req.endpoint_id=%s,  req_name = "
              "%s",
              request.remote_endpoint_id.c_str(),
              request.remote_endpoint_name.c_str());
          std::vector<uint8_t> payload;   //empty packet for remote

          // Accept all the connection requests as they come in.
          nearby_connection_->AcceptConnectionRequest(
              request.remote_endpoint_id, payload, msg_listener_);
          BroadcastNewConnection(request.remote_endpoint_id);
          // Adding this end point into the connected state:

          AddConnectionEndpoint(request.remote_endpoint_id, true,
                  /* is the host (advertiser of this connection) */true);
          nbc_state_ |= nearby_connection_state::CONNECTED;
          nbc_state_ &= ~nearby_connection_state::IDLE;
          EnableUI(true);
          LOGI("Accepting Request sending out (for %s)",
               request.remote_endpoint_id.c_str());
  });
  EnableUI(true);
}