Wired Mode

Last Updated on : 2024-06-28 08:10:32download

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

  1. Before pairing
    Guide the user to reset the device to activate pairing mode.

  2. Get pairing token
    The app gets the pairing token by calling the method in the SDK.

  3. Get device information
    The app gets the device information by calling the method in the SDK.

  4. Start pairing
    The app calls the pairing method in the SDK to set the required parameters.

  5. Finish pairing
    The app receives a callback from the SDK and finishes the pairing process.

Discover device

The SDK provides the capability to discover a wired device ready for pairing. The mobile phone and device must be on the same network. Implement the delegate for ThingSmartActivatorDelegate to get the device information. Alternatively, register the notification for discovering wired devices. When the SDK receives the packet advertised by a wired device, it forwards the device information through notifications.

Discover devices through ThingSmartActivatorDelegate

/// Callback for the wired gateway discovery, Wi-Fi gateway, Zigbee gateway.
/// @param activator instance
/// @param deviceId the device id
/// @param productId the product id
- (void)activator:(ThingSmartActivator *)activator didFindGatewayWithDeviceId:(nullable NSString *)deviceId productId:(nullable NSString *)productId;

Example

Objective-C:

@interface YourClass : NSObject <ThingSmartActivatorDelegate>
@end

@implementation YourClass

- (void)activator:(ThingSmartActivator *)activator didFindGatewayWithDeviceId:(nullable NSString *)deviceId productId:(nullable NSString *)productId {
    if (deviceId && productId) {
        NSLog(@"Found gateway with device ID: %@ and product ID: %@", deviceId, productId);
        // TODO: Handle the discovered gateway, e.g., store device info or update UI
    }
}

Swift:

class YourClass: NSObject, ThingSmartActivatorDelegate {

    func activator(_ activator: ThingSmartActivator, didFindGatewayWithDeviceId deviceId: String?, productId: String?) {
        if let deviceId = deviceId, let productId = productId {
            print("Found gateway with device ID: \(deviceId) and product ID: \(productId)")
            // TODO: Handle the discovered gateway, e.g., store device info or update UI
        }
    }
}

Notify device discovery by forwarding

// 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 device information

- (void)getHomekitDeviceInfWithProductId: (NSString *)productId success: (ThingSuccessDict) success
failure: (ThingFailureError) failure;

Example

Objective-C:

- (void)getHomekitDeviceInfo {
    ThingSmartHomeKitActivator *homeKitActivator = [[ThingSmartHomeKitActivator alloc] init];
    NSString *productId = @"yourProductId";

    [homeKitActivator getHomekitDeviceInfWithProductId:productId success:^(NSDictionary *deviceInfo) {
        NSLog(@"getHomekitDeviceInfo success: %@", deviceInfo);
        // TODO: handle successful retrieval
    } failure:^(NSError *error) {
        NSLog(@"getHomekitDeviceInfo failure: %@", error.localizedDescription);
        // TODO: handle failure
    }];
}

Swift:

func getHomekitDeviceInfo() {
    let homeKitActivator = ThingSmartHomeKitActivator()
    let productId = "yourProductId"

    homeKitActivator.getHomekitDeviceInfWithProductId(productId, success: { (deviceInfo) in
        print("getHomekitDeviceInfo success: \(deviceInfo)")
        // TODO: handle successful retrieval
    }, failure: { (error) in
        if let e = error {
            print("getHomekitDeviceInfo failure: \(e.localizedDescription)")
            // TODO: handle failure
        }
    })
}

Get a token

Before the pairing process, the app 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. A pairing token is returned.
failure The failure callback, returning an error message.

Example

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.

Example

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 stop or finish pairing, call [ThingSmartActivator stopConfigWiFi].

API description

- (void)stopConfigWiFi;

Example

Objective-C:

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

Swift:

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