watchkit tips for bluetooth low energy apps
TRANSCRIPT
General Issues• No CoreBluetooth BLE libraries available in
WatchKit. • Phone app / Watch extension data exchange. • Background mode Catch-22.
Also: No simulator support for CoreBluetooth!
Can only test on real watch.
No direct CoreBluetooth• Watch has to talk to watch extension • Watch extension has to talk to main app • Main app has to talk to device via BLE
Watch / Phone data exchange
• Phone apps and Watch extensions run in
separate process address spaces • Normal delegate/block/NSNotification
won’t work • Also need to consider app launch
sequence (phone first or watch first?)
Inter-process Notifications
Phone AppWatch Extension
NSUserDefaults for data
System notifications for events
*NSNotificationManager is for single process use
WatchMagic!
Data interchange
• Trade data via NSUserDefaults • Notify vs. Darwin queues or Mach Kernel
low-level mechanisms • For best performance send a hint along
with event pointing at update
For the lazy
• MMWormhole• https://github.com/mutualmobile/MMWormhole • General purpose mechanism to trade between
apps and extensions
Background Mode Catch-22
WKInterfaceController.openParentApplication(
["data" : "start"],
reply: { (replyDict, error) -> Void in
NSLog("Phone app launched")
})
Standard way to launch phone app from watch
Does not work (for connected apps)
• Watch launches app in background • CoreBluetooth has degraded BLE scanning in
background • Watch app times out • Catch 22!
Another BLE issue
• Pair or Connect prompt on first launch of
phone app • Impossible if you need bonded connections/
system prompt • Result: complex watch app UI
Solution
• Require user to launch app on phone first. • Make sure app can do BLE in background
• Put phone away
Why does this work?
• CoreBluetooth works fine in background if
BLE connection to device is already open • Works even if phone is put to sleep • Protip: test against latest iOS version (8.2
broke this. 8.3 fixed it)
Onewheel• Self-balancing motorized one-wheel
skateboard. • BLE used for dashboard: battery level,
ride mode, system stats, and over-the-air
firmware updates. • http://www.rideonewheel.com
Onewheel
• Self-balancing motorized one-wheel
skateboard. • BLE used for virtual dashboard: battery
level, ride mode, system stats, and over-
the-air firmware updates.
Apple Watch app
• Required on-device testing (simulator
does not support BLE) • Available on launch day • http://www.rideonewheel.com