tuya / tuya-home-ios-sdk-sample-swift

This sample demonstrates how to use Tuya Smart Home iOS SDK features in Swift.
https://developer.tuya.com/docs/app-development/ios-app-sdk/feature-overview?id=Ka5cgmlybhjk8
MIT License
31 stars 16 forks source link

Device Pairing (iOS) #22

Closed b0r1ngx closed 7 months ago

b0r1ngx commented 8 months ago

From previous issue, where i said, that we successfully init Tuya SDK (at clear project created via Xcode), then we go further to Device Pairing.

Now we stack on Device Pairing, we tried EZ and AP modes, in both situations, we get timeout from Tuya SDK, but lamp is changing its behaviour, so for example for .AP mode, behaviour and code looks like this: 1) When lamp starts blinking 2) iPhone sits on router wifi (SSID: flat_463) 3) We runs getToken(), providing previously created homeId (with addHome function) 4) token is successfully gathered, and when it is gathered, we connect to access-point that created by lamp, then we do next: 4.1) sit to router wifi -> get timeout 4.2) sit to lamp wifi -> get timeout 4.3) sit to lamp wifi, wait 50 secs, then switch to router wifi (for get internet connection) -> get timeout

We use this code (where i mark ✅ - this code is running):

func addHome() {
        let latitude: CLLocationDegrees = 0
        let longitude: CLLocationDegrees = 0
        let homeManager = ThingSmartHomeManager()

        homeManager.addHome(withName: "you_home_name",
                             geoName: "city_name",
                               rooms: ["room_name"],
                            latitude: latitude,
                           longitude: longitude,
                             success: { (homeId) in
            print("add home success, homeId: \(homeId)")
            self.homeId = homeId
        }) { (error) in
            if let error {
                print("add home failure: \(error)")
            }
        }
    }

func getToken(homeId: Int64) {
        let ssid = "flat_463" // provide router ssid
        let password = "love_infinity" // provide router password

        apActivator.getTokenWithHomeId(homeId, success: { token in
            print("getToken success: \(String(describing: token))") // ✅
            self.pairingToken = token
            if let token {
                self.startConfigWiFi(
                    withSsid: ssid,
                    password: password,
                    token: token
                )
            }
        }, failure: { error in
            print("getToken failure: \(String(describing: error?.localizedDescription))")
        })
    }

    func startConfigWiFi(withSsid ssid: String, password: String, token: String) {
        apActivator.delegate = self
        apActivator.startConfigWiFi(.AP, ssid: ssid, password: password, token: token, timeout: 100)
    }

    // also how it said in docs, we use ThingSmartActivatorDelegate, and override activator functions
extension SmartLightManager: ThingSmartActivatorDelegate {
    func activator(_ activator: ThingSmartActivator!, didReceiveDevice deviceModel: ThingSmartDeviceModel!, error: Error!) {
        print("\(TAG) activator()")
        if deviceModel != nil && error == nil {
            print("\(TAG) devModel.")
            initDevice(devId: deviceModel.devId)
            print("\(TAG) The device is paired.")
        }

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

    func activator(_ activator: ThingSmartActivator!, didPassWIFIToSecurityLevelDeviceWithUUID uuid: String!) {
        print("\(TAG) didPassWIFIToSecurityLevelDeviceWithUUID: \(String(describing: uuid))")
        self.apActivator.continueConfigSecurityLevelDevice()
    }
}

When we use Tuya SDK, and connect to wifi of lamp, lamp is stop blinking, when we not use Tuya SDK, lamp is blinking more than 3 minutes.

This behaviour, in my opinion, seems like something is happening (so some lamp is get some connection with Tuya SDK), but Tuya SDK just gives timeout always anyway :(

We tried it out by this docs: https://developer.tuya.com/en/docs/app-development/activator?id=Ka5cgmlzpfig4

taojingGino commented 8 months ago

@b0r1ngx Timeout means device get wifi ssid and password but can not connect the internet

1.Please check if your Wi Fi account and password are correct 2.Confirm that your Wi Fi is 2.4G 3.You can use the Tuya Smart app for network configuration to confirm that Wi Fi is unobstructed

If there are any issues, please let us know

taojingGino commented 7 months ago

I will close this issue. If there are any issues, you can continue to provide feedback