The library requires that a mode be set before scanning. The mode determines where the device responses are uploaded. Options are:
SANDBOX
PRODUCTION
The mode determines where responses from the lock are uploaded. Setting an upload URL manually is no longer supported
NokeDeviceManagerDelegate
: func nokeDeviceDidShutdown(noke: NokeDevice, isLocked: Bool, didTimeout: Bool)
The Nokē Mobile Library provides an easy-to-use and stable way to communicate with Nokē Devices via Bluetooth. It must be used in conjunction with the Nokē Core API for full functionality such as unlocking locks and uploading activity.
This library is compatible with iOS devices that support Bluetooth Low Energy (BLE) and are running iOS 8 or higher
target 'YourAppTargetName' do
use_frameworks!
pod 'NokeMobileLibrary'
end
pod install
Open the newly created .xcworkspace
Import the library to any of your classes by using import NokeMobileLibrary
and begin working on your project
Once you've add the CocoaPod to your project, add the Mobile API Key by setting it in the AppDelegate NokeDeviceManager.shared().setAPIKey("YOUR_KEY_HERE")
NokeDeviceManager.swift
class handles scanning for Nokē Devices, connecting, sending commands, and receiving responses. All interaction is done through a singleton shared instance of the class://Setup device manager
NokeDeviceManager.shared().delegate = self
NokeDeviceManagerDelegate
protocol to receive callbacks from the NokeDeviceManager
:func nokeDeviceDidUpdateState(to state: NokeDeviceConnectionState, noke: NokeDevice) {
switch state {
case .nokeDeviceConnectionStateDiscovered:
//Nokē Discovered
break
case .nokeDeviceConnectionStateConnected:
//Nokē Connected
break
case .nokeDeviceConnectionStateSyncing:
//Nokē Syncing
break
case .nokeDeviceConnectionStateUnlocked:
//Nokē Unlocked
break
case .nokeDeviceConnectionStateDisconnected:
//Nokē Disconnected
break
default:
//Unrecognized State
break
}
}
func nokeErrorDidOccur(error: NokeDeviceManagerError, message: String, noke: NokeDevice?) {
}
NokeDeviceManager.shared().setLibraryMode(NokeLibraryMode.SANDBOX)
NokeDeviceManager
class only scans for devices that have been added to the device array.//Add locks to device manager
let noke = NokeDevice.init(name: "LOCK NAME", mac: "XX:XX:XX:XX:XX:XX")
NokeDeviceManager.shared().addNoke(noke!)
NokeDeviceManager
to discover all Noke devices, use the following method:NokeDeviceManager.shared().setAllowAllNokeDevices(true)
*You can check the lock state (noke.lockState
) of the Noke object returned in the nokeDeviceDidUpdateState
method to know if the lock is unlocked, locked, or unshackled. HD locks running version 3.13+ will update in realtime as they're discovered
Note: Make sure that the Bluetooth Manager is in the Powered On State before beginning scanning or you will encounter an error. The bluetoothManagerDidUpdateState
protocol method can be used to receive updates on the state
func bluetoothManagerDidUpdateState(state: NokeManagerBluetoothState) {
switch (state) {
case NokeManagerBluetoothState.poweredOn:
debugPrint("NOKE MANAGER ON")
NokeDeviceManager.shared().startScanForNokeDevices()
break
case NokeManagerBluetoothState.poweredOff:
debugPrint("NOKE MANAGER OFF")
break
default:
debugPrint("NOKE MANAGER UNSUPPORTED")
break
}
}
NokeDeviceManager
the Nokē Device updates state to Discovered
. The manager can then connect to the Nokē Device.case .nokeDeviceConnectionStateDiscovered:
debugPrint(String.init(format:"%@ discovered", noke.name))
NokeDeviceManager.shared().connectToNokeDevice(noke)
break
noke.mac
) - The bluetooth MAC Address of the Nokē devicenoke.session
) - A unique session string used to encrypt commands to the locknoke
object after a successful connectionpublic func requestUnlock(noke: NokeDevice, email: String){
let url = String.init(format:"%@%@", serverUrl, "unlock/")
var jsonBody = [String: Any](https://raw.github.com/noke-inc/noke-mobile-library-ios/master/)
jsonBody["session"] = noke.session
jsonBody["mac"] = noke.mac
jsonBody["email"] = email
if(JSONSerialization.isValidJSONObject(jsonBody)){
guard let jsonData = try? JSONSerialization.data(withJSONObject: jsonBody, options: JSONSerialization.WritingOptions.prettyPrinted) else{return}
self.doRequest(url: url, jsonData: jsonData)
}
}
NokeDevice
object:currentNoke.sendCommands(commandString)
The offline key and unlock command should be set on the Noke device object before being used:
noke.setOfflineValues(key:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", command: "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy")
They can also be set at the time of offline unlocking:
noke.offlineUnlock(key:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", command: "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy")
Once the values are set, the offline unlock function can be called at any time to unlock the Noke device:
noke.offlineUnlock()
Logs, errors, and callbacks will continue to function the same as when unlocking online.
The Nokē Mobile Library automatically uploads all responses from the Nokē device to the Nokē Core API for parsing. Responses that contain activity logs are stored in the database and can be accessed using endpoints from the API. Please see the Nokē Core API documentation for more details.
*The library requires you to set a mode before scanning. The mode determines where the device responses are uploaded (it should be either SANDBOX or PRODUCTION)'
NokeDeviceManager.shared().setLibraryMode(NokeLibraryMode.SANDBOX)
Nokē Mobile Library is available under the Apache 2.0 license. See the LICENSE file for more info.
Copyright © 2018 Nokē Inc. All rights reserved.
link |
Stars: 5 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics