穿戴设备信息查询
// 在使用Wear Engine服务前,请导入WearEngine与相关模块
import { wearEngine } from '@kit.WearEngine';
import { BusinessError } from '@kit.BasicServicesKit';
查询穿戴设备是否支持某种WearEngine能力集
该接口的调用需要在开发者联盟申请设备基础信息权限(请参考申请接入Wear Engine服务)。
通过Device对象中的方法isWearEngineCapabilitySupported查询穿戴设备是否支持某种WearEngine能力集。
-
应用调用wearEngine中的getDeviceClient方法,获取DeviceClient对象。
let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext()); -
调用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}`);}) -
从设备列表中选取需要操作的设备。
-
调用Device对象中的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能力集
该接口的调用需要在开发者联盟申请设备基础信息权限(请参考申请接入Wear Engine服务)。
通过Device对象中的方法isDeviceCapabilitySupported查询穿戴设备是否支持某种Device能力集。
-
应用调用wearEngine中的getDeviceClient方法,获取DeviceClient对象。
let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext()); -
调用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}`);}) -
从设备列表中选取需要操作的设备。
-
调用Device对象中的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
该接口的调用需要在开发者联盟申请设备标识符权限(受限开放)并获得用户授权(请参考申请接入Wear Engine服务)。
通过Device对象中的方法getSerialNumber查询穿戴设备的SN。
-
应用调用wearEngine中的getDeviceClient方法,获取DeviceClient对象。
let deviceClient: wearEngine.DeviceClient = wearEngine.getDeviceClient(this.getUIContext().getHostContext()); -
调用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}`);}) -
从设备列表中选取需要操作的设备。
-
调用Device对象中的方法getSerialNumber查询穿戴设备的SN。
if (deviceList.length > 0) {// 步骤3 从得到的设备列表中选取目标设备,并定义为device(假设数组中存在已连接设备且第一位即为目标设备)let targetDevice: wearEngine.Device = deviceList[0];// 步骤4 调用设备的方法查询SNtargetDevice.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}`);})}