传输资源包
=====

游戏近场快传支持已安装游戏的玩家间传输游戏内资源包，节省玩家下载资源包所需的流量和时间。

#### 业务流程

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/3c/v3/mNjhujWtSXilKHruO99Odg/zh-cn_image_0000002594669198.png?HW-CC-KV=V1&HW-CC-Date=20260612T073610Z&HW-CC-Expire=86400&HW-CC-Sign=5E2283057D5C4E0965FB3B15672D9ADF9F82ECC45576C585E53AA4DE8DCED0A9)

1.  发送端和接收端调用[create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfercreate)创建游戏近场快传服务。
    
2.  创建成功后，游戏客户端调用以下接口注册监听。
    
    *   注册连接通知监听接口：[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonconnectnotify)('connectNotify')
    *   （发送端选择绑定接收端情况下需调用）注册发现结果事件监听接口：[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferondiscovery)('discovery')
    *   注册收到包信息监听接口：[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonreceivepackageinfo)('receivePackageInfo')
    *   注册传输通知监听接口：[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferontransfernotify)('transferNotify')
    *   注册错误事件监听接口：[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonerror)('error')
3.  接收端调用[publishNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferpublishnearbygame)发布自身近场快传服务。
    
4.  绑定接收端，支持如下两种方式。
    
    *   自动绑定：
        
        发送端调用[autoBindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferautobindnearbygame)自动绑定附近设备（搜索并绑定附近10米内第一个发现的近场快传服务）。
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/89/v3/l3GalY2WRIy_w6IwVAtLHQ/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073610Z&HW-CC-Expire=86400&HW-CC-Sign=F9F128B83CA461246CB4A347F88C2F3E67325A4A44A70C62FEA07C76789EB2DB)
        
        自动绑定操作2分钟内有效，超时需重新调用接口。
        
    *   选择绑定：
        
        发送端调用[discoveryNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdiscoverynearbygame)发现附近设备，发现操作完成后将收到discovery事件回调，获得可绑定的设备列表供玩家选择，调用[bindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferbindnearbygame)接口绑定玩家选定的接收端设备。
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/fa/v3/xhFr3hOOQL6dwCKzWkVwGw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073610Z&HW-CC-Expire=86400&HW-CC-Sign=5B504DFD28A736DD811BDF7B895EA3688D6D06795D3488DAE392E9712725971A)
        
        发现操作2分钟内有效，超时需重新调用接口。
        
5.  接收端收到UIAbility的[onCollaborate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability#oncollaborate18)回调后调用[acceptCollaboration](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferacceptcollaboration)接受协同。
    
6.  接收端收到建链成功connectNotify事件回调。
    
7.  接收端调用[sendPackageInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfersendpackageinfo)发送自身文件信息，如版本信息、包信息。
    
8.  发送端收到receivePackageInfo事件回调。
    
9.  发送端比较版本并调用[replyPackageInfoResult](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferreplypackageinforesult)上报对比结果。
    
10.  如发送端对比结果为需要发送，则调用[transferPackageData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfertransferpackagedata)向接收端发送需要传输的资源包。
     
11.  接收端可在transferNotify回调中获取当前已传输的包体大小、包体总大小、传输速率、传输剩余时间等信息，传输完成可获取已接收资源包的存储目录，对传输完成的资源文件做处理。
     
12.  处理传输完成的资源文件后，可调用[destroy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdestroy)销毁服务。
     
     ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/cc/v3/ibQROH7KTzi946Q0HW_ExQ/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073610Z&HW-CC-Expire=86400&HW-CC-Sign=D6E9E6502B9041F685C7EF9783F5115647808F3169E5FC731B270F71601A4682)
     
     *   destroy接口会清除已接收数据，请确保对已接收数据做好处理或转移后再调用该接口。
         
     *   每次调用[create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfercreate)接口会自动清理自身历史数据。
         
     

#### 接口说明

具体API说明详见[接口文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer)。

| 接口名 | 描述 |
| :-- | :-- |
| [create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfercreate)(createParameters: CreateParameters): Promise<CreateResult> | 创建游戏近场快传服务。 |
| [on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonconnectnotify)(type: 'connectNotify', callback: Callback<ConnectNotification>): void | 订阅连接通知事件。 |
| [off](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferoffconnectnotify)(type: 'connectNotify', callback?: Callback<ConnectNotification>): void | 取消订阅连接通知事件。 |
| [on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferondiscovery)(type: 'discovery', callback: Callback<DiscoveryResult>): void | 订阅发现结果事件。 |
| [off](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferoffdiscovery)(type: 'discovery', callback?: Callback<DiscoveryResult>): void | 取消订阅发现结果事件。 |
| [on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonreceivepackageinfo)(type: 'receivePackageInfo', callback: Callback<PackageInfo>): void | 订阅收到包信息事件。 |
| [off](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferoffreceivepackageinfo)(type: 'receivePackageInfo', callback?: Callback<PackageInfo>): void | 取消订阅收到包信息事件。 |
| [on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferontransfernotify)(type: 'transferNotify', callback: Callback<TransferNotification>): void | 订阅传输通知事件。 |
| [off](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferofftransfernotify)(type: 'transferNotify', callback?: Callback<TransferNotification>): void | 取消订阅传输通知事件。 |
| [on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferonerror)(type: 'error', callback: Callback<ReturnResult>): void | 订阅错误事件。 |
| [off](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferofferror)(type: 'error', callback?: Callback<ReturnResult>): void | 取消订阅错误事件。 |
| [publishNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferpublishnearbygame)(): Promise<void> | 发布近场快传服务。 |
| [autoBindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferautobindnearbygame)(): Promise<void> | 自动绑定近场快传服务。 |
| [discoveryNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdiscoverynearbygame)(): Promise<void> | 发现近场快传服务。 |
| [bindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferbindnearbygame)(bindParameters: BindParameters): Promise<void> | 绑定指定近场快传服务。 |
| [acceptCollaboration](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferacceptcollaboration)(acceptParameters: Record<string, object>): Promise<void> | 接受协同。 |
| [sendPackageInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfersendpackageinfo)(packageInfo: PackageInfo): Promise<void> | 接收端发送自身文件信息。 |
| [replyPackageInfoResult](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferreplypackageinforesult)(packageInfoResult: PackageInfoResult): Promise<void> | 上报包信息对比结果。 |
| [transferPackageData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfertransferpackagedata)(packageData: PackageData): Promise<void> | 传输包数据。 |
| [destroy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdestroy)(): Promise<void> | 销毁游戏近场快传服务。 |

#### 接入步骤

#### \[h2\]导入模块

导入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';

#### \[h2\]申请权限

申请ohos.permission.DISTRIBUTED\_DATASYNC权限用于设备发现，详情可参考[向用户申请授权](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/request-user-authorization)。

let atManager = abilityAccessCtrl.createAtManager();
let uiAbilityContext = this.getUIContext()?.getHostContext() as common.UIAbilityContext;
try {
  atManager.requestPermissionsFromUser(uiAbilityContext, \['ohos.permission.DISTRIBUTED\_DATASYNC'\]).then((data) => {
    if (data.authResults\[0\] === 0) {
      // 用户授权，可以继续访问目标操作。
      hilog.info(0x0000, 'nearby', \`ohos.permission.DISTRIBUTED\_DATASYNC is granted by user.\`);
    } else {
      // 用户拒绝授权，提示用户必须授权才能访问当前功能，并引导用户到系统设置中打开相应的权限。
      return;
    }
  }).catch((err: BusinessError) => {
    hilog.error(0x0000, 'nearby', '%{public}s', \`Failed to request permissions from user, code: ${err.code}, message: ${err.message}\`);
  })
} catch (error) {
  let err = error as BusinessError;
  hilog.error(0x0000, 'nearby', \`request permissions from user exception. Code: ${err.code}, message: ${err.message}\`);
}

#### \[h2\]创建游戏近场快传服务并注册相关回调

导入相关模块后，需先调用[create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfercreate)接口创建游戏近场快传服务，然后注册各回调事件。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/98/v3/LRl9hCxlTdW3UZ_X4S9gBA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073610Z&HW-CC-Expire=86400&HW-CC-Sign=6891B3CC1209F160A7806A2B99C88D7543E40A9EA474A993AF18DB71575C511F)

[create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfercreate)接口是调用其他接口的前提，如果未创建游戏近场快传服务或创建失败，将无法调用其他接口。

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,
  };

  try {
    gameNearbyTransfer.create(initParam).then((createResult) => {
      hilog.info(0x0000, 'nearby', \`create success localDeviceName ${createResult.localDeviceName}\`);
      this.registerCallback();
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'nearby', \`create failed. Code: ${err.code}, message: ${err.message}\`);
    });
  } catch (error) {
    let err = error as BusinessError;
    hilog.error(0x0000, 'nearby', \`create exception. Code: ${err.code}, message: ${err.message}\`);
  }
}

// 注册监听
public registerCallback() {
  try {
    gameNearbyTransfer.on('connectNotify', connectNotifyCallBack);
    gameNearbyTransfer.on('receivePackageInfo', receivePackageInfoCallBack);
    gameNearbyTransfer.on('transferNotify', transferNotifyCallBack);
    gameNearbyTransfer.on('error', errorCallBack);
  } catch (error) {
    let err = error as BusinessError;
    hilog.error(0x0000, 'nearby', \`registerCallback error. Code: ${err.code}, message: ${err.message}\`);
  }
}

function connectNotifyCallBack(callback: gameNearbyTransfer.ConnectNotification) {
  // 连接状态回调，接收端收到建链成功回调后，在此处调用sendPackageInfo接口发送自身文件信息，如版本信息、包信息
  hilog.info(0x0000, 'nearby', \`connectNotify. State: ${callback.connectState}\`);
}

function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {
  // 接收包信息回调，发送端收到接收端发送的版本信息后进行对比，根据对比结果决定是否需要传输资源包数据。
  hilog.info(0x0000, 'nearby', \`get package info. version: ${callback.version}\`);
}

function transferNotifyCallBack(callback: gameNearbyTransfer.TransferNotification) {
  // 传输回调，处理传输进度信息
  hilog.info(0x0000, 'nearby', \`get transfer state: ${callback.transferState}\`);
}

function errorCallBack(callback: gameNearbyTransfer.ReturnResult) {
  // 异常信息回调，处理相关异常信息
  hilog.error(0x0000, 'nearby', \`Error info. Code: ${callback.code}, message: ${callback.message}\`);
}

#### \[h2\]接收端接受协同

接收端实现[onCollaborate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability#oncollaborate18)回调，回调中调用[acceptCollaboration](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferacceptcollaboration)接口接受协同。

export default class EntryAbility extends UIAbility {
  // 协同回调
  onCollaborate(wantParam: Record<string, Object>): AbilityConstant.CollaborateResult {
    try {
      // 接受协同
      gameNearbyTransfer.acceptCollaboration(wantParam).catch((err: BusinessError) => {
        hilog.error(0x0000, 'nearby', \`acceptCollaboration failed. Code: ${err.code}, message: ${err.message}\`);
      });
    } catch (error) {
      let err = error as BusinessError;
      hilog.error(0x0000, 'nearby', \`acceptCollaboration exception. Code: ${err.code}, message: ${err.message}\`);
    }
    return AbilityConstant.CollaborateResult.ACCEPT;
  }
}

#### \[h2\]接收端发布自身游戏近场快传服务

接收端调用[publishNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferpublishnearbygame)接口发布自身游戏近场快传服务。

try {
  gameNearbyTransfer.publishNearbyGame().then(() => {
    hilog.info(0x0000, 'nearby', \`publishNearbyGame success\`);
  }).catch((err: BusinessError) => {
    hilog.error(0x0000, 'nearby', \`publishNearbyGame failed. Code: ${err.code}, message: ${err.message}\`);
  });
} catch (error) {
  let err = error as BusinessError;
  hilog.error(0x0000, 'nearby', \`publishNearbyGame exception. Code: ${err.code}, message: ${err.message}\`);
}

#### \[h2\]发送端绑定接收端游戏近场快传服务

发送端绑定接收端游戏近场快传服务支持如下两种方式：

*   方式一：自动绑定
    
    发送端调用[autoBindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferautobindnearbygame)接口自动绑定接收端近场快传服务。
    
    try {
      // 自动绑定近场快传服务
      gameNearbyTransfer.autoBindNearbyGame().then(() => {
        hilog.info(0x0000, 'nearby', \`autoBindNearbyGame success\`);
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'nearby', \`autoBindNearbyGame failed. Code: ${err.code}, message: ${err.message}\`);
      });
    } catch (error) {
      let err = error as BusinessError;
      hilog.error(0x0000, 'nearby', \`autoBindNearbyGame exception. Code: ${err.code}, message: ${err.message}\`);
    }
    
*   方式二：选择绑定
    
    1.  发送端调用[on](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferondiscovery)('discovery')接口注册“发现设备”结果事件监听。
        
        try {
          // 订阅发现结果
          gameNearbyTransfer.on('discovery', discoveryCallBack);
        } catch (error) {
          // 订阅失败
          let err = error as BusinessError;
          hilog.error(0x0000, 'nearby', \`Failed to subscribe discovery. Code: ${err.code}, message: ${err.message}\`);
        }
        
        function discoveryCallBack(callback: gameNearbyTransfer.DiscoveryResult) {
          // 获取到发现的设备 展示设备列表
          callback.nearbyGameDevices.forEach((device: gameNearbyTransfer.NearbyGameDevice, index: number) => {
            hilog.info(0x0000, 'nearby', \`device info. name: ${device.deviceName}, index: ${index}\`);
          });
        }
        
    2.  发送端调用[discoveryNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdiscoverynearbygame)发现附近设备。
        
        try {
          gameNearbyTransfer.discoveryNearbyGame().then(() => {
            hilog.info(0x0000, 'nearby', \`discoveryNearbyGame success.\`);
          }).catch((err: BusinessError) => {
            hilog.error(0x0000, 'nearby', \`discoveryNearbyGame failed. Code: ${err.code}, message: ${err.message}\`);
          });
        } catch (error) {
          let err = error as BusinessError;
          hilog.error(0x0000, 'nearby', \`discoveryNearbyGame exception. Code: ${err.code}, message: ${err.message}\`);
        }
        
    3.  “发现设备”操作完成后将收到discovery事件回调，获得发现的设备列表供玩家选择，调用[bindNearbyGame](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferbindnearbygame)接口绑定玩家选定的接收端设备。
        
        public bindNearbyGame(deviceInfo: gameNearbyTransfer.NearbyGameDevice) {
          let bindInfo: gameNearbyTransfer.BindParameters = {
            deviceId: deviceInfo.deviceId,
            networkId: deviceInfo.networkId
          };
          try {
            gameNearbyTransfer.bindNearbyGame(bindInfo).then(() => {
              hilog.info(0x0000, 'nearby', \`bindNearbyGame success\`);
            }).catch((err: BusinessError) => {
              hilog.error(0x0000, 'nearby', \`bindNearbyGame failed. Code: ${err.code}, message: ${err.message}\`);
            });
          } catch (error) {
            let err = error as BusinessError;
            hilog.error(0x0000, 'nearby', \`bindNearbyGame exception. Code: ${err.code}, message: ${err.message}\`);
          }
        }
        

#### \[h2\]接收端发送自身文件信息

收到建链成功回调后，接收端调用[sendPackageInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfersendpackageinfo)接口发送自身文件，如版本信息、包信息。

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', \`sendPackageInfo success\`);
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'nearby', \`sendPackageInfo failed. Code: ${err.code}, message: ${err.message}\`);
      });
    } catch (error) {
      let err = error as BusinessError;
      hilog.error(0x0000, 'nearby', \`sendPackageInfo exception. Code: ${err.code}, message: ${err.message}\`);
    }
  }
}

#### \[h2\]发送端对比后传输资源包

发送端收到接收端发送的版本信息后进行对比，调用[replyPackageInfoResult](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferreplypackageinforesult)上报对比结果，根据对比结果决定是否需要调用[transferPackageData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransfertransferpackagedata)接口发送资源包数据。

function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {
  const version = callback.version;
  hilog.info(0x0000, 'nearby', \`remote version: ${version}\`);
  // 比较版本，决定是否需要发送资源包，也可以比较文件hash
  let 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',
          destPath: 'xxxx/a.zip'
        }\] // srcPath是需要发送文件的路径，详情请参见沙箱路径。destPath为接收文件的路径，完整路径是fileStoragePath+destPath。
      };
      try {
        // 发送资源包
        gameNearbyTransfer.transferPackageData(packageData).then(() => {
          // 发送成功
        }).catch((err: BusinessError) => {
          hilog.error(0x0000, 'nearby', \`transferPackageData error Code: ${err.code}, message: ${err.message}\`);
        });
      } catch (err) {
        let error = err as BusinessError;
        hilog.error(0x0000, 'nearby', \`transferPackageData exception Code: ${error.code}, message: ${error.message}\`);
      }
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'nearby', \`replyPackageInfoResult error Code: ${err.code}, message: ${err.message}\`);
    });
  } catch (error) {
    let err = error as BusinessError;
    hilog.error(0x0000, 'nearby', \`replyPackageInfoResult exception Code: ${err.code}, message: ${err.message}\`);
  }
}

#### \[h2\]处理资源包传输进度信息

发送端和接收端在传输回调中处理传输进度信息。

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;
    hilog.info(0x0000, 'nearby', \`get transfer path: ${fileStoragePath}\`);
    // 对fileStoragePath下的文件做处理
  }
}

#### \[h2\]处理已接收资源包后销毁服务

对已接收数据做好处理或转移后，调用[destroy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/gameservice-nearbytransfer#gamenearbytransferdestroy)接口销毁服务。若服务销毁后再次使用近场快传服务，需重新[创建游戏近场快传服务并注册相关回调](#创建游戏近场快传服务并注册相关回调)。

public destroy() {
  // 取消回调注册
  this.unregisterCallback();
  // 销毁服务
  try {
    gameNearbyTransfer.destroy().then(() => {
      hilog.info(0x0000, 'nearby', \`destroy success\`);
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'nearby', \`destroy failed. Code: ${err.code}, message: ${err.message}\`);
    });
  } catch (error) {
    let err = error as BusinessError;
    hilog.error(0x0000, 'nearby', \`destroy exception. Code: ${err.code}, message: ${err.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 (error) {
    let err = error as BusinessError;
    hilog.error(0x0000, 'nearby', \`unregisterCallback error. Code: ${err.code}, message: ${err.message}\`);
  }
}

function connectNotifyCallBack(callback: gameNearbyTransfer.ConnectNotification) {
  // 连接状态回调，接收端收到建链成功回调后，在此处调用sendPackageInfo接口发送自身文件信息，如版本信息、包信息
  hilog.info(0x0000, 'nearby', \`connectNotify. State: ${callback.connectState}\`);
}

function receivePackageInfoCallBack(callback: gameNearbyTransfer.PackageInfo) {
  // 接收包信息回调，发送端收到接收端发送的版本信息后进行对比，根据对比结果决定是否需要传输资源包数据。
  hilog.info(0x0000, 'nearby', \`get package info. version: ${callback.version}\`);
}

function transferNotifyCallBack(callback: gameNearbyTransfer.TransferNotification) {
  // 传输回调，处理传输进度信息
  hilog.info(0x0000, 'nearby', \`get transfer state: ${callback.transferState}\`);
}

function errorCallBack(callback: gameNearbyTransfer.ReturnResult) {
  // 异常信息回调，处理相关异常信息
  hilog.error(0x0000, 'nearby', \`Error info. Code: ${callback.code}, message: ${callback.message}\`);
}

function discoveryCallBack(callback: gameNearbyTransfer.DiscoveryResult) {
  // 获取到发现的设备 展示设备列表
  callback.nearbyGameDevices.forEach((device: gameNearbyTransfer.NearbyGameDevice, index: number) => {
    hilog.info(0x0000, 'nearby', \`device info. name: ${device.deviceName}, index: ${index}\`);
  });
}