Smart Camera Pairing in QR Code Mode

Last Updated on : 2024-11-18 06:40:50download

A smart camera can scan a QR code on the app to get the pairing data. Users can pair the smart camera in this way.

Pairing process

Smart Camera Pairing in QR Code Mode

Initialize pairing parameters

API description

TuyaCameraActivatorBuilder builder = new TuyaCameraActivatorBuilder()
		 .setContext(context)
		 .setSsid(ssid)
		 .setPassword(password)
		 .setToken(token)
		 .setTimeOut(timeout)
		 .setListener(new ITuyaSmartCameraActivatorListener() {
			 @Override
			 public void onQRCodeSuccess(String qrcodeUrl) {
				 // The URL used to generate a QR code.
			 }

			 @Override
			 public void onError(String errorCode, String errorMsg) {
				 // Failed to pair the device.
			 }

			 @Override
			 public void onActiveSuccess(DeviceBean devResp) {
				// The device is paired.
			 }
		 }));

Parameters

Parameter Description
token The pairing token.
context The context to be set in activity.
ssid The name of the Wi-Fi network to which a paired device is connected.
password The password of the Wi-Fi network to which a paired device is connected.
timeout The timeout value of a pairing task. Default value: 100. Unit: seconds.

Get a token

Before the smart camera pairing process, the SDK must get a pairing token from the cloud in the networked state. The token is valid for 10 minutes and expires immediately after the device is paired. A new token must be generated if the device needs to be paired again.

API description

TuyaHomeSdk.getActivatorInstance().getActivatorToken(homeId,
		new ITuyaActivatorGetToken() {

			@Override
			public void onSuccess(String token) {

			}

			@Override
			public void onFailure(String s, String s1) {

			}
		});

Parameters

Parameter Description
homeId The home ID. For more information, see Home Management.

Call the pairing method

  • Pairing implementation class.

    ITuyaCameraDevActivator mTuyaActivator = TuyaHomeSdk.getActivatorInstance().newCameraDevActivator(builder);
    
  • Returns the URL of the QR code.

    mTuyaActivator.createQRCode(); // The result is returned by the callback of `onQRCodeSuccess`.
    
  • Generates a QR code by using the URL.

    In the following example, this dependency is required: zxing (implementation 'com.google.zxing:core:3.2.1').

    public static Bitmap createQRCode(String url, int widthAndHeight)
    			throws WriterException {
    		Hashtable hints = new Hashtable();
    		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    		hints.put(EncodeHintType.MARGIN,0);
    		BitMatrix matrix = new MultiFormatWriter().encode(url,
    				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
    
    		int width = matrix.getWidth();
    		int height = matrix.getHeight();
    		int[] pixels = new int[width * height];
    
    		for (int y = 0; y < height; y++) {
    			for (int x = 0; x < width; x++) {
    				if (matrix.get(x, y)) {
    					pixels[y * width + x] = BLACK;
    				}
    			}
    		}
    		Bitmap bitmap = Bitmap.createBitmap(width, height,
    				Bitmap.Config.ARGB_8888);
    		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    		return bitmap;
    	}
    
  • Starts pairing.

    mTuyaActivator.start();
    
  • Stops pairing.

    mTuyaActivator.stop();
    
  • Destroys data.

    mTuyaActivator.onDestory();