[Huawei Developers] Freemind R:
More information, you can visit HUAWEI Developer Forum.​

Complex self-networking, occasional long-tail latency, and network connection failures are preventing your game from earning more. Nearby Service will solve these problems with its nearby point-to-point (P2P) direct connection technologies.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200717034714.54395786990653338278575780238920:50510720122341:2800:F099E74CEADBF8E3E804EF28F73B96C468F41FB520BDB6FD31DC5D9C46C4B2CC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200717034714.54395786990653338278575780238920:50510720122341:2800:F099E74CEADBF8E3E804EF28F73B96C468F41FB520BDB6FD31DC5D9C46C4B2CC.png[/IMG]​

Nearby Service has a technological edge in the following gaming scenarios:

Uses Bluetooth and Wi-Fi to discover devices. After integrating Nearby Service, your game can automatically search for nearby users who are also in the game without manual intervention.

Selects the best networking and transmission modes automatically. Nearby Service supports P2P, star, and mesh topologies and allows devices to connect to each other through Wi-Fi P2P without connecting to the same AP.

Uses Huawei-developed transmission protocols featuring high bandwidth, low latency, and high stability and reliability to transmit data.

App Development

An open-source game named Snake Land on GitHub is available for us to implement network communications using Nearby Service to support multi-player gaming. Snake Land originally uses the open-source framework KryoNet to implement the network communication layer. KryoNet supports only interconnection between mobile phones connected to the same router. After Nearby Service is integrated, nearby mobile phones can connect to each other unconditionally.

Getting Started

To integrate HUAWEI Nearby Service, you must complete the following preparations:

Configure app information in AppGallery Connect.

Enable HUAWEI Nearby Service.

Integrate the HMS Core SDK.

Configure obfuscation scripts.

For details, please refer to Getting Started.

How Can I Use Nearby Service

First, let's compare the APIs of KryoNet and Nearby Service.

[IMG="https://cdn-images-1.medium.com/max/800/1*vSmLE_G9qTi3QaIbOJSyqw.png"]https://cdn-images-1.medium.com/max/800/1*vSmLE_G9qTi3QaIbOJSyqw.png[/IMG]​

API functions of Nearby Service and KryoNet are the same. Therefore, we only need to replace the APIs of KryoNet with those of Nearby Service and convert parameters based on the parameter type differences.

Permission Application

Nearby Service involves operations such as Bluetooth discovery and Wi-Fi hotspot creation. Therefore, you need to apply for some permissions from the user.

The sample code is as follows:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Building, Loading, and Debugging the App

After configuring the previous code, build your app, generate an APK file, and install it on the prepared phone running Android 8 or later for debugging. For details about the development procedure, please refer to Codelab.

Testing Your App

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200717034834.71154153262730055897962105674819:50510720122341:2800:F5CDD78072E255E7DA3A46A79C961B84EFD5B6F985C03F6314921DCB273E5C92.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20200717034834.71154153262730055897962105674819:50510720122341:2800:F5CDD78072E255E7DA3A46A79C961B84EFD5B6F985C03F6314921DCB273E5C92.gif[/IMG]​

One party taps Create a Game, and the other party taps Join a Game to start mutual discovery. After one another is discovered, tap GO! to start the game. Swipe on the screen to control the direction of the green snake and get more food. The snake will fail if its head hits an obstacle.

If you are interested in the implementation details, download the source code from GitHub:

https://github.com/HMS-Core/hms-nearby-demo/tree/master/NearbyGameSnake

Conclusion

Using Nearby Connection, you can implement the following functions for your app:

1. Local multi-player gaming: Players can connect to each other's device flexibly and enjoy a smooth gaming experience enabled by low-latency (less than 50 ms), stable, and reliable data transmission.

2. Offline file transfer: Files can be transferred at a speed of up to 80 Mbit/s without consuming any mobile data.

If you are interested and want to learn more, check our development guide at

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050040566