Your app can subscribe to network scenario changes. When the real-time or predicted scenario information changes, the device will send information to your app, including the link type, network scenario type, data transmission policy suggestions, and weak signal information.
For details about the APIs, please refer to API Reference.
| API | Description |
|---|---|
| int32_t HMS_NetworkBoost_RegisterNetSceneCallback(HMS_NetworkBoost_NetSceneChange callback, uint32_t* callbackId) | Registers the network scenario change callback. |
| int32_t HMS_NetworkBoost_UnregisterNetSceneCallback(uint32_t callbackId) | Deregisters the network scenario change callback. |
Import the Network Boost Kit module.
#include "NetworkBoostKit/network_boost_quality.h" #include <cstdio>
Add the following library to the CMakeLists.txt file. For details, please refer to Preparations for C API Development.
libnetwork_boost.so
Register the network scenario identification information callback.
uint32_t callbackId = 0;
void onNetworkSceneChanged(NetworkBoost_NetworkScene *ns)
{
// Process the network scenario identification callback information.
printf("Data link type: %d\n", ns->pathType);
printf("Network scenario: %d\n", ns->scene);
switch (ns->scene) {
case NB_SCENE_NORMAL:
// Common scenario processing.
break;
case NB_SCENE_CONGESTION:
// Congestion scenario processing.
break;
case NB_SCENE_FREQUENT_HANDOVER:
// Signal fast switching scenario processing.
break;
case NB_SCENE_WEAK_SIGNAL:
// Weak signal scenario processing.
break;
}
printf("Suggested data transmission policy: %d\n", ns->recommendedAction);
if (ns->weakSignalPrediction.duration > 0) {
// Weak signal prediction processing.
}
}
int32_t RegisterNetSceneCallback()
{
HMS_NetworkBoost_NetSceneChange callback;
callback = onNetworkSceneChanged;
// Register a callback and obtain the callback ID. The ID is returned by the system and will be used for subsequent deregistration.
int32_t ret = HMS_NetworkBoost_RegisterNetSceneCallback(callback, &callbackId);
printf("Result of registering the network scenario identification information callback: %d, Id:%d\n", ret, callbackId);
return ret;
} Deregister the network scenario identification information callback after your app process ends.
int32_t UnregisterNetSceneCallback()
{
// Deregister the callback using the callback ID obtained during registration.
int32_t ret = HMS_NetworkBoost_UnregisterNetSceneCallback(callbackId);
printf("Result of deregistering the network scenario identification information callback: %d\n", ret);
return ret;
}