Advanced development tips on creating Apple WatchKit apps that connect to hardware via Bluetooth Low Energy.
Presentation (and live demo) given at Silicon Valley iOS Meetup in May 2015.
2. 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.
3. 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
4. Watch - Phone - Device
Watch
iPhone
Device
WatchKit Bluetooth LE
Commands Data
5. 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?)
7. 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
8. For the lazy
• MMWormhole
• https://github.com/mutualmobile/MMWormhole
• General purpose mechanism to trade between
apps and extensions
10. Does not work
(for connected apps)
• Watch launches app in background
• CoreBluetooth has degraded BLE scanning in
background
• Watch app times out
• Catch 22!
11. 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
12. Solution
• Require user to launch app on phone first.
• Make sure app can do BLE in background
• Put phone away
13. 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)
15. 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
16. Onewheel
• Self-balancing motorized one-wheel
skateboard.
• BLE used for virtual dashboard: battery
level, ride mode, system stats, and over-
the-air firmware updates.
17. Apple Watch app
• Required on-device testing (simulator
does not support BLE)
• Available on launch day
• http://www.rideonewheel.com