Skip to content

iOS Version SesameSDK ​

Sesame device

Download App ​

Installation Process ​

1. Install Dependencies ​

    1. Change Scheme to SesameSDK, choose Any iOS Device
    1. command + shift + k to clear the product folder
    1. command + b to package SDK
    1. After completion, go to Xcode -> SDK project -> Products folder, find SesameSDK.framework, right-click, choose Open in Finder to get SesameSDK.framework.

SesameWatchKitSDK ​

    1. Change Scheme to SesameWatchKitSDK, choose Any watch OS Device
    1. command + shift + k to clear the product folder
    1. command + b to package SDK
    1. After completion, go to Xcode -> SDK project -> Products folder, find SesameWatchKitSDK.framework, right-click, choose Open in Finder to get SesameWatchKitSDK.framework.

Swift Package ​

Swift Package Manager is a tool for managing the distribution of Swift code. It integrates with the Swift build system and automatically performs the processes of downloading, compiling, and linking dependencies.

  • Use Swift Package Manager to integrate SesameSDK into Xcode project:
c

dependencies: [
    .package(url: "https://github.com/CANDY-HOUSE/SesameSDK_iOS_with_DemoApp.git", .branch("master"))
]

img

Manual Integration ​

  • If you don't want to use any dependency managers, you can manually integrate SesameSDK into your project.

img

2. Add Authorization ​

jsx

<key> NSBluetoothAlwaysUsageDescription </key>
<string> To connect Sesame Smart Lock and lock/unlock the door. </string>
<key> NSBluetoothPeripheralUsageDescription </key>
<string>This app would like to make data available to nearby Bluetooth devices even when you're not using the app.
</string>

3. Initialization ​

Please start the Bluetooth scan at an appropriate time

jsx
CHBluetoothCenter.shared.enableScan { res in }

Callback when Bluetooth status changes

jsx
public protocol CHBleStatusDelegate: AnyObject {
    func didScanChange(status: CHScanStatus)
}

The list of scanned Sesame devices will be returned to the caller at a frequency of once per second.

jsx
public protocol CHBleManagerDelegate: AnyObject {
    func didDiscoverUnRegisteredCHDevices(_ devices: [CHDevice])
}

4. Connect to Devices ​

Before establishing a connection, you should first confirm that the device status is connectable

jsx
if sesame5.deviceStatus == .receivedBle() {
    sesame5.connect() { _ in }
}

At this point, you will receive the connection status callback of the Sesame device

jsx
public protocol CHDeviceStatusDelegate: AnyObject {
    func onBleDeviceStatusChanged(device: CHDevice, status: CHDeviceStatus, shadowStatus: CHDeviceStatus?)
    func onMechStatus(device: CHDevice)
}

5. Device Registration ​

When the connection status changes to "Ready to Register", you can register the device to complete the pairing. Registration is a necessary step to bind the device

jsx
if device.deviceStatus == .readyToRegister() {
    device.register( _ in )
}

After the registration is completed, you can get the paired devices through CHDeviceManager

jsx
var chDevices = [CHDevice]()
CHDeviceManager.shared.getCHDevices { result in
    if case let .success(devices) = result {
        chDevices = devices.data
    }
}

6. Lock and Unlock ​

After getting the paired devices, you can lock and unlock through the APP

Lock ​

jsx

    public func lock(historytag: Data?, result: @escaping (CHResult<CHEmpty>))  {
      if deviceShadowStatus != nil,
         deviceStatus.loginStatus == .unlogined {
          CHIoTManager.shared.sendCommandToWM2(.lock, self) { _ in
              result(.success(CHResultStateNetworks(input: CHEmpty())))
          }
          return
      }
      if (self.checkBle(result)) { return }
      let hisTag = Data.createOS2Histag(historytag ?? self.sesame2KeyData?.historyTag)

      sendCommand(.init(.lock,hisTag)) { responsePayload in
          if responsePayload.cmdResultCode == .success {
              result(.success(CHResultStateBLE(input: CHEmpty())))
          } else {
              result(.failure(self.errorFromResultCode(responsePayload.cmdResultCode)))
          }
      }
  }

Unlock ​

jsx

    public func unlock(historytag: Data?, result: @escaping (CHResult<CHEmpty>))  {
      if deviceShadowStatus != nil,
         deviceStatus.loginStatus == .unlogined {
          CHIoTManager.shared.sendCommandToWM2(.unlock, self) { _ in
              result(.success(CHResultStateNetworks(input: CHEmpty())))
          }
          return
      }
      if (self.checkBle(result)) { return }
      let hisTag = Data.createOS2Histag(historytag ?? self.sesame2KeyData?.historyTag)

      sendCommand(.init(.unlock,hisTag)) { responsePayload in
          if responsePayload.cmdResultCode == .success {
              result(.success(CHResultStateBLE(input: CHEmpty())))
          } else {
              result(.failure(self.errorFromResultCode(responsePayload.cmdResultCode)))
          }
      }
  }

7. Communication Protocol ​

For details of interaction between each instruction, please refer to the description of Bluetooth Protocol.

8. See More Details ​

For the design details of SesameSDK, please refer to the following design diagrams and flowcharts.

Framework/Module Selection ​

BLE Product Code Example Explanation ​

For details of each Class, please refer to the description of Sesame SDK Class.

Bluetooth State Machine ​

State Machine

iOS Class Diagram ​

Class_diagram