Huawei's Near Field Service (Nearby Service) uses Huawei's self-developed protocol. After the application is integrated with the Near Service, it can realize fast and fast file transfer between smart devices without traffic, and the integration process is simple and easy to understand.

With the increasing penetration rate of smart phones, people are becoming more and more dependent on smart phones. Most commonly used mobile phone applications have file transfer functions, such as: social applications (transfer pictures, files, chat history, etc.), file management applications (transfer files, backup, phone cloning, etc.), audio and video applications (share songs) , Video, etc.). The file transfer function of these types of applications is mainly to transfer files to another device with the help of network disks and cloud servers. These methods all need to use user traffic or need to be transmitted in a Wi-Fi scenario, and do not have the capability of face-to-face transmission without traffic. Some developers may try to achieve near-field data transmission through Bluetooth or Wi-Fi, but this requires dealing with complex network protocols and hardware devices, and the transmission speed is not satisfactory.

Four advantages of Nearby Service

  • Easy integration : Only 2 file transfer APIs, no need to deal with complex network protocols.

  • Extremely fast transmission : The maximum transmission rate can exceed 60M/sec, and the transfer of 1GB files only takes 20 seconds.

  • No need to connect to the Internet : Without the help of routers or other network devices, data transmission is realized through Bluetooth and Wi-Fi, and it does not cost users their own traffic.

  • Platform support : Supports all Android platforms (Huawei devices and non-Huawei devices), and other platforms will be opened one after another.

Demo application introduction (NearbyTransfer)

In order to let developers better understand how to integrate Nearby Service, here is a demo for scanning code to transfer files for reference. NearbyTransfer integrates #HMS Core# Nearby Service and Scan Kit, and can complete data transmission between two smart devices (mobile phones, Pads, etc.) by scanning codes.

Development combat

Github link of NearbyTransfer open source project:

Here is how to run this Demo based on the source code, so that you can understand the implementation details.

Development preparation

Tool preparation

  1. Two Huawei phones (best effect)

  2. Development Tools Android Studio (3.X or later)

Register as a developer

Register as a Huawei developer.

Create an application

Refer to the development of Nearby Service and prepare to create your application in the Huawei application market.

Build a demo application

Import source code to Android Studio (3.X or later)

Download the agconnect-services.json file of the newly created application on the Huawei App Market to the local, and place it in the app directory (\app) of the sample code.

Run the sample application

  1. Install Demo application to test machine A, B

  2. Select "Send File" on test machine A, select the file to be transferred, and generate a QR code

  3. Select "Receive File" on test machine B

  4. Wait for the file transfer to end

Insert picture description here

Key code description

Add Huawei maven warehouse in project-level gradle

AndroidStudio project-level build.gradle file, incrementally add the following maven address:

buildscript {
    repositories {
        maven { url ''}
    }    }allprojects {
    repositories {
        maven { url ''}

Add SDK dependencies to application-level build.gradle

dependencies {
    implementation 'com.huawei.hms:nearby:'
    implementation 'com.huawei.hms:scan:'

Declare system permissions in the AndroidManifest.xml file

Because Nearby Service is based on Bluetooth, Wi-Fi, storage and other capabilities, it is necessary to declare the permissions of Bluetooth, Wi-Fi, storage, and location information. (To be added scan code permissions)

    <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" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA" />

Because ACCESS_FINE_LOCATION, WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE are dangerous system permissions, you need to apply for these permissions dynamically. If the permissions are insufficient, Nearby Service will refuse to enable broadcasting or scanning for your application.

Key code

Main code path: com\huawei\hms\simpleNearbyDemo\

If your application needs to integrate Nearby Service to transfer files, you only need to refer to MainActivity to integrate nearbyAgent.sendFile() and nearbyAgent.receiveFile() into your application, then you can scan code to transfer files.

1. Send the file The
sender selects the file and calls nearbyAgent.sendFile(uri) to prepare to send the file

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                Uri uri = data.getData();
        case NearbyAgent.REQUEST_CODE_SCAN_ONE:

    super.onActivityResult(requestCode, resultCode, data);

2. The recipient of the received file
calls nearbyAgent.onScanResult(data) to receive the file

  recvBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

For more details, please refer to:

Official website of Huawei Developer Alliance:

Obtain development guidance documents:

To participate in developer discussions, please go to the Reddit community:

To download the demo and sample code, please go to Github:

To solve integration problems, please go to Stack Overflow:

Original link:
Author: pepper

Guess you like