Need Help with Unlocking Smart Lock Device via BLE in iOS App

App development related product technical discussions, including OEM App, App SDK, device control interface development, mini program development and other topics.


Post Reply
abhi165
Posts: 1

Hi everyone,

I have a smart lock device with the device ID d7446bvfiszjytea. I’m trying to add functionality in my iOS app to connect and unlock this device. Below is my current implementation:

Code: Select all

class ConnectionManager: NSObject, ThingSmartBLEManagerDelegate {
    var siteID = Int64()

func start() {
    if !ThingSmartUser.sharedInstance().isLogin {
        ThingSmartUser.sharedInstance().registerAnonymous(withCountryCode: "+91",
                                                          userName: "guest",
                                                          success: {
            self.loginAndFetchSiteDetails()
        })
    } else {
        loginAndFetchSiteDetails()
    }
}

func createSite(siteName: String, geoName: String, rooms: [String], latitude: Double, longitude: Double, completion: @escaping (Int64?) -> Void) {
    ThingSmartHomeManager().addHome(withName: siteName, geoName: geoName, rooms: rooms, latitude: latitude, longitude: longitude, success: { siteId in
        print("Site created successfully with ID: \(siteId)")
        completion(siteId as? Int64)
    }, failure: {
        print("Failed to create site: \($0)")
        completion(nil)
    })
}

func loginAndFetchSiteDetails() {
    let siteName = "MySite"
    let geoName = "MyGeo"
    let rooms = ["Living Room", "Bedroom"]
    let latitude = 37.7749
    let longitude = -122.4194

    createSite(siteName: siteName, geoName: geoName, rooms: rooms, latitude: latitude, longitude: longitude) { siteId in
        if let siteId = siteId {
            self.siteID = siteId
            ThingSmartBLEManager.sharedInstance().startListening(true)
        }
    }
}

func didDiscoveryDevice(withDeviceInfo deviceInfo: ThingBLEAdvModel) {
    ThingBLELockActivator.shared().activeBLELock(deviceInfo,
                                                 siteId: Int64(siteID),
                                                 success: {
        print("Device activated: \($0)")

        // Attempt to unlock the device
        let lockDevice = ThingLockDevice.shared()
        lockDevice.unLock(withSiteId: self.siteID, deviceId: "d7446bvfiszjytea", success: {
            print("Unlock successful")
        }, failure: {
            print("Unlock failed: \($0)")
        })

        // Alternative method
        let ble = ThingSmartBLELockDevice(deviceId: "d7446bvfiszjytea")
        ble?.autoConnect()
        print("BLE connected: \(ble?.isBLEConnected() ?? false)")

        ble?.addMember(withUserName: ThingSmartUser.sharedInstance().userName,
                       allowUnlock: true,
                       timeType: .permanent,
                       effectiveDate: .now,
                       invalidDate: Date() + 10000,
                       success: {
            print("Member added: \($0)")
            ble?.bleUnlock("guest-171839258741392446433", success: {
                print("BLE unlock successful")
            }, failure: {
                print("BLE unlock failed: \($0)")
            })
        }, failure: {
            print("Failed to add member: \($0)")
            ble?.bleUnlock("guest-171839258741392446433", success: {
                print("BLE unlock successful")
            }, failure: {
                print("BLE unlock failed: \($0)")
            })
        })
    }, failure: {
        print("Device activation failed: \($0)")
    })
}
}

I am able to successfully connect and activate the device, but I am having trouble with unlocking it. I’ve tried two methods to unlock the device, but both of them are failing.

Method 1:

Code: Select all

let lockDevice = ThingLockDevice.shared()
lockDevice.unLock(withSiteId: self.siteID, deviceId: "d7446bvfiszjytea", success: {
    print("Unlock successful")
}, failure: {
    print("Unlock failed: \($0)")
})

Method 2:

Code: Select all

let ble = ThingSmartBLELockDevice(deviceId: "d7446bvfiszjytea")
ble?.autoConnect()
ble?.addMember(withUserName: ThingSmartUser.sharedInstance().userName,
               allowUnlock: true,
               timeType: .permanent,
               effectiveDate: .now,
               invalidDate: Date() + 10000,
               success: {
    print("Member added: \($0)")
    ble?.bleUnlock("guest-171839258741392446433", success: {
        print("BLE unlock successful")
    }, failure: {
        print("BLE unlock failed: \($0)")
    })
}, failure: {
    print("Failed to add member: \($0)")
    ble?.bleUnlock("guest-171839258741392446433", success: {
        print("BLE unlock successful")
    }, failure: {
        print("BLE unlock failed: \($0)")
    })
})

Both methods return different errors:

Code: Select all

•	Method 1: API or API version error
•	Method 2: product dp define error, can't find ble_unlock_check、bluetooth_unlock code

Can anyone guide me on how to properly unlock the device? Any help would be greatly appreciated.

sega
Posts: 1

Re: Need Help with Unlocking Smart Lock Device via BLE in iOS App

Have you managed to make any progress on this? I have the same problem...

bristle8656
Posts: 2

Re: Need Help with Unlocking Smart Lock Device via BLE in iOS App

I have the same exact issue. Can’t seem to figure it out, the documentation seems pretty spotty. Have you gotten anywhere with this yet?

User avatar
xiakehc
Posts: 8

Re: Need Help with Unlocking Smart Lock Device via BLE in iOS App

The first step is to make sure you are using the Commercial Lock App SDK, not the Smart Lock SDK.

For the difference between the two, please refer to the Note in the following link:
https://developer.tuya.com/en/docs/app- ... t175nsdh6i

企业微信截图_186e1414-8811-40e5-9a74-e8803ba356b5.png
bristle8656
Posts: 2

Re: Need Help with Unlocking Smart Lock Device via BLE in iOS App

What is the smart lock sdk for then? I am trying to connect to a bluetooth cabinet lock. I am having trouble initializing it, as I cannot determine what the device ID is.

User avatar
xiakehc
Posts: 8

Re: Need Help with Unlocking Smart Lock Device via BLE in iOS App

1、Provide your deviceId. To obtain it, use the devId field of the ThingSmartDevice class. For reference, see:https://developer.tuya.com/en/docs/app- ... cgmmjr46cp.

2、Commercial Lock App SDK is mainly used with lock PCBAs and commercial finished locks. Requires enterprise whitelist authorization and SaaS support.

3、For common smart door lock device connection and control, use: Smart Life App SDK and Smart Lock SDK, refer to: https://developer.tuya.com/en/docs/app- ... vl28puletn

Post Reply