[General Questions and Answers] XDARoni:
Introduction

1. Easy integration: You only need two file transmission APIs to integrate Nearby Service, and it doesn't require any complex network protocols.

2. Ultra-fast transmission: It achieves file transmission rate of up to 60 MB/s, so transmitting a 1 GB file takes only 20 seconds.

3. Offline transmission: Nearby Service doesn't require routers or any other network devices. Data is transmitted through Bluetooth and Wi-Fi, so it doesn't consume the user's mobile data.

4. Platform support: The service supports all Android platforms, and will work with other platforms soon.

Demo (NearbyFileTransfer)

Here, take NearbyFileTransfer as an example. You can see how NearbyFileTransfer uses QR codes to transmit files between devices. NearbyFileTransfer has integrated with Nearby Service and Scan Kit.

NearbyTransfer Development

You can find the open source code for NearbyFileTransferon GitHub.

Now, I'll show you how to create the demo based on the source code, so that you can better understand the implementation process.

Preparations

Tools

Two Huawei phones (recommended).

Android Studio (3.X or later).

Register as a Developer

Register as a Huawei developer.

Create an App

Create an app on Huawei AppGallery. For details, please refer to Nearby Service Getting Started Tutorial.

Create a Demo

Download the agconnect-services.json file from AppGallery Connect, and put it into the the app directory (\app) of the demo project.

Sync and build the demo project.

Run the Demo

3. Install the demo on test devices A and B.

4. Tap Send File on device A and select the file you want to transmit. A QR code will be generated.

5. Tap Receive File on device B.

6. Wait until the file transmission is complete.

Code Development

Add Huawei Maven Repository to the Project-Level build.gradle File of Your Project

Add the following Maven address to the build.gradle file in the root directory of

the demo project:

buildscript {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
} }allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}}

Add SDK Dependencies to the App-Level build.gradle File

dependencies {
implementation 'com.huawei.hms:nearby:5.0.2.300'
implementation 'com.huawei.hms:scan:1.2.3.300'
}

Please always use the newest version of Nearby Service listed on the version change history page.

Declare the System Permission in the AndroidManifest.xml File

This demo needs Bluetooth, Wi-Fi, storage, and location permission to transfer files, and needs camera permission to scan QR codes.

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

1. Sending a file.

The sender selects a file and calls nearbyAgent.sendFile to send the file.

@Override
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();
nearbyAgent.sendFile(new File(uri.getPath()));
}
break;
case NearbyAgent.REQUEST_CODE_SCAN_ONE:
nearbyAgent.onScanResult(data);
default:
break;
}

super.onActivityResult(requestCode, resultCode, data);

}

2. Receiving a file.

The receiver calls nearbyAgent.onScanResult(data) to receive a file.

recvBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nearbyAgent.receiveFile();
}
});

References

Our official website

Development Guide

Reddit to join our developer discussion

GitHub to download demos and sample codes

Stack Overflow to solve any integration problems

For more information like this, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0201386494098350507