[Huawei Developers] Mustafa Sarıtemur:
View attachment 5209541​

What is Share Engine

As a cross-device file transfer solution, Huawei Share uses Bluetooth to discover nearby devices and authenticate connections, then sets up peer-to-peer Wi-Fi channels, so as to allow file transfers between phones, PCs, and other devices. It delivers stable file transfer speeds that can exceed 80 Mbps if the third-party device and environment allow. Developers can use Huawei Share features using Share Engine.

The Huawei Share capabilities are sealed deep in the package, then presented in the form of a simplified engine for developers to integrate with apps and smart devices. By integrating these capabilities, PCs, printers, cameras, and other devices can easily share files with each other.

Three SDK development packages are offered to allow quick integration for Android, Linux, and Windows based apps and devices.

Working Principles

Huawei Share uses Bluetooth to discover nearby devices and authenticate connections, then sets up peer-to-peer Wi-Fi channels, so as to allow file transfers between phones, PCs, and other devices.

View attachment 5209543​

To ensure user experience, Huawei Share uses reliable core technologies in each phase of file transfer.

Devices are detected using in-house bidirectional device discovery technology, without sacrificing the battery or security

Connection authentication using in-house developed password authenticated key exchange (PAKE) technology

File transfer using high-speed point-to-point transmission technologies, including Huawei-developed channel capability negotiation and actual channel adjustment

For more information you can follow this link.

Let’s get into codding.

Requirements

For development, we need Android Studio V3.0.1 or later.

EMUI 10.0 or later and API level 26 or later needed for Huawei phone.

Development

1 - First we need to add this permission to AndroidManifest.xml. So that we can ask user for our app to access files.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2 - After let’s shape activity_main.xml as bellow. Thus we have one EditText for getting input and three Button in UI for using Share Engine’s features.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:hint="@string/hint"
tools:ignore="Autofill,TextFields" />

<Button
android:id="@+id/sendTextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="sendText"
android:text="@string/btn1"
android:textAllCaps="false" />

<Button
android:id="@+id/sendFileButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="sendFile"
android:text="@string/btn2"
android:textAllCaps="false" />

<Button
android:id="@+id/sendFilesButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="sendMultipleFiles"
android:text="@string/btn3"
android:textAllCaps="false" />

</LinearLayout>

3 - By adding the following to strings.xml, we create button and alert texts.

<string name="btn1">Send text</string>
<string name="btn2">Send single file</string>
<string name="btn3">Send multiple files</string>
<string name="hint">Write something to send</string>
<string name="errorToast">Please write something before sending!</string>

4 - Later, let’s shape the MainActivity.java class. First of all, to provide access to files on the phone, we need to request permission from the user using the onResume method.

@Override
protected void onResume() {
super.onResume();
checkPermission();
}

private void checkPermission() {
if (checkSelfPermission(READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {READ_EXTERNAL_STORAGE};
requestPermissions(permissions, 0);
}
}

5 - Let’s initialize these parameters for later uses and call this function in onCreate method.

EditText input;
PackageManager manager;

private void initApp(){
input = findViewById(R.id.inputEditText);
manager = getApplicationContext().getPackageManager();
}

6 - We’re going to create Intent with the same structure over and over again, so let’s simply create a function and get rid of the repeated codes.

private Intent createNewIntent(String action){
Intent intent = new Intent(action);
intent.setType("*");
intent.setPackage("com.huawei.android.instantshare");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}

We don’t need an SDK to transfer files between Huawei phones using the Share Engine, instead we can easily transfer files by naming the package “com.huawei.android.instantshare” to Intent as you see above.

7 - Let’s create the onClick method of sendTextButton to send text with Share Engine.

public void sendText(View v) {
String myInput = input.getText().toString();

if (myInput.equals("")){
Toast.makeText(getApplicationContext(), R.string.errorToast, Toast.LENGTH_SHORT).show();
return;
}

Intent intent = CreateNewIntent(Intent.ACTION_SEND);
List<ResolveInfo> info = manager.queryIntentActivities(intent, 0);
if (info.size() == 0) {
Log.d("Share", "share via intent not supported");
} else {
intent.putExtra(Intent.EXTRA_TEXT, myInput);
getApplicationContext().startActivity(intent);
}
}

8 - Let’s edit the onActivityResult method to get the file/s we choose from the file manager to send it with the Share Engine.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK && data != null) {
ArrayList<Uri> uris = new ArrayList<>();
Uri uri;

if (data.getClipData() != null) {
// Multiple files picked
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
uri = data.getClipData().getItemAt(i).getUri();
uris.add(uri);
}
} else {
// Single file picked
uri = data.getData();
uris.add(uri);
}
handleSendFile(uris);
}
}

With handleSendFile method we can perform single or multiple file sending.

private void handleSendFile(ArrayList<Uri> uris) {
if (uris.isEmpty()) {
return;
}

Intent intent;
if (uris.size() == 1) {
// Sharing a file
intent = CreateNewIntent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
} else {
// Sharing multiple files
intent = CreateNewIntent(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

List<ResolveInfo> info = manager.queryIntentActivities(intent, 0);
if (info.size() == 0) {
Log.d("Share", "share via intent not supported");
} else {
getApplicationContext().startActivity(intent);
}
}

9 - Finally, let’s edit the onClick methods of file sending buttons.

public void sendFile(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(i, 10);
}

public void sendMultipleFiles(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
i.setType("*/*");
startActivityForResult(i, 10);
}

We’ve prepared all the structures we need to create, so let’s see the output.

Text Sharing:

View attachment 5209563​

Single File Sharing:

View attachment 5209565
​

Multiple File Sharing:

View attachment 5209567
​

With this guide, you can easily understand and integrate Share Engine to transfer file/s and text with your app.

For more information: https://developer.huawei.com/consumer/en/share-kit/