Overview

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. File transfer speeds can exceed 80 Mbps if the third-party device and environment allow.
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. In this codelab, your will learn how to use Huawei Share on Android devices. Powered by Huawei Share, Android devices can send files or text content to Huawei devices.

In this codelab, you will create a demo project, integrate the Share Engine SDK into the demo project, and complete the overall process setup of the Share Engine service.

Schematic drawing

What You Will Learn

Hardware Requirements

Software Requirements

What You Need to Be Familiar with

Share Engine integration requires the following preparations:

1.Integrate the Share Engine SDK.

Place the SDK package in the libs directory of your app and completes the following settings in the build.gradle file of the app:

dependencies { implementation files('libs/sharekit-1.0.1.300.aar') implementation 'com.android.support:support-annotations:28.0.0' implementation 'com.android.support:localbroadcastmanager:28.0.0' implementation 'com.android.support:support-compat:28.0.0' implementation 'com.google.guava:guava:24.1-android' }

Change the value of minSdkVersion in the build.gradle file to 26, as shown in the following figure.

Click the sync button, as shown in the following figure.

The dialog box shown in the following figure is displayed when the syncing is successful.

2.Initialize Share Engine.

Initialize Share Engine in the onCreate method of the activity.

private void onInit() { IShareKitInitCallback initCallback = isSuccess -> { Log.i(TAG, "sharekit init result:" + isSuccess); updateStatusHistory( isSuccess ? getString(R.string.sharekit_init_finish) : getString(R.string.sharekit_init_failed)); }; updateStatusHistory(getString(R.string.sharekit_init_start)); shareKitManager.init(initCallback); clearDeviceList(); }

3.Register a callback interface.

The callback interface is called when a device appears, disappears, or has status changed.

/* ShareKit callback interface, will be called when device found or disappear, and status change */ private static final int STATE_PROGRESS = 1; private static final int STATE_SUCCESSFUL = 2; private static final int STATE_CHANGE = 3; private static final int STATE_EXCEPTION = 4; private IWidgetCallback callback = new IWidgetCallback.Stub() { @Override public synchronized void onDeviceFound(NearByDeviceEx nearByDeviceEx) { Log.i(TAG, "onDeviceFound"); String item = getString(R.string.sharekit_device_found, nearByDeviceEx.getBtName() + "-" + nearByDeviceEx.getCommonDeviceId().substring(0, COMMON_ID_PRINT_LENGTH)); updateStatusHistory(item); synchronized (lock) { deviceMap.put(nearByDeviceEx.getCommonDeviceId(), nearByDeviceEx); foundTimeMap.put(nearByDeviceEx.getCommonDeviceId(), format.format(new Date())); } updateDeviceList(); } @Override public void onDeviceDisappeared(NearByDeviceEx nearByDeviceEx) { Log.i(TAG, "onDeviceDisappeared"); String item = getString(R.string.sharekit_device_disappear, nearByDeviceEx.getBtName() + "-" + nearByDeviceEx.getCommonDeviceId().substring(0, COMMON_ID_PRINT_LENGTH)); updateStatusHistory(item); synchronized (lock) { deviceMap.remove(nearByDeviceEx.getCommonDeviceId()); foundTimeMap.remove(nearByDeviceEx.getCommonDeviceId()); } updateDeviceList(); } @Override public void onTransStateChange(NearByDeviceEx nearByDeviceEx, int state, int stateValue) { /* check the document about what does each number means */ Log.i(TAG, "trans state:" + state + " value:" + stateValue); String stateDesc = ""; switch (state) { case STATE_PROGRESS: stateDesc = getString(R.string.sharekit_send_progress, stateValue); break; case STATE_SUCCESS: stateDesc = getString(R.string.sharekit_send_finish); break; case STATE_STATUS: stateDesc = getString(R.string.sharekit_state_chg, translateStateValue(stateValue)); break; case STATE_ERROR: stateDesc = getString(R.string.sharekit_send_error, translateErrorValue(stateValue)); break; default: break; } String item = getString(R.string.sharekit_trans_state, nearByDeviceEx.getBtName(), stateDesc); updateStatusHistory(item); } @Override public void onEnableStatusChanged() { int status = shareKitManager.getShareStatus(); Log.i(TAG, "sharekit ability current status:" + status); updateStatusHistory(getString(R.string.sharekit_status), status); } } private void onRegister() { int ret = shareKitManager.registerCallback(callback); updateStatusHistory(getString(R.string.sharekit_register_ret), ret); clearDeviceList(); }

4.Start to search for devices.

private void onDiscovery() { int ret = shareKitManager.startDiscovery(); updateStatusHistory(getString(R.string.sharekit_start_discovery_ret), ret); clearDeviceList(); }

5.Select the recipient device and start sharing.

private void doSendText() { String text = shareText.getText().toString(); if (TextUtils.isEmpty(text)) { updateStatusHistory(getString(R.string.sharekit_content_empty)); return; } String deviceName = destDevice.getText().toString(); if (TextUtils.isEmpty(deviceName)) { updateStatusHistory(getString(R.string.sharekit_device_name_empty)); return; } ShareBean shareBean = new ShareBean(text); doSend(deviceName, shareBean); } private void doSendFiles() { String text = shareText.getText().toString(); if (TextUtils.isEmpty(text)) { updateStatusHistory(getString(R.string.sharekit_file_list_empty)); return; } String deviceName = destDevice.getText().toString(); if (TextUtils.isEmpty(deviceName)) { updateStatusHistory(getString(R.string.sharekit_device_name_empty)); return; } List<Uri> uris = getFileUris(text); if (uris.isEmpty()) { updateStatusHistory(getString(R.string.sharekit_no_files_left)); return; } ShareBean shareBean = new ShareBean(uris); doSend(deviceName, shareBean); } private void doSend(String deviceName, ShareBean shareBean) { updateStatusHistory(getString(R.string.sharekit_start_found, deviceName)); synchronized (lock) { boolean isDeviceFound = false; for (NearByDeviceEx device : deviceMap.values()) { if (deviceName.equals(device.getBtName())) { int ret = shareKitManager.doSend(device, shareBean); updateStatusHistory(getString(R.string.sharekit_device_desc, deviceName, device.getCommonDeviceId().substring(0, COMMON_ID_PRINT_LENGTH))); updateStatusHistory(getString(R.string.sharekit_try_send, deviceName), ret); isDeviceFound = true; } } if (!isDeviceFound) { updateStatusHistory(getString(R.string.sharekit_device_alread_lost)); } } }

6.Cancel sharing.

private void onCancel() { String deviceName = destDevice.getText().toString(); if (TextUtils.isEmpty(deviceName)) { return; } updateStatusHistory(getString(R.string.sharekit_start_found, deviceName)); synchronized (lock) { List<NearByDeviceEx> deviceList = shareKitManager.getDeviceList(); for (NearByDeviceEx device : deviceList) { if (deviceName.equals(device.getBtName())) { int ret = shareKitManager.cancelSend(device); updateStatusHistory(getString(R.string.sharekit_device_desc, deviceName, device.getCommonDeviceId().substring(0, COMMON_ID_PRINT_LENGTH))); updateStatusHistory(getString(R.string.sharekit_try_cancel_ret, deviceName), ret); } } } }

7.Stop searching.

private void onStopDiscovery() { int ret = shareKitManager.stopDiscovery(); updateStatusHistory(getString(R.string.sharekit_stop_discover_ret), ret); clearDeviceList(); }

8.Deregister the callback interface and deinitialize Share Engine.

Deregister the callback interface and Share Engine in the onDestory method of the activity.

protected void onDestroy() { /* unregister the callback and uninit the shareKitManager */ shareKitManager.unregisterCallback(callback); shareKitManager.uninit(); super.onDestroy(); }

9.Applying for Permissions

Apply for the following permissions:

android.permission.READ_EXTERNAL_STORAGE android.permission.ACCESS_FINE_LOCATION

Well done. You have successfully completed this codelab and learned:

1.Enable Huawei Share on your Huawei device.

Huawei Share settings

2.Start the ShareKitDemo program on the third-party Android device. The following figure shows the program UI.

ShareKitDemo program UI

Click the initialize, register callback, or search buttons one by one to search for available devices.

3.Select the recipient device and the files to be shared, and click the share button. The Huawei device prompts you to accept the shared file.

Receive the file on the Huawei device.

4.Huawei Share displays the list of received files.

List of received files in Huawei Share

For more information, please click the following link:
Huawei Share Service Introduction

For details about the APIs, see the API Description for Integrating Share Engine into Android Devices.

For details about the sample code, see the Sample Code for Integrating Share Engine into Android Devices.

Download

Code copied