Scan QR Code on Device

Last Updated on : 2026-02-02 09:19:42download

This pairing mode only applies to devices that are supplied with a QR code and connected to the internet.

API example for pairing with device QR code

For more information, see Device Pairing.

Get device UUID

Scan the device’s QR code to obtain its UUID.

Scan the QR code

build() {
    Column() {
      Row() {
        Button("Tap to scan")
          .backgroundColor('#0D9FFB')
          .fontSize(20)
          .fontColor('#FFFFFF')
          .fontWeight(FontWeight.Normal)
          .align(Alignment.Center)
          .type(ButtonType.Capsule)
          .width('90%')
          .height(40)
          .margin({ top: 50, bottom: 5 })
          .enabled(this.scanBtnEnable)
          .onClick(() => {
            this.scanBtnEnable = false;
            // Define the scan parameters (options)
            let options: scanBarcode.ScanOptions = {
              scanTypes: [scanCore.ScanType.ALL],
              enableMultiMode: true,
              enableAlbum: true
            };
            try {
              scanBarcode.startScanForResult(getContext(this), options).then((result: scanBarcode.ScanResult) => {
                // Parse code value results
                hilog.info(0x0001, '[Scan CPSample]', `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);
                this.parseQRCode(result['originalValue'] as string);

              }).catch((error: BusinessError) => {
                this.scanBtnEnable = true;
                hilog.error(0x0001, '[Scan CPSample]',
                  `Failed to get ScanResult by promise with options. Code:${error.code}, message: ${error.message}`);
              });
            } catch (error) {
              this.scanBtnEnable = true;
              hilog.error(0x0001, '[Scan CPSample]',
                `Failed to start the scanning service. Code:${error.code}, message: ${error.message}`);
            }
          })
      }

    }
    .width('100%')
  }

Parse scan results

  parseQRCode(code: string) {
    TSmartActivatorRequester.parseQRCode(code).then(res => {
      console.log('parseQRCode ', JSON.stringify(code));
      // {"actionData":{"uuid":"dc18219067cf0516"},"actionName":"device_net_conn_multi_ver"}
      if (res['actionName'] === 'device_net_conn_multi_ver') {
        this.uuid = (res['actionData'] as Record<string, string>)['uuid'];
      }
    }).catch((error: Error) => {
      console.log('parseQRCode error');
    })
  }

Pairing

Implement methods in the pairing listener

activatorListener: ITSmartActivatorListener = {
    onActiveSetpAndError: (step: TSmartActivatorStep, error?: Error, device?: TSmartDeviceModel) => {
      // Implement the relevant logic
    },

    onActiveSuccess: (deviceModel: TSmartDeviceModel) => {
      // Handle the logic for successful pairing, such as updating the UI and navigating to the next page
    },
};

Create a pairing instance

    const builder = TSmartActivator.buildMQTTDirectlyActivatorBuilder(homeId, this.uuid!, 120000,this.activatorListener);
    this.activator = TSmartActivator.createActivator(builder);

Start pairing

this.activator.startActive()

Stop pairing

this.activator.stopActive()