BKClient
open class BKClient : NSObject, BKDeviceDiscoveryServiceDelegate, BKDiscoveryBackendResolverDelegate
Main Controller class for discovering BLE modules
BlukiiClient is a single point of contact for all data retieving actions as follows:
- Discovering BLE modules, extracting BLE advertised data
- Decryption of blukii SecureBeacon advertising
A. Discover BLE modules and extract their BLE data
To do this you have to do the following steps:
1. Initialize and start discovery
Call initDiscovery()
and implement the BKClientDiscoveryDelegate
delegates. After initialization is ready (BKClientDiscoveryDelegate.onDiscoveryInitialized()
) you can start the discovery.
var mBlukiiClient: BKClient!
override func viewDidLoad() {
...
mBlukiiClient = BKController.instance.getDiscoveryClient()
mBlukiiClient.discoveryDelegate = self
mBlukiiClient.initDiscovery()
...
}
func stopDiscovery() {
mBlukiiClient.stopDiscovery()
}
// Discovery Delegate
func onDiscoveryInitialized() {
if mBlukiiClient.startDiscovery() {
// Discovery successfully started
}
}
func onDiscoveryResult(discoveryResultList: [BKDiscoveryData]) {
// retrieve BLE results
}
func onDiscoveryError(errorCode: BKDiscoveryError) {
// retrieve errors
}
func onDiscoveryInfo(infoCode: BKDiscoveryInfo) {
// retrieve infos and changed states
}
2. Configure discovery settings
Use the BKDiscoverySettings
object for configuring discovery. Get BKClient.discoverySettings
to retrieve the settings property set.
func onInitialized() {
// Discovery successfully initialized
let discoverySettings: BKDiscoverySettings = mBlukiiClient.discoverySettings
discoverySettings.setRssiThreshold(-100)
discoverySettings.scanDuration = 10000
...
if mBlukiiClient.startDiscovery() {
// Discovery successfully started
}
}
When BLE scanner is started the BLE discovery is acting according to the BKDiscoverySettings
:
- The scanner is searching for BLE modules inside the defined rssi range (
BKDiscoverySettings.getRssiThreshold()
). - After one scan duration (
BKDiscoverySettings.scanDuration
) the results are called back to (BKClientDiscoveryDelegate.onDiscoveryResult(_:)
) - This is repeated until BLE Scanning is stopped by
BKClient.stopDiscovery()
or the app did enter the background. - if the scanner is paused according to the
BKDiscoverySettings
the infoBKDiscoveryInfo.bleScanPaused
is called. - For background scanning it is necessary to be in the Configured iBeacon Region. Background scanning will only Scan, if the device is in the configured iBeacon Region and the display will be switched on. In background it is possible to scan for iBeacon, EddystoneBeacon and InfoBeacon.
3. Retrieve results
The callback BKClientDiscoveryDelegate.onDiscoveryResult(_:)
will be called after finishing every discovery phase. It retrieves the discovered list of BKOutputElements.
Note: If no BLE modules have been found discoveryResultList is empty (not nil).
func onDiscoveryResult(discoveryResultList: [BKDiscoveryData]) {
// Discovery phase has finished: list of OutputElements are retrieved
for discoveryData in discoveryResultList {
let id = discoveryData.blukiiId
let rssi = discoveryData.getRssi()
...
}
}
4. Retrieve errors and infos
The callbacks BKClientDiscoveryDelegate.onDiscoveryError(_:)
and BKClientDiscoveryDelegate.onDiscoveryInfo(_:)
will be called if there is important information about the discover process.
func onDiscoveryError(errorCode: BKDiscoveryError) {
// An error has been sent from discover
}
func onDiscoveryInfo(infoCode: BKDiscoveryInfo) {
// An info has been sent from discovery
}
5. Permission
In Info.plist set the following keys for Beacon Detection:
- NSLocationAlwaysUsageDescription
- NSBluetoothAlwaysUsageDescription
Add the Follow Capapilities to your Project, for scanning Beacons in Background:
- Background Mode: Location Updates
- Background Mode: Uses Bluetooth LE accessoires
B. Decryption of SecureBeacon advertising
blukii SecureBeacons are encrypted to prevent third parties from misusing the Beacon identification.
The encryption is affecting the mac address and the iBeacon Major and Minor value.
You can decrypt DiscoveryData by calling decryptSecureBeacons(discoveryDataList:)
. To detect a SecureBeacon, you have to set your secureBeacon UUID to BKDiscoverySettings.securebeaconUUID
The delegate BKDecryptSecureBeaconsDelegate.onDecryptSecureBeacons(_:decryptedCount:)
is retrieving a copy of the input parameter list containing decrypted values. Each BKDiscoveryData object, that has been decrypted is marked with BKDiscoveryData.secureBeaconState
as BKSecureBeaconState.decrypted
.
func onDecryptSecureBeacons(_ decryptedDiscoveryDataList: [BKDiscoveryData], decryptedCount: Int) {
print("BlukiiClient.onDiscoveryResult: DecryptSecureBeacons Successful: Count=" + decryptedCount)
}
func onDecryptError(_ error: Error) {
print("BlukiiClient.onDiscoveryResult: DecryptSecureBeacons failed: error=" + error.localizedDescription)
}
Note: SecureBeacons can only be decrypted by its owner, so the corresponding user needs to be logged in (@see BKBlukiiCloud
)
-
Discovery Delegate for BLE discovery
Declaration
Swift
open var discoveryDelegate: BKClientDiscoveryDelegate?
-
SecureBeacon Decryption Delegate
Declaration
Swift
open var decryptSecureBeaconDelegate: BKDecryptSecureBeaconsDelegate?
-
Discovery Settings
Declaration
Swift
internal(set) open var discoverySettings: BKDiscoverySettings!
-
Gets the state if BLE scanner is actuallly running.
Declaration
Swift
open func isDiscovering() -> Bool
Return Value
true, if BLE scanner is scanning
-
Initializes the discovery service.
If successful, the delegate
BKClientDiscoveryDelegate.onDiscoveryInitialized()
is called. Otherwise the delegateBKClientDiscoveryDelegate.onDiscoveryError(_:)
is called with an error.Note: By calling this function the service instance BKDeviceDiscoveryService is started and binded by the BKClient object. This is required before calling
startDiscovery()
andstopDiscovery()
for starting and stopping the discovery for BLE modules. WithoutstartDiscovery()
BKClient
is not do any action and therefore not using the battery.Note: Use this function only, if the view is loaded. For Example viewDidAppear.
Declaration
Swift
open func initDiscovery()
-
Gets the state if the discovery service is initialized.
Declaration
Swift
open func isDiscoveryInitialized() -> Bool
Return Value
true, if discovery service is initialized
-
Starts the BLE scanner.
It only can be started if the discovery service has been initialized befor by calling
initDiscovery()
!Note: Only when the BLE scanner is started successfully the
BKClientDiscoveryDelegate
delegates are called if there is any discovery result, error or info.You can stop scanning by calling
stopDiscovery()
Declaration
Swift
open func startDiscovery() -> Bool
Return Value
true, if start is successful.
-
Stops the BLE scanner.
You can start scanning by calling
startDiscovery()
Declaration
Swift
open func stopDiscovery() -> Bool
Return Value
true, if stop is successful.
-
The Default SecureBeacon cache Timeout
Declaration
Swift
public static let SECUREBEACON_CACHETIMEOUT_DEFAULT: Int
-
Gets the current cache timeout for secure beacon decryption. If value is 0, cache is off.
Declaration
Swift
open func getDecryptSecureBeaconCacheTimeout() -> Int
Return Value
Timeout in seconds
-
Sets the current cache timeout for secure beacon decryption. If value is 0, cache is off.
Declaration
Swift
open func setDecryptSecureBeaconCacheTimeout(_ decryptSecureBeaconCacheTimeout: Int)
-
Tries to decrypt the
discoveryDataList
list items that are marked withBKDiscoveryData.secureBeaconState
asBKSecureBeaconState.encrypted
. If successfulBKDecryptSecureBeaconsDelegate.onDecryptSecureBeacons(_:decryptedCount:)
is called with the result list, otherwiseBKDecryptSecureBeaconsDelegate.onDecryptError(_:)
is called. Therefore an blukii Manager user authentication viaBKBlukiiCloud.login()
is needed.Declaration
Swift
open func decryptSecureBeacons(discoveryDataList: [BKDiscoveryData])
Parameters
discoveryDataList
discoveryData Array that needs to be decrypted