摄像头二维码配网

更新时间:2025-12-16 06:21:21下载pdf

通过摄像头设备扫描 App 二维码来传递配网信息的方式来实现配网设备。

IPC 类设备扫码配网 API 使用示例

API 说明,请参考 设备配网

获取配网token

// 获取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()