摄像头二维码配网

更新时间:2023-06-05 02:50:00下载pdf

智能摄像机特有的二维码配网模式可以参考文档二维码配网.

有线设备配网

有线设备已通过网线连接着网络,设备激活过程无需输入路由器的名称和密码。下图以 Zigbee 有线网关为例,描述有线网关配网流程

摄像头二维码配网

发现设备

SDK 提供发现待配网有线设备的功能,获取设备前手机需与设备接入同一网络,然后注册获取有线设备的通知,待 SDK 收到有线设备的广播即会通过通知转发设备信息。

通知

// 收到有线配网设备的广播后,会发送此通知。objec为dictionary,@{@"productId":productId, @"gwId":gwId}
extern NSString *const TuyaSmartActivatorNotificationFindGatewayDevice;

获取 Token

开始配网之前,SDK 需要在联网状态下从涂鸦云获取配网 Token,然后才可以开始有线设备激活配网。Token 的有效期为 10 分钟,且配置成功后就会失效(再次配网需要重新获取)

接口说明

配网 Token 获取接口

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

参数说明

参数 说明
homeId 设备将要绑定到的家庭的 Id
success 成功回调,返回配网 Token
failure 失败回调,返回失败原因

示例代码

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)")
        }
    })
}

开始配网

接口说明

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

参数说明

参数 说明
token 配网 Token
timeout 超时时间

示例代码

Objc:

- (void)startConfigWiFiToken:(NSString *)token {
	// 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
	[TuyaSmartActivator sharedInstance].delegate = self;

	// 开始配网
	[[TuyaSmartActivator sharedInstance] startConfigWiFiWithToken:token timeout:100];
}

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

	if (!error && deviceModel) {
		//配网成功
    }

    if (error) {
        //配网失败
    }

}

Swift:

func startConfigWifiToken(_ token: String) {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
    TuyaSmartActivator.sharedInstance()?.delegate = self

    // 开始配网
    TuyaSmartActivator.sharedInstance()?.startConfigWiFi(withToken: token, timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //配网成功
    }

    if let e = error {
        //配网失败
        print("\(e)")
    }
}

停止配网

开始配网操作后,App 会持续广播配网信息(直到配网成功,或是超时)。如果需要中途取消操作或配网完成,需要调用 [TuyaSmartActivator stopConfigWiFi] 方法

接口说明

- (void)stopConfigWiFi;

示例代码

Objc:

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

Swift:

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

子设备配网

摄像头二维码配网

开始子设备配网

接口说明

- (void)activeSubDeviceWithGwId:(NSString *)gwId timeout:(NSTimeInterval)timeout

参数说明

参数 说明
gwId 网关 Id
timeout 超时时间

示例代码

Objc:

- (void)activeSubDevice {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
	[TuyaSmartActivator sharedInstance].delegate = self;

	[[TuyaSmartActivator sharedInstance] activeSubDeviceWithGwId:@"your_device_id" timeout:100];
}

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

    if (!error && deviceModel) {
		//配网成功
    }

    if (error) {
        //配网失败
    }
}

Swift:

func activeSubDevice() {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
    TuyaSmartActivator.sharedInstance()?.delegate = self
    TuyaSmartActivator.sharedInstance()?.activeSubDevice(withGwId: "your_device_id", timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //配网成功
    }

    if let e = error {
        //配网失败
        print("\(e)")
    }
}

停止激活子设备

接口说明

- (void)stopActiveSubDeviceWithGwId:(NSString *)gwId

参数说明

参数 说明
gwId 网关 Id

示例代码

Objc:

- (void)stopActiveSubDevice {
  [TuyaSmartActivator sharedInstance].delegate = nil;
	[[TuyaSmartActivator sharedInstance] stopActiveSubDeviceWithGwId:@"your_device_id"];
}

Swift:

func stopActiveSubDevice() {
    TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopActiveSubDevice(withGwId: "your_device_id")
}

扫设备二维码配网

该功能仅适用于设备有二维码且已连接互联网的设备
摄像头二维码配网

  1. 利用通用接口获取 uuid

扫描设备二维码获取 url, 然后通过通用接口获取 uuid

参数说明

参数 说明
apiName tuya.m.qrcode.parse
Version 4.0
postData @{@“code”:url}

示例代码

Objc:

    [self.apiRequest requestWithApiName:@"tuya.m.qrcode.parse" postData:@{@"code":url} version:@"4.0" success:^(id result) {

    } failure:^(NSError *error) {

    }];

Swift:

apiRequest .request(withApiName: "tuya.m.qrcode.parse", postData: ["code":url], version: "4.0", success: {_ in

        }, failure: { (Error) in

        })
  1. 二维码配网

获取配网 Token 接口


- (void)getTokenWithUUID:(NSString *)uuid
                  homeId:(long long)homeId
                 success:(TYSuccessString)success
                 failure:(TYFailureError)failure;

示例代码
Objc:

 [[TuyaSmartActivator sharedInstance] getTokenWithUUID:uuid homeId:homeId success:^(NSString *result) {
       // 获取token
    } failure:^(NSError *error) {

    }];

Swift:

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

开始配网接口

- (void)startConfigWiFi:(TYActivatorMode)mode
                   ssid:(NSString *)ssid
               password:(NSString *)password
                  token:(NSString *)token
                timeout:(NSTimeInterval)timeout;

示例代码
Objc:

[[TuyaSmartActivator sharedInstance] startConfigWiFi:TYActivatorModeEZ ssid:@"" password:@"" token:self.token timeout:Timeout];

Swift:

 TuyaSmartActivator.sharedInstance()?.startConfigWiFi(TYActivatorModeEZ, ssid:nil, password: nil, token: token, timeout: Timeout)

获取设备回调代理

- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;

示例代码
Objc:

[TuyaSmartActivator sharedInstance].delegate = self;

- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {
      // get deviceModel
}

Swift:

func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //配网成功
    }

    if let e = error {
        //配网失败
        print("\(e)")
    }
}