in NearbyConnectionsCpp/app/src/main/cpp/NearbyConnection.cpp [45:121]
void Engine::InitGoogleNearbyConnection() {
// Need only one connection
if (nearby_connection_ != nullptr) {
return;
}
nbc_state_ = nearby_connection_state::IDLE;
gpg::AndroidInitialization::android_main(app_);
gpg::AndroidPlatformConfiguration platform_configuration;
platform_configuration.SetActivity(app_->activity->clazz);
// Two devices can connect using Nearby Connections if they share the
// same service ID. This value is set in AndroidManifest.xml, it can be any
// string that is unique for your application
service_id_ = ndk_helper::JNIHelper::GetInstance()->GetNearbyConnectionServiceID();
// Prepare to create a nearby connection interface
gpg::NearbyConnections::Builder nbBuilder;
nearby_connection_ = nbBuilder.SetDefaultOnLog(gpg::LogLevel::VERBOSE)
// register callback so we get notified the nearby connect is ready
.SetOnInitializationFinished([this](gpg::InitializationStatus status) {
switch (status) {
case gpg::InitializationStatus::VALID:
// Our interface is ready to use [ could advertise or discover ]
LOGI("InitializationFinished() returned VALID");
nbc_state_ = nearby_connection_state::IDLE;
// Configure our listener to use for listening
msg_listener_.SetOnMessageReceivedCallback(
[this](int64_t receiver_id,
std::string const &remote_endpoint,
std::vector<uint8_t> const &payload,
bool is_reliable) {
OnMessageReceivedCallback(receiver_id,
remote_endpoint,
payload, is_reliable);
})
.SetOnDisconnectedCallback([this](
int64_t receiver_id, std::string const &remote_endpoint) {
OnMessageDisconnectedCallback(receiver_id, remote_endpoint);
});
LOGI("Nearby Connection Interface is ready to use!");
break;
case gpg::InitializationStatus::ERROR_VERSION_UPDATE_REQUIRED:
/*
* interface need update, we should prompt user to do so, or we do
* it automatically here [not done]
*/
LOGE(
"InitializationFinished(): ERROR_VERSION_UPDATE_REQUIRED "
"returned,"
"please restart your device to update Google play service");
nbc_state_ = nearby_connection_state::FAILED;
break;
case gpg::InitializationStatus::ERROR_INTERNAL:
/*
* Error happened, interface is not ready to use
*/
LOGE(
"InitializationFinished() failed, unable to start nearby connection");
nbc_state_ = nearby_connection_state::FAILED;
EnableUI(false);
break;
default:
LOGE("InitializationFinished(): Unrecognized error code: %d",
(int)status);
nbc_state_ = nearby_connection_state::FAILED;
EnableUI(false);
break;
}
})
.Create(platform_configuration);
// At this point, connection is still not useful; wait till
// InitializationFinished()
// telling us that VALID initialization has been completed
LOGV("InitGoogleNearbyConnection() created interface: %p",
nearby_connection_.get());
}