Huawei Share 作为跨设备传输文件的解决方案,通过蓝牙实现设备之间发现及连接鉴权,建立P2P WiFi通道,实现手机、PC、第三方设备间文件互传。在第三方设备处理能力及传输环境有保障的情况下,稳定传输速度超过80MB/s。
Huawei Share能力现已通过深度封装,以极简Kit面向应用APP、智能设备开发者开放。PC、打印机、相机等众多硬件设备均可集成Huawei Share,实现跨设备文件传输。Share Kit提供Android、Linux、Windows 3种SDK开发包,涵盖华为手机应用集成、Android设备集成、Linux设备集成、Windows PC应用集成等主流集成方式,帮助开发者快速集成Huawei Share。在这个Codelab中,主要介绍Android设备如何使用Huawei Share功能。实现该程序后,可以支持Android设备向华为设备发送文件或文本内容。

在这个Codelab中,你将创建Demo Project,并将Share Kit的SDK集成到Demo Project中,以及完成Share Kit服务的整体流程搭建。

您将会学到什么

硬件要求

软件要求

需要的知识点

集成Share Kit能力,需要完成以下准备工作:

1、集成SDK。
将获取好的sdk包放在app的libs目录下,在app的build.gradle文件中做如下配置:

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' }

另外在把app的build.gradle文件中的minSdkVersion版本改为26,如下图所示:

点击同步工程按钮,进行工程同步,如下图所示:

同步成功后,如下图所示:

2、初始化ShareKit
在Activity的onCreate方法中进行初始化

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、注册回调接口
ShareKit接口回调处理函数,在检查到设备出现、消失、状态变化时被调用。

/* ShareKit callback interface,will be called when device found or disappear, and status change */ 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_sate, 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、开始搜索设备

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

5、选择设备发起分享

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、取消分享

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、停止搜索

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

8、注销回调接口及去初始化ShareKit
在Activity的onDestory方法中注销回调接口及去初始化ShareKit。

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

干得好,你已经成功完成了Codelab并学到了:

  1. 首先需要在华为设备上打开Huawei Share功能:
  2. 在第三方Android设备启动ShareKitDemo程序,APP显示界面如下:
  3. 点击【初始化】、【注册回调】、【开始搜索】按钮后,设备开始搜索可以共享文件的设备。

  4. 选择对端设备及分享文件后,点击Demo程序【开始分享】按钮,华为设备显示接收到分享文件,提示用户选择是否接收:
  5. 选择接收文件后,Huawei Share界面可以显示接收的文件列表:

您可以阅读下面链接,了解更多相关的信息。
Huawei Share 业务介绍

接口详细描述参见《Huawei Share Android集成API接口描述》。

详细代码示例可参考《Huawei Share Android 设备集成示例代码》

已复制代码