42
BLE 101 Jacob Rosenthal pretty deep dive. Expect you'll want a copy of the slides to refresh i hear DCC likes code talks, so this is a bit of a bait and switch So really more of a state of BLE only a SMALL amount of code at the end

Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Embed Size (px)

DESCRIPTION

Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Citation preview

Page 1: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

BLE 101Jacob Rosenthal

pretty deep dive. Expect you'll want a copy of the slides to refreshi hear DCC likes code talks, so this is a bit of a bait and switchSo really more of a state of BLEonly a SMALL amount of code at the end

Page 2: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

citizengadget.com

@jacobrosenthal

About Me

#hackPHX

Consulting hardware and firmware engineer

HeatSync and HackPHXConsulting hardware engineer - Hardware, firmwareDone work for Octoblu several talks here todayConsumer products, a lot of IOT right nowWearables my passion - did research at ASU and still consult for them on hapticscitizengadget.com@jacobrosenthal

fine places to get ahold of me if you need some work done

Page 3: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE • Choosing your BLE solution • Platform support • Code (javascript) • Conclusion

Agenda

Seems simple enough… But lets see

Page 4: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE• Choosing your BLE solution • Platform support • Code (javascript) • Conclusion

Agenda

Seems simple enough… But lets see

Page 5: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Overview of BLE

Bluetooth Smart (Bluetooth 4.0)

• Bluetooth Classic • Bluetooth High Speed • Bluetooth Low Energy - nee Wibree/ULP

Bluetooth 4.0 vs BLE

Bluetooth 4 (also called Bluetooth Smart) is really three things, Bluetooth Low Energy, Bluetooth High Speed, and Bluetooth Classic.

Bluetooth Classic (ie all of BT2.1, or BT3.0 is still there!)Bluetooth High Speed most people have never heard of yet. It lets Bluetooth Classic up negotiate a Wifi connection for high speed transfers. Finally Bluetooth Low Energy, BLE, is what we all talk about when we say BT4.0 - entirely new protocol stack

Page 6: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

http://en.wikipedia.org/wiki/Bluetooth_low_energy

Overview of BLE

So why did we a need BLE? as much as 2 magnitudes decrease in power consumption but you can push it, if you use the shit out of it its not low power anymorehow:by reducing throughput from 0.7–2.1 Mbit/s to 0.27 Mbit/sreducing range from ~300ft to what Ive seen and experienced ~30ftreducing advertising rate to as low as every other second, and only on 3 of 40 channels so less to search through too means device doesn't have to stay awake as much

All this means generally poorly supports high throughput or high current like streaming audio, leds/motors. Again BLE doesn't replace BT Classic.

Also BT is a pain in the dick - sets out to fix that in some ways

Page 7: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Overview of BLE - Terms

GAP - Master/Slave or rather Central/Peripheral • Pairing - actually separated into Connection and

Authentication

BLE is a master/slave system, or rather Central/Peripheral. One connection at a time. No advertising while connected..

Page 8: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Overview of BLE - Terms

Broadcasting - Sneak some data in Advertising. • In practice lets one device talk to many • Simple! - Uses nothing else well talk about today. • iBeacon is just Apples trademark for this concept • Almost nobody is doing anything interesting yet

Advertising - big new thing here is broadcasting, we can sneak some data in

Slaves ping out their advertising data rlatively rarely- including its tx power, ssid name and another up to 20 bytes of data. This data could be dynamic -, but no implementations I've come across let you change it outside of initial setup of the chip. This is what iBeacons and other proximity are using. Beware though, In my experience you really can only tell three distinct magnitudes <1 foot, <10ft, <100ft.

This data could be dynamic, (stuff sensor data in there) though no implementations I've come across utilize that fact. Ive asked punch through why it hasn't been done yet and they think its because the apple implementation wouldn't support it easily and they’re the biggest dog right now.

Page 9: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

BLE Defined - Profiles, Services, Characteristics

https://learn.adafruit.com/introduction-to-bluetooth-low-energy/introduction

profiles hath services have characteristics

Page 10: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

BLE Defined - Profiles, Services, Characteristics

https://developer.bluetooth.org/TechnologyOverview/Pages/HRP.aspx

heartrate profile has heart rate service and device information servicehttps://developer.bluetooth.org/TechnologyOverview/Pages/HRS.aspx

Spec defines tons of profiles like blood pressure, alerts, thermometer, heart rate, HID, Proximity, Time, Or make your own!

Page 11: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

BLE Defined - Profiles, Services, Characteristics

https://developer.bluetooth.org/TechnologyOverview/Pages/HRS.aspx

Theres a location, the measurement itself, and a control (settings) characteristic

Page 12: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

BLE Defined - Profiles, Services, Characteristics

https://developer.bluetooth.org/TechnologyOverview/Pages/HRS.aspx

we can read the sensor location characteristic, and set a notification (subscribe) to the heart rate measurement characteristic, and we can write to the configurator descriptor (device setup, in this case to tell device to start or stop sending data)

Page 13: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Overview of BLE - Terms

UUIDs • Devices • Services • Characteristics

Bean - A495FF10-C5B1-4B44-B512-1370F02D74DE

Heart Rate -180D

uuidsofficial services and characteristics have a shortened 16 bit uuid, devices and custom services/characteristics have 128 bit uuids

Page 14: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE • Choosing your BLE solution• Platform support • Code (javascript) • Conclusion

Agenda

Page 15: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

Metawear, Bleduino, Zwear, Rfduino, RedBear Punchthrough

Page 16: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

• Does it have the profile you want? • Does it let you make profiles? • Actually Low power? • Does it force you to use an IDE? • Can you program it on mac/linux? • Is it actually Arduino?

MacGuffins: • Led • Accelerometer! • Temperature • Size • The word Arduino

Better criteria:

Page 17: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

Bean

2 microcontrollers, their module + actual Arduino Program wirelessly

Immature SDK - Not a lot of services / profilesCant make your own (though they have ‘scratch’

characteristics)

I wont bury the lead. Ill start with the best and it'll get worse.

Page 18: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

Metawear

You don't write ‘arduino’ code at all

You’re stuck with what sensors it has

Page 19: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

Redbear

Terrible Open Source track record

Actually Arduino

Really complex arduino SDK and uses up lots of your arduino

Lets look at our list again

redbeararduino has to communicate with nrf8001 transceiver which means its really complicated and theres not a lot of room for your stuff on the micro. Also very disappointed with their 'open source' label. have years long tickets asking for schematics and theyve finally released their ios stuff but it doesnt have firmata or other features their apps use. Continue to promise they'll release new stuff, well see.

New stuff coming out very soon, but their track record is so bad.

Page 20: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

ZWear

Its open source, but you have to use a Windows only IDE called IAR

zwear is ‘open source’ (havent checked) some windows only IDE called IAR!

Page 21: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

Chinese tranceivers

SPP devices, only Serial data out over BLE. No access to characteristics or services

Terrible battery life, no real way to put them to sleep. They’re always listening for serial data to send!

Page 22: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - How to pick?

RFDuino

Terrible Open Source track record

Actually not Arduino. Rewriting arduino from scratch for arm processor!

Lets look at our list again

RFDuino was supposed to be open source + arduino but got into a lot of trouble and turned their back on open source. Not actually arduino! like Spark (but closed source) are rewriting arduino for arm AND rewriting nordics code so its a heroic effort and slow going

Page 23: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

How did this get so hard so quick?

Page 24: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution

Chip

Module

Solution

TI, CSR, ST blueNRG, Nordic, Broadcom

Adafruit, RedBear, Panasonic, Bluegiga, Laird, ConnectBlue, Fujitsu, Stollman, Blue Radios,

RfDuino, PunchThrough

Metawear, Bleduino, Zwear, Rfduino, RedBear

Punchthrough

$2-5

$15-30

~$30

So lets separate our vendors into 3 categories, theres tons more vendors but this is a start

Page 25: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution

Chip TI, CSR, ST blueNRG, Nordic

mbientlab.com/blog/how-much-does-ble-really-cost-a-tip-from-renaissance-io/

Probably none of you will ever dwell here, and I hope to talk you out of it, but to explain that we need to talk at the chip level.

ibeacons can be had under 5 each!? why pay 30?!http://mbientlab.com/blog/how-much-does-ble-really-cost-a-tip-from-renaissance-io/

First as a prototyper and consumer products engineer, not really. Basically nothing can be made and shipped for under $30. Arduino, $30, Arduino Shields $30, the list goes on and onTrue at scale and its not your main business model you can get Chinese clones and cheap shit for $15 but that doesn't support code development, support or anything else.

Page 26: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution

Caution:

Chip TI, CSR, ST blueNRG, Nordic

• Difficult footprints to solder • NDA (forget open source) • Quantity Buys, only dealing with Corps • Windows only tools (No GCC) • Per Seat Licensing • $100s in development tools • Antenna engineering • FCC Certification ($5k per region) • Arm complexity (this aint Arduino)

Probably none of you will ever dwell here, and I hope to talk you out of it, but to explain that we need to talk at the chip level.

solder footprints, ndas, quantity buys and corporate domains, windows only, per seat licensing mean tools and compilers can cost thousands!, antenna engineering, arm isn't arduino—all arm code is an island, not like what arduino has accomplished over the past few years

For nerds, closest right now is Nordic specifically their nrf51 parts and the MBED stack (arduino for arm) which is where Im currently focusing

Knowing that NRF is the closest starts to tell you which solution you might want to look at

Page 27: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution

Metawear, Bleduino, Zwear, Rfduino, RedBear Punchthrough

So these guys absorbing all that pain for you, and charging you for itBut no NDA means they can’t break their NDA, so they’re Rewriting the entire god damn stack

Bluegiga uses cc2540 and sells bgscript to get you out of that nightmare.RFduino, laird, uses nordicPunchthrough uses TI, RedBear used to use TI, now uses Nordic?Cheap Chinese modules use CSR and ISSC - just serial port passthrough

Page 28: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Choosing BLE Solution - PunchThrough Bean

Bean

Prefer nordic so we can eventually drop down to their base chip if we go to production

so after all that, even though its based on TI, Punchthrough wins anyway runner up to bleeding

its a silicon valley startup spin on itdoing a great job making it friendlyBean actually has an arduino, don't have to reengineer that piecebut doesn't fuss up your sketch with nonsense or use up all of your cyclespower is goodcode is.. coming

Im picking NRF51 based solutions for developers who are going to drop down and want more power

Page 29: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE • Choosing your BLE solution • Platform support• Code (javascript) • Conclusion

Agenda

Page 30: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Platform Support - Windows

• May be built in, or buy dongle

• Windows 8+

Software Hardware

BLE is already here

Start with the bad though

Windows8 brought BLE support, but I don’t know how much hardware has it built in. In the Windows ecosystem though, its a cheap purchase to grab a BLE dongle from amazon.However early reports are not good. Punchthrough is months behind on their windows supportrecent progress means they might get it, but indicating that workflow significantly poorer than other platformshttps://punchthrough.com/bean/development-schedule/Sorry windows people, but by now you should understand Windows is a platform saddled with huge legacy.

Page 31: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Platform Support - Android

• Samsung S3 and Note 2+

• HTC One+ • LG Optimus

4x, Nexus 4, Optimus G+

• Jellybean+

Software Hardware

Android bet aggressively on NFC, and kind of lost on that. Their NFC is apparently good, not that it matters, but BLE has been bad. In frustration vendors like Samsung started rolling their own stacks. Official support came in 4.3 Jellybean. With hardware support Samsung s3 and note 2 and up, Htc One and up , LG Optimus 4x, Nexus 4, Optimus G and up. Your results may vary though. Grabbing a new phone is probably your best bet.

Apple has aggressively implemented BLE as far back as 2011 in the iPhone 4s and 2012 for most of the rest of their products. The API is basically the same for both (corebluetooth). The experience is really nice. Further the Noble abstraction layer is available for BLE on macs via JS.

Page 32: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Platform Support - Apple

• Mac/IOS- 2011 Mac Air and Mac Mini+

• Ipad 3rd gen+ • 2012 Macbook

Pros+

• IOS5+ • OSX 10.7.2 Lion+

Software Hardware

Apple has aggressively implemented BLE as far back as 2011 in the iPhone 4s and 2012 for most of the rest of their products. The API is basically the same for both (corebluetooth). The experience is really nice. Further the Noble abstraction layer is available for BLE on macs via JS.

Page 33: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Platform Support - Linux

• Linux CSR • Broadcom

• bluez or bluepy

Software Hardware

Linux -

Page 34: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE • Choosing your BLE solution • Platform support • Code (javascript)• Conclusion

Agenda

Page 35: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code!

Noble - unified cross platform abstraction Mac / Linux Mobile soon thanks to @monteslu

• Discover peripherals • Connect • Discover Services • Discover Characteristics • Use Characteristics

Lifecycle

That being said, Noble, on Mac and Linux, is becoming a unifiedhttps://www.npmjs.org/package/noble

So based on that platform supportmac linux have something called noble in JS that ties everything together cross platformmore abstractions possible, our very own Luis Montes trying to help with some of that bringing the noble js abstractions to phonesI wrote the unofficial punch through NPM package on noblewe like this so well go there

Page 36: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code! - Discover Devices

You search for a device based upon a service it has that you want. IE give me devices who have a heart rate service. You can leave that scan going for.. ever really. More likely you want to start a scan and stop it every couple minutes to see if your device popped up.

Page 37: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code! - Connect and Discover Services

When you get a hit, your callback will be called with a peripheral. Keep this around so you can disconnect it later.You might check that you got the right one, and stop scanning.We still actually need to discover the service so we can get the juicy characteristics were after. so do that with another callback.Now your callback gets called with a service.

Page 38: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code! - Discover Characteristics

Ask it for specific characteristics that you’re looking for like heart rate measurement. When your callback gets called you need to save a copy of that characteristic somewhere.

Page 39: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code! - Save and Use Characteristics

Now that you've got a characteristic you can set a notify on it so that function gets called every time theres new data, you can write to it to send data, or read from it.

Page 40: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Code! - Kappa

npm install ble-bean

So now a real world exampleNow, Ive taken all that for the bean for instance and wrapped it in a library called ble-bean so it looks like thishttps://www.npmjs.org/package/ble-bean

Page 41: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

• Overview of BLE • Choosing your BLE solution • Platform support • Code (javascript) • Conclusion

Agenda

Page 42: Desert Code Camp 2014.2 Intro to Bluetooth Low Energy

Conclusion

• Stick with BLE its still maturing • Buy a bean • feel free to email me with questions • check out this BLE hackathon at Arcosanti

Ive also written ble for iOS and punch through offers a lot of stuff therealso checkout hackathon upcoming at arcostantiI may or may not be able to attend :)https://www.facebook.com/events/503068346497493/

Basically all of these BLE chips are arm chip with a BLE stack on them. Arm They license their silicon designs and thumb (assembly) instruction set. But then all manufacturers made their own C abstraction above that. Tower of babel now.Arm trying to reunify ala arduino.. mbed. As we said Nordic is the only chip that has gcc, and thus its also being developed for armIm working there right now