穿戴设备信息查询
========

// 在使用Wear Engine服务前，请导入WearEngine与相关模块
import { wearEngine } from '@kit.WearEngine';
import { BusinessError } from '@kit.BasicServicesKit';

#### 查询穿戴设备是否支持某种WearEngine能力集

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/be/v3/N79WQkICRX2hfj9Bu9FuzA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073450Z&HW-CC-Expire=86400&HW-CC-Sign=9873B8D1BDECBA6C01A862116B33F7A361C8BF761EC9EF3059B5227920B3840C)

该接口的调用需要在开发者联盟申请设备基础信息权限（请参考[申请接入Wear Engine服务](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/wearengine_apply)）。

通过[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的方法[isWearEngineCapabilitySupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#iswearenginecapabilitysupported)查询穿戴设备是否支持某种WearEngine能力集。

1.  应用调用[wearEngine](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api)中的[getDeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#wearenginegetdeviceclient)方法，获取[DeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#deviceclient)对象。
    
    let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext());
    
2.  调用[getConnectedDevices](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#getconnecteddevices)方法，获取已连接的设备列表。
    
    let deviceList: wearEngine.Device\[\] = \[\];
    deviceClient.getConnectedDevices().then(devices => {
      // 存储已连接的设备列表
      deviceList = devices;
      console.info(\`Succeeded in getting deviceList, devices number is ${deviceList.length}\`);
    }).catch((error: BusinessError) => {
      // 处理调用失败时捕获到的异常
      console.error(\`Failed to get deviceList. Code is ${error.code}, message is ${error.message}\`);
    });
    
3.  从设备列表中选取需要操作的设备。
    
4.  调用[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的[isWearEngineCapabilitySupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#iswearenginecapabilitysupported)接口可查询该设备是否支持传入的WearEngine能力（true：支持；false：不支持），以P2P能力为例。
    
    if (deviceList.length > 0) {
      // 步骤3 从得到的设备列表中选取目标设备，并定义为device(假设数组中存在已连接设备且第一位即为目标设备)
      let targetDevice: wearEngine.Device = deviceList\[0\];
    
      // 步骤4 调用设备的方法查询是否支持某种WearEngine能力（以P2P为例）
      targetDevice.isWearEngineCapabilitySupported(wearEngine.WearEngineCapability.P2P\_COMMUNICATION).then((isSupportP2P) => {
        console.info(\`Succeeded in checking p2p capability, result is ${isSupportP2P}\`);
      }).catch((error: BusinessError) => {
        console.error(\`Failed to check p2p capability. Code is ${error.code}, message is ${error.message}\`);
      });
    }
    

#### 查询穿戴设备是否支持某种Device能力集

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e9/v3/3ptKZmCNQhu3u1UVtrHgYA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073450Z&HW-CC-Expire=86400&HW-CC-Sign=D5926598B2CA47CCB416CC85DA9062C68D4EC8E7C8D198A2B865ECE9A60C875E)

该接口的调用需要在开发者联盟申请设备基础信息权限（请参考[申请接入Wear Engine服务](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/wearengine_apply)）。

通过[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的方法[isDeviceCapabilitySupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#isdevicecapabilitysupported)查询穿戴设备是否支持某种Device能力集。

1.  应用调用[wearEngine](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api)中的[getDeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#wearenginegetdeviceclient)方法，获取[DeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#deviceclient)对象。
    
    let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext());
    
2.  调用[getConnectedDevices](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#getconnecteddevices)方法，获取已连接的设备列表。
    
    let deviceList: wearEngine.Device\[\] = \[\];
    deviceClient.getConnectedDevices().then(devices => {
      // 存储已连接的设备列表
      deviceList = devices;
      console.info(\`Succeeded in getting deviceList, devices number is ${deviceList.length}\`);
    }).catch((error: BusinessError) => {
      // 处理调用失败时捕获到的异常
      console.error(\`Failed to get deviceList. Code is ${error.code}, message is ${error.message}\`);
    });
    
3.  从设备列表中选取需要操作的设备。
    
4.  调用[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的[isDeviceCapabilitySupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#isdevicecapabilitysupported)接口可查询该设备是否支持传入的Device能力（true：支持；false：不支持）。
    
    if (deviceList.length > 0) {
      // 步骤3 从得到的设备列表中选取目标设备，并定义为device(假设数组中存在已连接设备且第一位即为目标设备)
      let targetDevice: wearEngine.Device = deviceList\[0\];
    
      // 步骤4 调用设备的方法查询是否支持某种Device能力（以是否支持应用安装为例）
      targetDevice.isDeviceCapabilitySupported(wearEngine.DeviceCapability.APP\_INSTALLATION).then((isSupportInstall) => {
        console.info(\`Succeeded in checking install app capability, result is ${isSupportInstall}\`);
      }).catch((error: BusinessError) => {
        console.error(\`Failed to check install app capability. Code is ${error.code}, message is ${error.message}\`);
      });
    }
    

#### 查询设备SN

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/99/v3/1MqVIqAtRtyowMpyAAPhyg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073450Z&HW-CC-Expire=86400&HW-CC-Sign=0A33588B2B857B8A2016BD1EFC59F311887AEC044B071AF564E21E3AC0CB7EAE)

该接口的调用需要在开发者联盟申请设备标识符权限（受限开放）并获得用户授权（请参考[申请接入Wear Engine服务](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/wearengine_apply)）。

通过[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的方法[getSerialNumber](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#getserialnumber)查询穿戴设备的SN。

1.  应用调用[wearEngine](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api)中的[getDeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#wearenginegetdeviceclient)方法，获取[DeviceClient](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#deviceclient)对象。
    
    let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext());
    
2.  调用[getConnectedDevices](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#getconnecteddevices)方法，获取已连接的设备列表。
    
    let deviceList: wearEngine.Device\[\] = \[\];
    deviceClient.getConnectedDevices().then(devices => {
      // 存储已连接的设备列表
      deviceList = devices;
      console.info(\`Succeeded in getting deviceList, devices number is ${deviceList.length}\`);
    }).catch((error: BusinessError) => {
      // 处理调用失败时捕获到的异常
      console.error(\`Failed to get deviceList. Code is ${error.code}, message is ${error.message}\`);
    });
    
3.  从设备列表中选取需要操作的设备。
    
4.  调用[Device](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#device)对象中的方法[getSerialNumber](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/wearengine_api#getserialnumber)查询穿戴设备的SN。
    
    if (deviceList.length > 0) {
      // 步骤3 从得到的设备列表中选取目标设备，并定义为device(假设数组中存在已连接设备且第一位即为目标设备)
      let targetDevice: wearEngine.Device = deviceList\[0\];
    
      // 步骤4 调用设备的方法查询SN
      targetDevice.getSerialNumber().then((sn) => {
        console.info(\`Succeeded in getting device SN, result is ${sn}\`);
      }).catch((error: BusinessError) => {
        console.error(\`Failed to get device SN. Code is ${error.code}, message is ${error.message}\`);
      });
    }