智能客服
你问我答,随时在线为你解决问题
该接口的调用前,需要在开发者联盟申请设备基础信息权限(具体请参考申请接入Wear Engine服务)。
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对象
- // this.getUIContext().getHostContext() 表示应用上下文Context对象
- let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext());
- // 创建一个设备列表用于存储返回的设备
- 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}`);
- });