-
Notifications
You must be signed in to change notification settings - Fork 43
Apple Watch Integration
See the iOS Peripheral sample project for additional guidance.
In order to integrate watch functionality into your project, it is recommended you start the project using Apple's template for "iOS App with WatchKit App". That will enable you to create a WatchKit Extension for your app.
Your iOS project must import the iOS version of the framework and the WatchKit Extension must import the watchOS version.
Your Watch Extension should use the startAs method to launch as a Peripheral, and set an instance variable for the VgcWatchConnectivity class, which the watchOS framework publishes:
VgcManager.startAs(.Peripheral, appIdentifier: "", customElements: CustomElements(), customMappings: CustomMappings())
watchConnectivity = VgcWatchConnectivity()
Within your watch extension, use the following method on your VgcWatchConnectivity instance variable to send an element value to both your iOS app (living on your phone) and the Central (forwarded from the iOS app automatically):
let element = watchConnectivity.elements.rightShoulder
element.value = 1.0
watchConnectivity.sendElementState(element)
element.value = 0.0
watchConnectivity.sendElementState(element)
When your watch sends values as described in the previous section, there are two approaches to handling those messages. If you do nothing, the values will be automatically forwarded to the Central. If you want to do handling on the Peripheral, and either forward or not forward, you can set the valuChangedHandler like so:
VgcManager.peripheral.watch.valueChangedHandler = { (element: Element) in
print("iOS iPhone watch handler fired for \(element.name) with value \(element.value)")
}
If the valueChangedHandler is non-nil, values will no longer be automatically forwarded to the Central, and so if you wish those values to be forwarded, you must do so yourself:
VgcManager.peripheral.sendElementState(element)
To send a value to a watch from your Peripheral iOS app, you can use the following method:
VgcManager.peripheral.watch.sendElementState(element)
If a watch is paired with your iPhone iOS app, values received by the iOS app from a Central will be forwarded to the watch, and you can react to those values by setting a valueChangedHandler on your watchConnectivity instance:
watchConnectivity.valueChangedHandler = { (element: Element) in
print("Watch handler fired for \(element.name) with value \(element.value)")
}
Note that the same handler will fire if your Peripheral sends a value to the watch as described above.
You can send motion values from the watch, but please note that the performance is not great:
watchConnectivity.motion.start()
watchConnectivity.motion.stop()