how hot is my coffee? sensors, ios and core bluetooth
TRANSCRIPT
How Hot is My Coffee? Sensors, iOS and Core Bluetooth
Evan K. Stone Senior iOS Developer
Cloud City Development // San Francisco
An integrated design and so-ware consultancy specializing in web and mobile apps
There is an explosion.
Not bombs.
Usually doesn’t hurt people.
Very oCen helps people.
This explosion is…
The Internet of Things
Connected devices
Nest. Fitbit. Polar.
Key technology behind it?
Bluetooth Low Energy
What is it?
Not Classic Bluetooth.
A lightweight subset
AKA “Bluetooth Smart”
Why do we care?
It’s exciSng!
It’s fun!
It can be rewarding.
Allows us to interact with physical devices
We can develop iOS apps that communicate with other things
around us!
No cerSficaSon required!
Super Low Barrier for Entry.
TI SensorTag Ambient Light
Digital Microphone
MagneSc Sensor
Humidity
Accelerometer GyroscopeMagnetometer
Ambient Temperature
IR Temperature
$29
What does this mean for us?
The IoT Landscape is changing
Tons of innovaSon going on right now.
Startups
Incubators and Accelerators
They will need you.
Ge]ng started now will help you help them.
Side note:
What about iBeacons?
Subject of another talk!
iBeacons are BLE devices.
Behave differently
They are also fun to work with.
Are handled by a different framework!
Core LocaSon
Ge]ng back to BLE…
Acronym Time!
GAPGeneric Access Profile
Defines how devices adver&se themselves and how they connect
Defines the concept of the Central and the Peripheral
GATTGeneric Aaribute Profile
Defines the way devices transfer data back and forth
Introduces Services and CharacterisScs
You won’t be tested on the acronyms.
Just know they exist.
Roles
We are already used to Client and Server roles
In BLE we have the Central and Peripheral roles
Central
Wants data from a Peripheral
Generally this is your iOS device.
Peripheral
Supplies data.
A device like a heart rate monitor
Becomes an extension of our iOS device, giving it abiliSes it didn’t
have previously
Side note:
An iOS device can be a Peripheral…
…but typically they act as a Central.
The Age of Discovery
For devices to communicate with each other, they must find each
other.
AdverSsing and Discovery
The Peripheral AdverSses
The Central scans and listens
When it finds a Peripheral, the Central can ask to connect.
Kinda like a headhunter on LinkedIn.
Services and CharacterisScs
Services group data
Services contain CharacterisScs
Service
Characteris2c
Characteris2c
Characteris2c
Central connects and discovers Services and CharacterisScs the
peripheral offers
But how?
Core Bluetooth
Apple’s abstracSon over all that Bluetooth gobbledygook.
Encapsulates the funcSonality of the BLE enSSes in framework classes
Remember the four main players?
Central and Peripheral. Service and CharacterisSc.
Everything begins with a CB
Peripheral
CBPeripheral
Central
CBCentralManager
Service
CBService
CharacterisSc
CBCharacterisSc
Side note:
Caveat with Core Bluetooth development
iOS Simulator does not support BLE.
So you have to use a device.
Which devices support BLE?
iPhone 4s and later.iPad 3 and later.
Basic Workflow
Feels very client-server
That’s not an accident.
Workflow:
InstanSate a CBCentralManager
Tell CBCentralManager to scan for Peripherals with Services
we want
When it finds a Peripheral, request to connect
ACer connecSng, find out more about the Peripheral
Read and set characterisScs
Rinse, Lather, Repeat…
Disconnect.
What makes this work?
Enter the UUID.
Enter the UUID.
Standard UUIDs have a 16-bit form.
0x180A
Custom UUIDs have a 128-bit form.
F000AA00-0451-4000-B000-000000000000
What do we mean by Standard and Custom UUIDs?
For example…
Heart Rate Monitor Service0x180D
Heart Rate Measurement CharacterisSc
0x2A37
ExciSng Reading!developer.bluetooth.org
🤓
Custom Service UUIDs
Anything not listed on the standard list.
For example…
IR Temperature SensorF000AA00-0451-4000-B000-000000000000
Where do I find these crazy numbers?
Typically on the vendor website
Or the hardware engineer on your team creates them!
Back to scanning…
CBCentralManager
Core Bluetooth relies heavily on delegates
CBCentralManagerDelegate
CBPeripheralDelegate
Demo Time!
🎉
How Hot is My Coffee?
Side note:
Backgrounding
Yes it’s possible!
Requires a flag in your Info.plist
key: UIBackgroundModesvalue: bluetooth-central
Devices
Many to choose from
New ones appear constantly
TI SensorTaghap://www.S.com/sensortag
LightBlue Bean/Bean+haps://punchthrough.com/bean
Adafruit Feather 32u4 Bluefruit LEhaps://www.adafruit.com/products/2829
TinyCircuitshaps://Sny-circuits.com/
The IoT space is exploding!
More connected devices are being developed every day!
More connected apps are needed every day!
As an iOS developer, you can be on the cu]ng edge of this new fronSer!
Explore!
Experiment!
Connect and Communicate
Core Bluetooth.
Resources
Cloud City DevelopmentBlog hap://cloudcity.io/blog/2015/06/11/zero-to-ble-on-ios-part-one/ hap://cloudcity.io/blog/2015/10/15/developing-ios-app-using-ble-standard/
GitHub haps://github.com/cloudcity/BLECoffee haps://github.com/cloudcity/ZeroToBLE-Part2
Apple's Official Core Bluetooth Docs
IntroducSon to Core Bluetooth Background OperaSon
Best PracSces
Adafruit's IntroducSon to BLE haps://learn.adafruit.com/introducSon-to-bluetooth-low-energy/introducSon
Evan K. Stone
web: h<p://www.cloudcity.io
cloud city blog: h<p://blog.cloudcity.io
blog: h<p://www.interac?velogic.net
twi<er: @interac?vlogic