Wired Mode

Last Updated on : 2023-04-17 09:04:57download

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 ThingSmartActivatorNotificationFindGatewayDevice;

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:(ThingSuccessString)success
                   failure:(ThingFailureError)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 {
        ThingSmartActivator *wiredActivator =  [[ThingSmartActivator alloc] init];
	[wiredActivator getTokenWithHomeId:homeId success:^(NSString *token) {
		NSLog(@"getToken success: %@", token);
		// TODO: startConfigWiFi
	} failure:^(NSError *error) {
		NSLog(@"getToken failure: %@", error.localizedDescription);
	}];
}

Swift:

func getToken() {
     let wiredActivator = ThingSmartActivator()
    wiredActivator.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 `ThingSmartActivator`.
	self.wiredActivator.delegate = self;

	 // Starts pairing.
	[self.wiredActivator startConfigWiFiWithToken:token timeout:100];
}

- (ThingSmartActivator *)wiredActivator {
    if (!_wiredActivator) {
        _wiredActivator = [[ThingSmartActivator alloc] init];
    }
    return _wiredActivator;
}

#pragma mark - ThingSmartActivatorDelegate
- (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(ThingSmartDeviceModel *)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 `ThingSmartActivator`.
    wiredActivator.delegate = self

        // Starts pairing.
    wiredActivator.startConfigWiFi(withToken: token, timeout: 100)
}

lazy var wiredActivator: ThingSmartActivator = {
    let activator = ThingSmartActivator()
    return activator
}()

#pragma mark - ThingSmartActivatorDelegate
func activator(_ activator: ThingSmartActivator!, didReceiveDevice deviceModel: ThingSmartDeviceModel!, 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 [ThingSmartActivator stopConfigWiFi] to stop pairing.

API description

- (void)stopConfigWiFi;

Example

ObjC:

- (void)stopConfigWifi {
	self.wiredActivator.delegate = nil;
	[self.wiredActivator stopConfigWiFi];
}

Swift:

func stopConfigWifi() {
    wiredActivator.delegate = nil
    wiredActivator.stopConfigWiFi()
}