一、结论

隔空传送是API20,也就是6.0系统上开放的系统功能接口。
通过在发起设备正面摄像头前,打开手掌后攥成拳头,来手势激活隔空传送的发起,在另外一台设备的正面摄像头前,将拳头展开。完整整个隔空传送的闭环分享操作。
需要注意的是,两个设备同账号,会自动分享。不同账号,一个小时内首次会有确认提示。
并且该功能,需要在设置中进行开启。(设置搜索框,搜索隔空传送即可找到入口,还会有手势引导学习,可以看下)
隔空传送的接口整体就分为三个:
(1)监听隔空传送harmonyShare.on(‘gesturesShare’, this.onGesturesShare )
(2)取消监听隔空传送harmonyShare.off(‘gesturesShare’, this.onGesturesShare )
(3)收到隔空传送回调后,处理要分享的信息,调用分享发送接口。onGesturesShare 回调函数中的shareData分享包和调用share接口。
当监听到隔空传送发起操作时,就可以收到该操作的时机,时机指的就是分享回调。此时可以根据分享包shareData的参数信息,进行配置,比如分享图片,还是视频,还是链接等等。shareData中的参数非常多,可以实现不同的分享效果和展示样式,具体参考文末的链接。
当不需要处理分享时,关闭监听即可。整体逻辑非常简单。
二、代码实现和详细解释
核心工具类HarmonyShareMgr,碰一碰图片分享举例:
import { harmonyShare, systemShare } from "@kit.ShareKit";
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
/**
* 鸿蒙系统分享工具类
*/
export class HarmonyShareMgr{
private TAG: string = "HarmonyShareMgr";
private static mHarmonyShareMgr: HarmonyShareMgr | null = null;
public static Ins(){
if(!HarmonyShareMgr.mHarmonyShareMgr){
HarmonyShareMgr.mHarmonyShareMgr = new HarmonyShareMgr();
}
return HarmonyShareMgr.mHarmonyShareMgr;
}
// 分享图片Uri
private mImageUri: string = "";
// 分享类型
private mShareType: string = "";
/**
* 设置图片分享信息包
*/
public setImageShareData(imageUri: string){
this.mImageUri = imageUri;
this.mShareType = utd.UniformDataType.JPEG;
}
/**
* 隔空传送回调
*/
private onGesturesShare = (shareTarget: harmonyShare.SharableTarget)=>{
// 打包
const shareData = new systemShare.SharedData({
utd: this.mShareType,
uri: this.mImageUri,
});
// 发送分享包
shareTarget.share(shareData);
}
/**
* 监听隔空传送
*/
public registerGesturesShare(){
console.log(this.TAG, " registerGesturesShare");
// API20
harmonyShare.on('gesturesShare', this.onGesturesShare);
}
/**
* 取消监听隔空传送
*/
public unRegisterGesturesShare(){
console.log(this.TAG, " unRegisterGesturesShare");
// API20
harmonyShare.off('gesturesShare', this.onGesturesShare);
}
}
测试页面内调用:
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { HarmonyShareMgr } from '../mgr/HarmonyShareMgr';
/**
* 鸿蒙分享测试页面
*/
@Entry
@Component
struct HarmonyShareTestPage {
private TAG: string = "HarmonyShareTestPage";
onPageShow(): void {
// 当前界面显示时,进行隔空传送监听
HarmonyShareMgr.Ins().registerGesturesShare();
}
onPageHide(): void {
HarmonyShareMgr.Ins().unRegisterGesturesShare();
}
onClickSelectPhoto = ()=>{
try {
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
// 设置筛选过滤条件
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
// 选择用户选择数量
PhotoSelectOptions.maxSelectNumber = 1;
// 实例化图片选择器
let photoPicker = new photoAccessHelper.PhotoViewPicker();
// 唤起安全相册组件
photoPicker.select(PhotoSelectOptions, (err: BusinessError, PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
if (err) {
console.error(this.TAG, "onClickSelectPhoto photoPicker.select error:" + JSON.stringify(err));
return;
}
// 用户选择确认后,会回调到这里。
console.info(this.TAG, "onClickSelectPhoto photoPicker.select successfully:" + JSON.stringify(PhotoSelectResult));
// 调用碰一碰分享的图片分享赋值
HarmonyShareMgr.Ins().setImageShareData(PhotoSelectResult.photoUris[0]);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(this.TAG, "onClickSelectPhoto photoPicker.select catch failed:" + JSON.stringify(err));
}
}
build() {
Row(){
Button('点击唤起相册选择')
.onClick(this.onClickSelectPhoto)
}
.justifyContent(FlexAlign.Center)
.size({
width: "100%",
height: "100%"
})
}
}
三、引用资料地址
空传送与隔空截屏的联动官方文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/gestures-share-open#section32701456165015
https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-application-gesture-share
可信任设备间传输:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/gestures-share-trust
分享信息包:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#section816451553012