Wired Device Pairing

Last Updated on : 2024-06-26 09:49:02download

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:

AppSDKGatewayCloudReset ZigbeegatewayRequest a tokenRequest a tokenReturn a tokenReturn a tokenThe app connects tothe same routerhotspot as thegatewaySend an activation commandSend an activation commandThe device receivesactivation dataActivate in the cloudActivated successfullyActivated successfullyActivated successfullyAppSDKGatewayCloud(Zigbee) Gateway Pairing

Discover 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 notifications for discovering wired devices. When the SDK receives the packet advertised by the wired device, it forwards the device information through notifications.

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 to be bound.
success The success callback. A pairing token is returned.
failure The failure callback. An error message is returned.

Sample code

Objective-C:

- (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 period.

Sample code

Objective-C:

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

Sample code

Objective-C:

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

Swift:

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