Connectivity
public class Connectivity : NSObject
Class is used to register your 3rd party app for use with the companion BLE framework.
-
Shared Instance, Singleton
Declaration
Swift
public static let shared: Connectivity
-
Notification.Name that Connectivity.state has changed. UserInfo dictionary contains “state” of connectivity.
NotificationCenter.default.addObserver(forName: Connectivity.connectivityStateChanged, object: nil, queue: nil) { [weak self] (notification) in if let state = notification.userInfo?["state"] as? ConnectivityState { ... } }
Declaration
Swift
public static let connectivityStateChanged: Notification.Name
-
State of connectivity
Declaration
Swift
public var state: ConnectivityState { get set }
-
Connectivity established and ready.
Declaration
Swift
public var isConnected: Bool { get }
-
Register a BroadcastReceiver with an intent action.
Declaration
Swift
public func registerReceiver(intentAction: String, receiver: BroadcastReceiver)
Parameters
intentAction
the intent action that you want to listen for
receiver
the broadcast receiver that you would like to execute when connectivity receivers intent action.
-
Register a BroadcastReceiver with an intent action.
Declaration
Swift
public func registerReceiver(intentAction: String, onReceiveBlock: @escaping (Intent) -> Void) -> BroadcastReceiver
Parameters
intentAction
the intent action that you want to listen for
onReceiverBlock
the code block to be executed when framework recieves intent.
Return Value
BroadcastReceiver. Hold reference so you can stop listening.
-
UnRegister a BroadcastReceiver from an intent action.
Declaration
Swift
public func unregisterReceiver(intentAction: String, receiver: BroadcastReceiver)
Parameters
intentAction
the intent action that you want to stop listen for
onReceiverBlock
Broadcast receiver that was listening.
-
Request connection a to share connectivity with compaion app. Listen for notification on the connectivity state.
See also
ConnectivityStateif connectivity.isConnected == false { connectivity.requestConnection() } else { self.textView.text = "--Connected to Blade" } NotificationCenter.default.addObserver(forName: .connectivityStateChanged, object: nil, queue: nil) { [weak self] (notification) in if let state = notification.userInfo?["state"] as? ConnectivityState { if state == ConnectivityState.connected { self?.textView.text = "--Connected to Blade!" } else if state == ConnectivityState.searchingForDevice { self?.textView.text = "--Searching for Device" } else if state == ConnectivityState.connecting { self?.textView.text = "--Connecting to Device" } } }
Declaration
Swift
public func requestConnection()
-
Broadcast this intent to the device.
The call is asynchronous and returns immediately. No results are returned from the receivers.
Remark
If you want results to be returned from the receiver then use orderedBroadcast.
See also
orderedBroadcast(callBack:)
Declaration
Swift
public func broadcast(intent: Intent)
-
Broadcast this intent to the device and receive data back.
Declaration
Swift
public func orderedBroadcast(intent: Intent, callBack: @escaping ((BroadcastResult) -> Void))
Parameters
callBack
called when data is received from original broadcast. Result is of type
BroadcastResult
. -
Broadcast this intent to the device and receive data back.
Declaration
Swift
public func orderedBroadcast(intent: Intent, extras: BundleContainer?, callBack: @escaping ((BroadcastResult) -> Void))
Parameters
callBack
called when data is received from original broadcast. Result is of type
BroadcastResult
.extras
optional
BundleContainer
of data you want to send with intent