[Huawei Developers] Jack Wu Shenzhen:
Introduction

When you integrate Nearby Service into your apps, they'll be able to transmit files between smart devices without using mobile data. The service uses Huawei's own protocols, and the integration process is quick and easy.

Many mobile apps need to transmit files between devices. Social networking apps transmit images, files, and chat records; file management apps transmit files, data backups, and cloned phone data; and audio and video apps transmit songs and videos. To achieve this, these apps have to rely on web disks and cloud servers, which means users have to either use their own mobile data or be connected to Wi-Fi. Previously, implementing near-field data transmissions backed by Bluetooth or Wi-Fi required complex network protocols and hardware devices, and the transmission rate was average. Now, thanks to Nearby Service, this is easier and more effective than ever.

Advantages of Using Nearby Service

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 (NearbyTransfer)

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

NearbyTransfer Development

You can find the open source code for NearbyTransfer on 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

1.	Two Huawei phones (recommended)

2.	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 HUAWEI Developers-App Development.

Create a Demo

•	Import the source code to Android Studio (3.X or Later).

•	Download the agconnect-services.json file for the new demo, and save it on your local computer in the app directory (\app) of the sample code.

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.

Let's see the demo effect!

https://www.reddit.com/r/HMSCore/comments/jj1o9z/how_to_implement_highspeed_datafree_file/

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 your Android Studio 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'
}

Declare the System Permission in the AndroidManifest.xml File

Nearby Service is implemented based on Bluetooth, Wi-Fi, storage, and location information, so the corresponding permissions need to be declared. (The barcode scanning permission is to be added.)

<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" />

The ACCESS_FINE_LOCATION, WRITE_EXTERNAL_STORAGE, and READ_EXTERNAL_STORAGE permissions are dangerous system permissions, so you need to apply for them dynamically. If your app does not have these permissions, Nearby Service will not let it broadcast or scan.

Key Code

Main code path: com\huawei\hms\simpleNearbyDemo\MainActivity.java

If you want to integrate Nearby Service's file transmission function, take MainActivity for reference and integrate nearbyAgent.sendFile() and nearbyAgent.receiveFile() into your app.

1.	Sending a file.

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

[HTML [MENTION=439709]override[/MENTION]

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(uri);

}

break;

case NearbyAgent.REQUEST_CODE_SCAN_ONE:

nearbyAgent.onScanResult(data);

default:

break;

}

super.onActivityResult(requestCode, resultCode, data);

}

[/HTML]

2.	Receiving a file.

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

recvBtn.setOnClickListener(new View.OnClickListener() {
[MENTION=439709]override[/MENTION]
public void onClick(View v) {
nearbyAgent.receiveFile();
}
});

References

Official Huawei Developers website:

https://developer.huawei.com/consumer/en/hms/huawei-pushkit

Development Guide:

https://developer.huawei.com/consum...-Guides/service-introduction-0000001050040060

Official HMS Core community on Reddit:

https://www.reddit.com/r/HuaweiDevelopers/

Demo and sample code:

https://github.com/HMS-Core

Discussions on Stack Overflow:

https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest

If you have any needs or suggestions, please contact us:

nearbyservice@Huawei.com