当前位置:网站首页>Teach you how to integrate HMS nearby service to realize high-speed, no flow transmission of files between intelligent devices!

Teach you how to integrate HMS nearby service to realize high-speed, no flow transmission of files between intelligent devices!

2020-11-10 18:04:53 Huawei Developer Forum

Huawei short distance communication service (Nearby Service) Using Huawei self research protocol , Application Integration Nearby Service after , It can realize the high-speed and fast transmission of files between intelligent devices , And the integration process is easy to understand .

With the increasing popularity of smart phones , People are increasingly dependent on smart phones . Most commonly used mobile applications have file transfer capabilities , Such as : Social applications ( Transfer pictures 、 Transfer files 、 Send chat records, etc )、 File management application ( Transfer files 、 Backup 、 Cell phone cloning, etc )、 Audio and video applications ( Share songs 、 Video etc. ). The file transfer function of these applications mainly relies on the network disk 、 The cloud server transfers the file to another device . These methods need to use user traffic or need to use Wi-Fi Transmission under the scene of , It does not have the ability of face-to-face transmission without traffic . Some developers may try to use Bluetooth or Wi-Fi To achieve near-field data transmission , But this requires complex network protocols 、 Dealing with hardware equipment , And the transmission speed is not satisfactory .

Nearby Service Four advantages

  • Easy integration : only 2 File transfers API, No need to deal with complex network protocols .
  • Fast transmission : The maximum transmission rate can exceed 60M/ second ,1GB File transfer only requires 20 second .
  • There's no need for networking : No need for routers or other network devices , Via bluetooth 、Wi-Fi Realize data transmission , It doesn't cost users their own traffic .
  • Platform support : Support all Android platforms ( Huawei equipment and non Huawei equipment ), Other platforms will open in succession .

Demo Application Introduction (NearbyTransfer)

In order to let developers better understand how to integrate Nearby Service, Here we provide a scan code file Demo For reference .NearbyTransfer Integrated #HMS Core# Nearby Service and Scan Kit, In two smart devices ( mobile phone 、Pad etc. ) Data transmission can be completed by scanning the code .

Developing actual combat

NearbyTransfer Open source project Github link :

https://github.com/HMS-Core/h...

Here's how to use the Demo Run up , So that you can understand the implementation details .

The development of preparation

Tool preparation

  1. Two Huawei mobile phones ( It works best )
  2. development tool Android Studio(3.X or later)

Register as a developer

Registered as a Huawei developer .

Create an application

Reference resources Nearby Service Development ready to create your application in Huawei application market .

structure Demo application

  • Import source code to Android Studio(3.X or later)
  • New applications in Huawei's application market agconnect-services.json Download the file locally , And put it in the app Under the table of contents (\app).

Run sample app

  1. take Demo Application installation to tester A,B
  2. On the tester A Choose "Send File", Pick the files that need to be transferred , Generate qr code
  3. On the tester B Choose "Receive File"
  4. Wait for the end of the file transfer

 Insert picture description here

Key code description

At the project level gradle Add Huawei maven warehouse

AndroidStudio Project level build.gradle file , Incrementally add the following maven Address :

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

At the application level build.gradle Inside plus SDK rely on

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

stay AndroidManifest.xml System permissions are declared in the file

because Nearby Service It's based on Bluetooth 、Wi-Fi、 Storage and other capabilities , Bluetooth needs to be declared here 、Wi-Fi、 Storage and location information permissions .( Code scanning permission 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 permissions -->
    <uses-permission android:name="android.permission.CAMERA" />

because ACCESS_FINE_LOCATION,WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE It's dangerous system permissions , therefore , You need to apply for these permissions dynamically . If there is insufficient authority ,Nearby Service Will refuse to turn on broadcast or scan for your app .

Key code

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

If your application needs integration Nearby Service Transfer files , Just a reference MainActivity take nearbyAgent.sendFile() and nearbyAgent.receiveFile() Integrate into your application , The file can be scanned .

1. Send a file
After the sender selects the file, it calls. nearbyAgent.sendFile(uri) Ready to send 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(uri);
            }
            break;
        case NearbyAgent.REQUEST_CODE_SCAN_ONE:
            nearbyAgent.onScanResult(data);
        default:
            break;
    }
  
    super.onActivityResult(requestCode, resultCode, data);
}

2. Receive the file
Receiver calls nearbyAgent.onScanResult(data) Receive the file

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

For more details , see also :

Official website of Huawei developer Alliance :https://developer.huawei.com/consumer/en/hms/huawei-pushkit

Get development guidance document :
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060

To participate in the developer discussion, please go to Reddit Community :
https://www.reddit.com/r/HuaweiDevelopers/

download demo And sample code go to Github:https://github.com/HMS-Core

To solve the integration problem, please go to Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


Link to the original text :
https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203382449995050417&fid=18
author : pepper

版权声明
本文为[Huawei Developer Forum]所创,转载请带上原文链接,感谢