SensorDispatch
public class SensorDispatch
A SensorDispatch object is used to receive sensor readings. A SensorDispatch object listens for the underlying WearableDeviceEvent notifications indicating that sensor data has been received. It then unpacks the individual values from the data and dispatches each to its appropriate callback block and to its handler (a SensorDispatchHandler object).
An app can have arbitrarily many SensorDispatch objects. This allows multiple handlers inside an app all to listen to the same sensor. Each SensorDispatch automatically registers and deregisters for the appropriate WearableDeviceEvent notifications.
A SensorDispatch object is created with an associated OperationQueue. Specifying the .main queue is suitable for when the app’s user interface should be updated in response to the event. Otherwise, a background OperationQueue can be provided.
When a WearableDeviceEvent.didReceiveSensorData(SensorData) notification is received, the SensorDispatch object does the following on the OperationQueue provided to the SensorDispatch initializer:
- Dispatches the
SensorDataobject tosensorDataCallbackand toSensorDispatchHandler.receivedSensorData(_:). - Each of the values in the
SensorDataobject is dispatched to the appropriate handler:- Accelerometer values are dispatched to
accelerometerCallbackand toSensorDispatchHandler.receivedAccelerometer(vector:accuracy:timestamp:) - Gyroscope values are dispatched to
gyroscopeCallbackand toSensorDispatchHandler.receivedGyroscope(vector:accuracy:timestamp:) - Rotation values are dispatched to
rotationCallbackand toSensorDispatchHandler.receivedRotation(quaternion:accuracy:timestamp:). - Game rotation values are dispatched to
gameRotationCallbackand toSensorDispatchHandler.receivedGameRotation(quaternion:timestamp:). - Orientation values are dispatched to
orientationCallbackand toSensorDispatchHandler.receivedOrientation(vector:accuracy:timestamp:) - Magnetometer values are dispatched to
magnetometerCallbackand toSensorDispatchHandler.receivedMagnetometer(vector:accuracy:timestamp:) - Uncalibrated magnetometer values are dispatched to
uncalibratedMagnetometerCallbackand toSensorDispatchHandler.receivedUncalibratedMagnetometer(vector:bias:timestamp:)
- Accelerometer values are dispatched to
Callbacks are only invoked if they are non-nil. Thus you need only provide callback blocks for sensors you are interested in. Similarly, the SensorDispatchHandler protocol provides default implementation of all of its handler functions. Thus you need only provide implementations for sensors you are interested in. A SensorDispatchHandler and callback blocks can co-exist. Each callback is invoked before the corresponding SensorDispatchHandler function.
-
The sensor dispatch handler. This is invoked in response to any incoming sensor data.
Declaration
Swift
public weak var handler: SensorDispatchHandler? -
Creates a new
SensorDispatchobject that will dispatch incoming sensor data on the specified operation queue.Declaration
Swift
public init(queue: OperationQueue, session: WearableDeviceSession? = nil)
-
Callback to receive aggregated sensor data. This is useful if you need to tie together the various readings received in a single update.
Declaration
Swift
public var sensorDataCallback: ((SensorData) -> Void)?
-
Callback to receive gesture data.
Declaration
Swift
public var gestureDataCallback: ((GestureType, SensorTimestamp) -> Void)?
-
Callback to receive accelerometer readings.
Declaration
Swift
public var accelerometerCallback: ((Vector, VectorAccuracy, SensorTimestamp) -> Void)?
-
Callback to receive gyroscope readings.
Declaration
Swift
public var gyroscopeCallback: ((Vector, VectorAccuracy, SensorTimestamp) -> Void)?
-
Callback to receive rotation readings.
Declaration
Swift
public var rotationCallback: ((Quaternion, QuaternionAccuracy, SensorTimestamp) -> Void)?
-
Callback to receive game rotation readings.
Declaration
Swift
public var gameRotationCallback: ((Quaternion, SensorTimestamp) -> Void)?
-
Callback to receive orientation readings.
Declaration
Swift
public var orientationCallback: ((Vector, VectorAccuracy, SensorTimestamp) -> Void)?
-
Callback to receive magnetometer readings.
Declaration
Swift
public var magnetometerCallback: ((Vector, VectorAccuracy, SensorTimestamp) -> Void)?
-
Callback to receive uncalibrated magnetometer readings.
Declaration
Swift
public var uncalibratedMagnetometerCallback: ((Vector, Vector, SensorTimestamp) -> Void)?
SensorDispatch Class Reference