Wired Mode

Last Updated on : 2023-05-22 06:38:26

A wired device connects to a router over an Ethernet cable. During the pairing process, users do not need to enter the name and password of the router.

Pairing process

The following figure shows the process to pair a Zigbee wired gateway:

Wired Mode

Discover a device

The SDK provides the capability to discover a wired device pending pairing. The device must be connected to the same network as the mobile phone to enable pairing. Then, register the notification of data of the wired device. When the SDK receives the data broadcasted by the wired device, it forwards the data as a notification to the app.

Forward the notification

// Receives the data broadcasted by the wired device and forwards the notification. `objec` is a dictionary type of data, @{@"productId":productId, @"gwId":gwId}
extern NSString *const TuyaSmartActivatorNotificationFindGatewayDevice;

Get a token

Before the wired 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

- (void)getTokenWithHomeId:(long long)homeId
                   success:(TYSuccessString)success
                   failure:(TYFailureError)failure;

Parameters

Parameter Description
homeId The ID of the home with which the device is bound.
success The success callback with a pairing token returned.
failure The failure callback with an error message returned.

Example

ObjC:

- (void)getToken {
	[[TuyaSmartActivator sharedInstance] getTokenWithHomeId:homeId success:^(NSString *token) {
		NSLog(@"getToken success: %@", token);
		// TODO: startConfigWiFi
	} failure:^(NSError *error) {
		NSLog(@"getToken failure: %@", error.localizedDescription);
	}];
}

Swift:

func getToken() {
    TuyaSmartActivator.sharedInstance()?.getTokenWithHomeId(homeId, success: { (token) in
        print("getToken success: \(token)")
        // TODO: startConfigWiFi
    }, failure: { (error) in
        if let e = error {
            print("getToken failure: \(e)")
        }
    })
}

Start pairing

API description

- (void)startConfigWiFiWithToken:(NSString *)token timeout:(NSTimeInterval)timeout

Parameters

Parameter Description
token The pairing token.
timeout The timeout value of a pairing task.

Example

ObjC:

- (void)startConfigWiFiToken:(NSString *)token {
	// Implements the delegate method of `TuyaSmartActivator`.
	[TuyaSmartActivator sharedInstance].delegate = self;

	 // Starts pairing.
	[[TuyaSmartActivator sharedInstance] startConfigWiFiWithToken:token timeout:100];
}

#pragma mark - TuyaSmartActivatorDelegate
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {

	if (!error && deviceModel) {
		// The device is paired.
    }

    if (error) {
        // Failed to pair the device.
    }

}

Swift:

func startConfigWifiToken(_ token: String) {
    // Implements the delegate method of `TuyaSmartActivator`.
    TuyaSmartActivator.sharedInstance()?.delegate = self

        // Starts pairing.
    TuyaSmartActivator.sharedInstance()?.startConfigWiFi(withToken: token, timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        // The device is paired.
    }

    if let e = error {
        // Failed to pair the device.
        print("\(e)")
    }
}

Stop pairing

After the pairing process is started, the app continuously broadcasts the pairing data until a device is paired or the process times out. To allow users to cancel or complete pairing during the process, you must call the API method [TuyaSmartActivator stopConfigWiFi] to stop pairing.

API description

- (void)stopConfigWiFi;

Example

ObjC:

- (void)stopConfigWifi {
	[TuyaSmartActivator sharedInstance].delegate = nil;
	[[TuyaSmartActivator sharedInstance] stopConfigWiFi];
}

Swift:

func stopConfigWifi() {
    TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopConfigWiFi()
}