设备管理
更新时间:2025-12-31 14:54:05
包括设备绑定,设备解绑,设备列表
// 导入设备绑定头文件
import { RTCXAddDeviceService, RTCXAddDeviceType, RTCXDeviceMgr, RTCXDeviceMgrTypes, RTCXThingModel, RTCXThingModelType, RTCXLogger } from 'rtcxsdk'
设备绑定
1. 无线绑定(如: 二维码、蓝牙等方式)
总体介绍参考设备绑定开发指南。
- 设备绑定流程
- App调用prepareAddDevice()方法后,当监听到EAddDeviceStatus.Prepared状态时可获取bindToken (有效时长30分钟)。
- 将bindToken传递给设备(二维码、蓝牙等),设备保证网络连通情况下,通过DeviceSDK进行设备绑定
- 海外设备绑定时,同时还需要将regionId传递给设备
- 在EAddDeviceStatus.Prepared状态下,App只需要调用startAddDevice()方法实时监听绑定状态回调即可(超时自行控制,最大超时依赖bindToken的有效时长)。
注意
- startAddDevice()方法需要等添加状态为EAddDeviceStatus.Prepared时才可以调用,所以可以在 onAddDeviceStatus: ()=>void 回调状态为EAddDeviceStatus.Prepared调用开始添加方法
- 已配网设备(如4G、有线设备)无需处理步骤2,即不需要处理bindToken
- 设备绑定详细流程说明请点击
相关API如下:
export interface IAddStatusListener {
// 设备添加状态回调,回调过程中状态、数据、错误信息
onAddDeviceStatus: (status: EAddDeviceStatus, data: Object|undefined, error: BusinessError<void>|undefined)=>void
}
export interface IAddDeviceService {
/**
* 准备添加设备,成功才可以进行下一步
*
* @param addType 添加类型
*/
prepareAddDevice(addType: EAddDeviceType): void
/**
* 开始添加设备,成功才可以进行下一步
*
* @param addReq 配网模式对应入参
*/
startAddDevice(addReq: IAddDeviceReq): void
/**
* 停止添加设备
*
*/
stopAddDevice(): void
/**
* 注册设备添加状态回调监听
*
* @param listener 回调监听者
*/
registerStatusListener(listener: IAddStatusListener): void
/**
* 获取配网所需随机数
*/
getBindToken(): string
/**
获取当前regionId(海外设备配网需要)
*/
getRegionId(): number
}
调用示例如下:
- 注册监听,并准备绑定
statusListerner: RTCXAddDeviceType.IAddStatusListener = {
onAddDeviceStatus: (status: RTCXAddDeviceType.EAddDeviceStatus, data: Object|undefined, error: BusinessError<void>|undefined)=> {
RTCXLogger.i(this.TAG, `device add status: ${status}, ${data}, ${error}`)
if (status === RTCXAddDeviceType.EAddDeviceStatus.Prepared) {
const bindToken = RTCXAddDeviceService.getBindToken()
RTCXLogger.i(this.TAG, `get bind token: ${bindToken}`)
} else if (status === RTCXAddDeviceType.EAddDeviceStatus.Success) {
showToast(this,'绑定成功')
this.pathStack.pop(true)
} else if (status === RTCXAddDeviceType.EAddDeviceStatus.Failed) {
showToast(this,'绑定失败')
}
}
}
RTCXAddDeviceService.registerStatusListener(this.statusListerner)
RTCXAddDeviceService.prepareAddDevice(RTCXAddDeviceType.EAddDeviceType.QR)
- 开始绑定
const req: RTCXAddDeviceType.QRAddDeviceReq = {addDeviceType: RTCXAddDeviceType.EAddDeviceType.QR}
RTCXAddDeviceService.startAddDevice(req)
- 结束绑定(该方法可以不调用,内部会自动处理)
RTCXAddDeviceService.stopAddDevice()
2. 有线绑定(如: 4G、有线设备)
- 与上面正常设备绑定流程类似,特殊之处是不需要向设备传递bindToken,而是直接调用WRAddDeviceReq有线添加的方式即可完成绑定(如:App可以扫描设备上的二维码获取productKey和deviceName,然后调用startAddDevice()方法绑定,同时监听绑定状态回调)
// App扫描设备二维码绑定
const req: RTCXAddDeviceType.WRAddDeviceReq = {productKey: 'xxx', deviceName: 'xxx', addDeviceType: RTCXAddDeviceType.EAddDeviceType.WR}
RTCXAddDeviceService.startAddDevice(req)
设备解绑
/**
* 解绑用户的设备
* @param iotId - 设备ID
*/
export async function deviceUnbind(iotId: string): Promise<void>
调用示例如下:
RTCXDeviceMgr.deviceUnbind(deviceInfo.iotId)
设备列表
/**
* 设备列表
* @param deviceListReq - 设备列表请求参数
* @returns
* @public
*/
export async function getDeviceList(deviceListReq: IDeviceListReq): Promise<IDeviceListResp>
/**
* 获取网关子设备
* @param subListReq - 获取网关子设备列表请求参数
* @returns
* @public
*/
export async function getGatewaySubList(subListReq: GatewaySubListReq): Promise<IDeviceListResp>
调用示例如下:
const reqParams: RTCXDeviceMgrTypes.IDeviceListReq = {
pageNo: 1,
pageSize: 100
}
try {
const deviceListRsp = await RTCXDeviceMgr.getDeviceList(reqParams)
this.deviceList = deviceListRsp.data
} catch (e) {
showToast(this, `获取设备列表失败${e}`)
}
获取设备状态
/**
* 获取设备状态
* @param iotId - 设备ID
* @returns 设备状态 DeviceStatusEnum
* @public
*/
export async function getDeviceStatus(iotId: string): Promise<DeviceStatusEnum>
调用示例如下:
try {
const status = await RTCXDeviceMgr.getDeviceStatus('iotId')
} catch (e) {
showToast(this, `获取设备状态失败${e}`)
}
设备缩略图
通过物模型服务获取设备缩略图
this.thingModel = new RTCXThingModel(PlayDeviceInfo)
try {
const result = await this.thingModel.invokeService<IDevPicture>('GetDevPicture',{'definition': 1, "picType": "jpg"})
if (result) {
const rsp = result as RTCXThingModelType.InvokeServiceRsp<IDevPicture>
if (rsp.code == 200) {
const devPicture = rsp.data as IDevPicture
RTCXLogger.i(TAG,`设备缩略图:${thingM.deviceInfo.iotId}, ${devPicture.downloadUrl}`)
this.deviceThumnails[thingM.deviceInfo.iotId] = devPicture
} else {
showToast(this, `获取缩略图失败`)
}
}
} catch (e) {
showToast(this, `获取缩略图失败${e}`)
}