更新时间:2026-01-08 09:31:30下载pdf
通过摄像头设备扫描 App 二维码来传递配网信息的方式来实现配网设备。
具体的 API 说明,请参考 设备配网。
// 获取token
this.activatorToken = await TSmartActivatorRequester.getActivatorToken(this.homeId)
获取二维码字符串
this.qrCodeString = TSmartActivator.getQRCodeString(this.ssid, this.password, this.activatorToken)
二维码数据生成
let content: string = this.qrCodeString;
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 400,
width: 400,
margin: 1,
level: generateBarcode.ErrorCorrectionLevel.LEVEL_M,// 默认是 LEVEL_H,有的 IPC 设备识别不了,需要设置成 M
backgroundColor: 0xFFFFFF,
pixelMapColor: 0x000000
}
try {
// 码图生成接口,成功返回 PixelMap 格式图片
generateBarcode.createBarcode(content, options).then((pixelMap: image.PixelMap) => {
this.pixelMap = pixelMap;
}).catch((error: BusinessError) => {
hilog.error(0x0001, '[generateBarcode]',
`Failed to get PixelMap by promise with options. Code: ${error.code}, message: ${error.message}`);
})
} catch (error) {
hilog.error(0x0001, '[generateBarcode]',
`Failed to createBarcode by promise with options. Code: ${error.code}, message: ${error.message}`);
}
二维码展示
二维码用 Image 展示 PixelMap 格式的图片,不能用 QRCode。QRCode 生成的二维码有些 IPC 设备无法识别。
Row() {
Image(this.pixelMap)
.width(300)
.height(300)
.objectFit(ImageFit.Contain)
}
.margin({ top: 10 })
配网使用上面生成的二维码,确定设备处于配网状态,将二维码对准摄像头,设备捕捉到二维码信息后会发出提示音。此时,通过以下接口监听配网结果并开始配网。
activatorListener: ITSmartActivatorListener = {
onActiveSetpAndError: (step: TSmartActivatorStep, error?: Error, device?: TSmartDeviceModel) => {
// 实现相关逻辑
},
onActiveSuccess: (deviceModel: TSmartDeviceModel) => {
// 处理配网成功的逻辑,例如更新 UI,跳转到下一个页面
},
};
const activatorBuilder = TSmartActivator.buildQRCodeActivatorBuilder(
this.homeId,
this.ssid,
this.password,
120 * 1000,
this.activatorListener,
this.activatorToken,
)
this.activator = TSmartActivator.createActivator(activatorBuilder);
this.activator.startActive()
this.activator.stopActive()
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈