Top Banner
Watchkit Tips for BLE Connected Apps Ramin Firoozye SVIOS Meetup - May 2015
20
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: WatchKit Tips for Bluetooth Low Energy Apps

Watchkit Tips for BLE Connected Apps

Ramin Firoozye SVIOS Meetup - May 2015

Page 2: WatchKit Tips for Bluetooth Low Energy Apps

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.

Page 3: WatchKit Tips for Bluetooth Low Energy Apps

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

Page 4: WatchKit Tips for Bluetooth Low Energy Apps

Watch - Phone - Device

WatchiPhone

DeviceWatchKit Bluetooth LE

Commands Data

Page 5: WatchKit Tips for Bluetooth Low Energy Apps

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?)

Page 6: WatchKit Tips for Bluetooth Low Energy Apps

Inter-process Notifications

Phone AppWatch Extension

NSUserDefaults for data

System notifications for events

*NSNotificationManager is for single process use

WatchMagic!

Page 7: WatchKit Tips for Bluetooth Low Energy Apps

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

Page 8: WatchKit Tips for Bluetooth Low Energy Apps

For the lazy

• MMWormhole• https://github.com/mutualmobile/MMWormhole • General purpose mechanism to trade between

apps and extensions

Page 9: WatchKit Tips for Bluetooth Low Energy Apps

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

Page 10: WatchKit Tips for Bluetooth Low Energy Apps

Does not work (for connected apps)

• Watch launches app in background • CoreBluetooth has degraded BLE scanning in

background • Watch app times out • Catch 22!

Page 11: WatchKit Tips for Bluetooth Low Energy Apps

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

Page 12: WatchKit Tips for Bluetooth Low Energy Apps

Solution

• Require user to launch app on phone first. • Make sure app can do BLE in background

• Put phone away

Page 13: WatchKit Tips for Bluetooth Low Energy Apps

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)

Page 14: WatchKit Tips for Bluetooth Low Energy Apps

Live Demo

Onewheel

Page 15: WatchKit Tips for Bluetooth Low Energy Apps

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

Page 16: WatchKit Tips for Bluetooth Low Energy Apps

Onewheel

• Self-balancing motorized one-wheel

skateboard. • BLE used for virtual dashboard: battery

level, ride mode, system stats, and over-

the-air firmware updates.

Page 17: WatchKit Tips for Bluetooth Low Energy Apps

Apple Watch app

• Required on-device testing (simulator

does not support BLE) • Available on launch day • http://www.rideonewheel.com

Page 18: WatchKit Tips for Bluetooth Low Energy Apps
Page 19: WatchKit Tips for Bluetooth Low Energy Apps

Let’s ride!

Page 20: WatchKit Tips for Bluetooth Low Energy Apps

Q&A

• Ramin Firoozye • raminf AT gmail • @raminf