想成为优秀的技术人员你必须做到的几件事情

📅 发布时间:2026/7/7 15:08:45 👁️ 浏览次数:
想成为优秀的技术人员你必须做到的几件事情
排坪晒们功能优势无需网络近场快传技术无需依赖网络在无网络或网络不稳定的环境下玩家也能轻松分享游戏资源或组队。玩家只需将两台支持近场快传的设备靠近即可自动建立连接并开始传输操作简单快捷。高速传输游戏近场快传技术传输速度最高可达100MB/S能够显著提升游戏资源的传输效率。跨设备兼容游戏近场快传支持多种设备平台如Phone、PC/2in1、Tablet实现无缝传输。使用步骤设备靠近玩家需要将支持游戏近场快传的设备靠近以确保能够建立稳定的通信链路。选择传输内容在游戏内或专门的传输应用中玩家需要选择要传输的游戏资源或数据。开始传输一旦设备间建立连接玩家即可启动传输过程等待资源传输完成。接入步骤以下示例中使用的API说明详见接口文档。开发流程图片1导入模块导入Game Service Kit及公共模块。import { abilityAccessCtrl, AbilityConstant, UIAbility, common } from kit.AbilityKit;import { hilog } from kit.PerformanceAnalysisKit;import { gameNearbyTransfer } from kit.GameServiceKit;import { BusinessError } from kit.BasicServicesKit;申请权限申请ohos.permission.DISTRIBUTED_DATASYNC权限用于设备发现。let atManager abilityAccessCtrl.createAtManager();let uiAbilityContext this.getUIContext()?.getHostContext() as common.UIAbilityContext;try {atManager.requestPermissionsFromUser(uiAbilityContext, [ohos.permission.DISTRIBUTED_DATASYNC]).then((data) {hilog.info(0x0000, nearby, %{public}s, data: JSON.stringify(data));}).catch((err: object) {hilog.error(0x0000, nearby, %{public}s, err: JSON.stringify(err));})} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}创建游戏近场快传服务并注册相关回调导入相关模块后需先调用create接口创建游戏近场快传服务然后注册各回调事件。public create() {let uiAbilityContext this.getUIContext()?.getHostContext() as common.UIAbilityContext;let initParam: gameNearbyTransfer.CreateParameters {abilityName: uiAbilityContext.abilityInfo.name,context: uiAbilityContext,moduleName: uiAbilityContext.abilityInfo.moduleName,needShowSystemUI: false // 是否展示系统UItrue为展示false为不展示默认为false};try {gameNearbyTransfer.create(initParam).then((createResult) {hilog.info(0x0000, [nearby], %{public}s, create success createResult.localDeviceName);this.registerCallback();}).catch((err: BusinessError) {hilog.error(0x0000, [nearby], %{public}s, create error (err as Error).message);})} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}// 注册监听public registerCallback() {try {gameNearbyTransfer.on(connectNotify, connectNotifyCallBack);gameNearbyTransfer.on(receivePackageInfo, receivePackageInfoCallBack);gameNearbyTransfer.on(transferNotify, transferNotifyCallBack);gameNearbyTransfer.on(error, errorCallBack);} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}function connectNotifyCallBack(callback: gameNearbyTransfer.ConnectNotification) {}function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {}function transferNotifyCallBack(callback: gameNearbyTransfer.TransferNotification) {}function errorCallBack(callback: gameNearbyTransfer.ReturnResult) {}接收端接受协同接收端实现onCollaborate回调回调中调用acceptCollaboration接口接受协同。export default class EntryAbility extends UIAbility {// 协同回调onCollaborate(wantParam: Record): AbilityConstant.CollaborateResult {try {gameNearbyTransfer.acceptCollaboration(wantParam);} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}hilog.info(0x0000, [nearby], %{public}s, onCollaborate: accept collaborate);return AbilityConstant.CollaborateResult.ACCEPT;}}接收端发布自身游戏近场快传服务接收端调用publishNearbyGame接口发布自身游戏近场快传服务。try {gameNearbyTransfer.publishNearbyGame().then(() {hilog.info(0x0000, [nearby], %{public}s, publishNearbyGame success);}).catch((err: BusinessError) {hilog.error(0x0000, [nearby], %{public}s, publishNearbyGame error);})} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}发送端绑定接收端游戏近场快传服务发送端绑定接收端游戏近场快传服务支持如下两种方式自动绑定和选择绑定。这里以自动绑定为例发送端调用autoBindNearbyGame接口自动绑定接收端近场快传服务。try {gameNearbyTransfer.autoBindNearbyGame().then(() {hilog.info(0x0000, [nearby], %{public}s, autoBindNearbyGame success);}).catch((err: BusinessError) {hilog.error(0x0000, [nearby], %{public}s, autoBindNearbyGame error);})} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}接收端发送自身文件信息收到建链成功回调后接收端调用sendPackageInfo接口发送自身文件如版本信息、包信息包名和扩展数据的字符长度范围[0, 2048]、版本号字符长度范围[0, 256]、文件列表最多10000条。function connectNotifyCallBack(callback: gameNearbyTransfer.ConnectNotification) {if (callback.connectState gameNearbyTransfer.ConnectState.CONNECTED) {// 连接成功回调判断当前是否为接收端。若当前设备为接收端请设置为true否则请设置为false。let isReceive true;if (!isReceive) {return;}// 接收端收到连接回调后需要处理,发送资源包信息给发送端let packageInfo: gameNearbyTransfer.PackageInfo {name: com.huawei.xxxx,files: [],version: 1.1.0,extraData: extraData};let fileInfo: gameNearbyTransfer.FileInfo {path: /xxx/xxxx/files/data.zip, // 使用沙箱路径hash: fileHash // 可选};packageInfo.files?.push(fileInfo);try {gameNearbyTransfer.sendPackageInfo(packageInfo).then(() {hilog.info(0x0000, [nearby], %{public}s, sendPackageInfo success);}).catch((err: BusinessError) {hilog.error(0x0000, [nearby], %{public}s, sendPackageInfo error);});} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}}发送端对比后传输资源包发送端收到接收端发送的版本信息后进行对比调用replyPackageInfoResult上报对比结果根据对比结果决定是否需要调用transferPackageData接口发送资源包数据。function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {// 比较版本,决定是否需要发送资源包,也可以比较文件hashlet packageInfoResult: gameNearbyTransfer.PackageInfoResult {packageInfoResultCode: gameNearbyTransfer.PackageInfoResultCode.PACKAGE_AVAILABLE_COMPARED};try {// 上报对比结果gameNearbyTransfer.replyPackageInfoResult(packageInfoResult).then(() {let packageData: gameNearbyTransfer.PackageData {name: com.huawei.gamenearbydemo,version: 1.0.0,files: [{srcPath: /data/xxxx/a.zip, // srcPath是需要发送文件的沙箱路径详情请参见应用沙箱目录。destPath: xxxx/a.zip // destPath是接收文件的自定义路径完整的沙箱路径是fileStoragePathdestPath详情请参见应用沙箱目录。}]}try {// 发送资源包gameNearbyTransfer.transferPackageData(packageData).then(() {// 发送成功}).catch((err: BusinessError) {// 发送异常hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);});} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}).catch((err: BusinessError) {// 上报异常hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);});} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}处理资源包传输进度信息发送端和接收端在传输回调中处理传输进度信息。function transferNotifyCallBack(callback: gameNearbyTransfer.TransferNotification) {if (callback.transferState gameNearbyTransfer.TransferState.SEND_PROCESS) {// 处理发送进度,如显示进度条和速率}if (callback.transferState gameNearbyTransfer.TransferState.SEND_FINISH) {// 发送完成}if (callback.transferState gameNearbyTransfer.TransferState.RECEIVE_PROCESS) {// 处理接收进度,如显示进度条和速率}if (callback.transferState gameNearbyTransfer.TransferState.RECEIVE_FINISH) {// 接收完成,获取到资源包存储的沙箱路径let fileStoragePath callback.fileStoragePath;// 对fileStoragePath下的文件做处理}}处理已接收资源包后销毁服务对已接收数据做好处理或转移后调用destroy接口销毁服务。若服务销毁后再次使用近场快传服务需重新创建游戏近场快传服务并注册相关回调。public destroy() {// 取消回调注册this.unregisterCallback();// 销毁服务try {gameNearbyTransfer.destroy().then(() {hilog.info(0x0000, [nearby], %{public}s, destroy success);}).catch((err: Error) {hilog.error(0x0000, [nearby], %{public}s, destroy error (err as Error).message);});} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}public unregisterCallback() {try {gameNearbyTransfer.off(connectNotify, connectNotifyCallBack);gameNearbyTransfer.off(receivePackageInfo, receivePackageInfoCallBack);gameNearbyTransfer.off(transferNotify, transferNotifyCallBack);gameNearbyTransfer.off(error, errorCallBack);// 发送端选择手动绑定接收端且已订阅discovery事件gameNearbyTransfer.off(discovery, discoveryCallBack);} catch (err) {hilog.error(0x0000, [nearby], %{public}s, error (err as Error).message);}}function connectNotifyCallBack(callback: gameNearbyTransfer.ConnectNotification) {}function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {}function transferNotifyCallBack(callback: gameNearbyTransfer.TransferNotification) {}function errorCallBack(callback: gameNearbyTransfer.ReturnResult) {}function discoveryCallBack(callback: gameNearbyTransfer.DiscoveryResult) {