鸿蒙开发(NEXT/API 12)【已连接穿戴设备查询】手机侧应用开发
Wear Engine提供查询用户已连接的穿戴设备列表(即支持Wear Engine能力且与手机侧运动健康App处于连接状态的穿戴设备)的接口。
建议开发者在使用Wear Engine其他API接口前先实现该接口功能。
-
应用调用[wearEngine] 中的[getDeviceClient] 方法,获取[DeviceClient] 对象。
-
调用[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}`);
})