[Huawei Developers] Freemind R:
HUAWEI Nearby Service is an important feature of HMS Core, designed to help users quickly find players in the vicinity, and automatically establish a low-latency, high-reliability, and zero-traffic data transmission channel, backed by underlying technologies such as Wi-Fi and Bluetooth. Integrating Nearby Service equips your game to provide users with a premium experience across the board.

1. Premium Attributes

1.1 One-Click Cross-Device Connections for Local Multiplayer Games

Current LAN games require that users are connected to the same router, in order for a connection to be established. If there is no router available, users have to manually enable a hotspot, complicating the process. Nearby Service resolves this persistent issue.

1.2 Face-to-Face Grouping and Friend Adding

Nearby Service provides you with functions for creating face-to-face groups and adding friends, without having to rely on social software or GPS, facilitating free and easy user interactions.

1.3 Face-to-Face Prop Sharing

Nearby Service allows users to quickly share game props with friends, helping you acquire new users and improve user stickiness.

2. Introduction to Plugins

Two encapsulated plugins are provided here. You can directly use the two plugins in the app, and view the source code of the plugins, for a thorough understanding of how to integrate the Nearby Service.

2.1 Preparations

Development environment of Unity.

Download the plugins on GitHub.

2.2 Plugin Import

Choose Assets > Import Package > Custom Package, and click Nearby Player or Discovery Plugin on the toolbar of Unity.

Wait for the package to be processed. After that, the resource list of plugins will display. Then click Import.

2.3 Key Code

2.3.1 Nearby Player Plugin

This plugin is applicable to such scenarios as creating face-to-face groups, adding friends, and sharing props. Declare the NearbyManager class in the plugin. The class provides APIs startDiscovery() and SendMessage() for users to discover nearby players and send messages.

Call startDiscovery to discover nearby players and make the current users discoverable by nearby players when the game starts.

The code of the called API is as follows:

void Start() {
AndroidMyCallback cb = new AndroidMyCallback(this);
nm = new NearbyManager(cb);
nm.startDiscovery(randomName());
}

The callback function AndroidMyCallback is used to perform operations after a successful discovery.

// Perform the subsequent operations when a player is discovered. In this demo, the player is being added to the player list.
public override void onFoundPlayer(string endpointName, string endpointId) {
mListController.AddPlayerToList(endpointName, endpointId);
}

// Perform the subsequent operations when a player is lost. In this demo, the player is being removed from the player list.
public override void onLostPlayer(string endpointId) {
mListController.RemovePlayerFromList(endpointId);
}

// Perform the subsequent operations when a player's message is received. In this demo, only the content of the message is displayed.
public override void onReceiveMsg(string endpointName, string Msg) {
mListController.ReceiveMsg(endpointName, Msg);
}

After discovering nearby players, users can send messages to the players for grouping, adding friends, or sharing props.

// In this demo, click a player's profile picture in the player list to send a grouping invitation.
private void OnClick(string endpointId) {
nm.log("OnClick. SendMessage to " + endpointId);
nm.SendMessage(endpointId, "invites you to join a game.");
}

2.3.2 Nearby Discovery Plugin

This is a plugin developed based on Unity UNET. Users are able to connect their devices on a mutual basis, even they are in different Wi-Fi environments. Declare the NearbyManager class, which offers two APIs: startBroadcast() and startDiscovery(). Two game devices can be connected by calling the above APIs.

The code of the called APIs is as follows:

private void OnClick() {
Button btn = this.GetComponent<Button>();
btn.enabled = false;
AndroidMyCallback androidMyCallback = new AndroidMyCallback(mNetworkManager);
NearbyManager nearbyManager = new NearbyManager(androidMyCallback);
nearbyManager.startBroadcast();
}

The callback function AndroidMyCallback is used to perform operations after a successful connection. Here, the networkManager API of UNET is called to start the game after a successful connection.

public class AndroidMyCallback : AndroidCallback {
private NetworkManager mNetworkManager;

public AndroidMyCallback(NetworkManager networkManager) : base() {
mNetworkManager = networkManager;
}

public override void onClientReady(string ipaddr) {
mNetworkManager.networkAddress = ipaddr;
mNetworkManager.StartClient();
}

public override void onServerReady(string ipaddr) {
mNetworkManager.StartHost();
}
}

2.4 Demo

Below we have provided two demos that have integrated the plugins detailed above, to provide you with a better understanding of the process.

Nearby-Player-Demo

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110092241.66287290886989902355811656326861:50511116074740:2800:265496E9E2D8DB9B8BB414B32F84E0749E2ED8240A0A66313DDE5B2CECD0DD2C.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110092241.66287290886989902355811656326861:50511116074740:2800:265496E9E2D8DB9B8BB414B32F84E0749E2ED8240A0A66313DDE5B2CECD0DD2C.jpg[/IMG]

UNET-NEARBY-DEMO

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110092206.05651084308042564325274698109716:50511116074740:2800:FBD01544410F0EFA626562BC7294A6B73FED29B2237BE0660A5E750D9E30A9E1.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110092206.05651084308042564325274698109716:50511116074740:2800:FBD01544410F0EFA626562BC7294A6B73FED29B2237BE0660A5E750D9E30A9E1.jpg[/IMG]

3. Game Apps That Have Integrated Nearby Service

Tic Tac Toe

Tic Tac Toe is a local battle game that was developed based on native Android APIs from Nearby Service, and released on the HUAWEI AppGallery platform.

NearbyGameSnake

NearbyGameSnake is a local multiplayer game that integrates Nearby Service. It is easy to play, and enables users to play directly in groups, without requiring an Internet connection.

4. Learn More

For more details, please visit HUAWEI Developers.

For more instructions, please visit Development Guide.

You can join the HMS Core developer discussion by going to Reddit.

You can download the demo and sample code from GitHub.

To solve integration problems, please go to Stack Overflow.