Bluetooth Pairing

Last Updated on : 2024-05-28 06:22:47download

Initialize IActivator

Parameters

Parameter Type Required Description
mode ActivatorMode Yes The pairing mode.

Example

self.pair = ActivatorService.shared.activator(.BLE)

Initialize device scanning IDiscoveryService

Parameters

Parameter Type Required Description
mode DiscoveryMode Yes The scanning mode.

Example

self.discovery = DiscoveryService.shared.discovery(.BLE)

Register IDiscoveryListener to listen for scanning result

Parameters

Parameter Type Required Description
device AnyObject Yes The discovered device object. If the device is Bluetooth Low Energy (LE), the object is ThingBLEAdvModel.

Example

private func start() {
    discovery.listener = self
}

extension BluetoothModeViewController: IDiscoveryListener {
    func didDiscover(device: AnyObject) {
        guard let model = device as? ThingBLEAdvModel, let uuid = model.uuid else {return}
        self.deviceInfos[uuid] = model
        self.tableView.reloadData()
    }
}

Register IActivatorListener to listen for pairing result

Parameters

Parameter Type Required Description
deviceModel IActivatedDevice? Yes If the operation is successful, it indicates the information about the activated device. Otherwise, it is nil.
params ActivatorParams? Yes The parameters of actions.

Callbacks

Function name Parameter Description
onSuccess deviceModel: IActivatedDevice? , params: ActivatorParams? The success callback, returning the information about the activated device.
onError error: Error, params: ActivatorParams? The failure callback, returning an error message.

Example

private func start() {
    pair.listener = self
}

extension TableViewController: IActivatorListener {
    func onSuccess(deviceModel: IActivatedDevice?, params: ActivatorParams?) {
        SVProgressHUD.dismiss()
        self.navigationController?.popToRootViewController(animated: true)
    }

    func onError(error: Error, params: ActivatorParams?) {
        SVProgressHUD.dismiss()
        SVProgressHUD.showInfo(withStatus: error.localizedDescription)
    }
}

Start scanning

This method initiates device scanning.

Parameters

This method has no parameters.

Example

self.discovery.startDiscovery()

Stop scanning

Example

// Stop scanning for devices. discovery is generated during initialization.
// self.discovery = DiscoveryService.shared.discovery(.BLE)
discovery.stopDiscovery()

Start pairing

This method starts device pairing.

Parameters

Parameter Type Required Description
params ActivatorParams Yes The parameters of the pairing action. See the example.
deviceInfo DeviceInfo Yes Device information, an array that stores the ThingBLEAdvModel object.
assetId String Yes The asset ID.

Example

// Create BLEActivatorParams instance.
let params = BLEActivatorParams(deviceInfo: devInfo, assetId: assetID)

// Start pairing. pair is generated during initialization.
// self.pair = ActivatorService.shared.activator(.BLE)
pair.startPair(params)

Stop pairing

Example

// Stop pairing. pair is generated during initialization.
// self.pair = ActivatorService.shared.activator(.BLE)
pair.stopPair()