欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

鸿蒙开发(NEXT/API 12) [联网可穿戴设备查询] 手机端应用开发

最编程 2024-10-01 07:47:23
...

Wear Engine提供查询用户已连接的穿戴设备列表(即支持Wear Engine能力且与手机侧运动健康App处于连接状态的穿戴设备)的接口。

建议开发者在使用Wear Engine其他API接口前先实现该接口功能。

  1. 应用调用[wearEngine] 中的[getDeviceClient] 方法,获取[DeviceClient] 对象。

  2. 调用[getConnectedDevices] 方法,查询用户已连接的穿戴设备列表。

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

// 步骤1:获取DeviceClient对象
// getContext(this) 表示应用上下文Context对象
let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(getContext(this));
// 创建一个设备列表用于存储返回的设备
let deviceList: wearEngine.Device[] = [];

// 步骤2:调用getConnectedDevices方法,查询用户是否有已连接的穿戴设备
deviceClient.getConnectedDevices().then(devices => {
  // 处理返回的设备列表
  deviceList = devices ;
  console.info(`Succeeded in getting deviceList, deviceList number is ${deviceList.length}`);
}).catch((error: BusinessError) => {
  // 处理调用失败时捕获到的异常
  console.error(`Failed to get deviceList. Code is ${error.code}, message is ${error.message}`);
})