Make: Bluetooth
by Alasdair Allan , Don Coleman , and Sandeep Mistry
Copyright 2016 Alasdair Allan, Don Coleman, and Sandeep Mistry. All rights reserved.
Printed in the United States of America.
Published by Maker Media, Inc. , 1160 Battery Street East, Suite 125, San Francisco, CA 94111.
Maker Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact OReilly Medias institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Roger Stewart
- Production Editor: Melanie Yarbrough
- Copyeditor: Gillian McGarvey
- Proofreader: Christina Edwards
- Indexer: Last Look Editorial
- Interior Designer: David Futato
- Cover Designer: Jean Tashima
- Illustrator: Rebecca Demarest
- December 2015: First Edition
Revision History for the First Edition
- 2015-11-24: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781457187094 for release details.
Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc. The Maker Media logo is a trademark of Maker Media, Inc. Make: Bluetooth and related trade dress are trademarks of Maker Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Maker Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
978-1-457-18709-4
[LSI]
Preface
One of the drivers behind the recent explosive growth in the Internet of Things has been the Bluetooth Low Energy (LE) standard. What makes it so appealing is the ubiquity of smartphonesboth Apple and otherwisewith support for the standard, which means that thing-makers no longer have to worry about a display or user interface. And that means that things like smart lightbulbs can look a lot more like lightbulbs rather than a computer that happens to have a light attached to it.
Bluetooth LE is very different from classic Bluetoothin fact, pretty much the only thing that is the same is the name. Youre probably used to thinking about radios as sort of like serial connections working similarly to a phone call between two phonesonce you establish a connection, each person talks as the other listens and vice versa. They stay connected, even if neither is saying anything, until one hangs up and the call is ended.
In systems like these, data is transferred using a queue, and when data is read by the receiver, its erased from the queuejust as once my words reach your ears over the phone, theyre out of the communications channel. Effectively, this is how classic Bluetooth works.
What Makes Bluetooth LE Unique?
Instead of communicating via a point-to-point connection like a phone, a Bluetooth LE radio acts like a community bulletin board, with each radio acting as either a board or a reader of the board.
If your radio is a bulletin boardcalled a peripheral device in Bluetooth LE parlanceit posts data on its board for everyone in the community to read. If your radio is a readercalled a central device in Bluetooth LE termsit can read from any of the boards (the peripheral devices) that have information it cares about.
If you dont like that analogy, you can also think of peripheral devices as the servers in a client-server transaction. Similarly, central devices are the clients of the Bluetooth LE world because they read information from the peripherals.
But I Like Serial Connections!
Most (perhaps all?) of the Bluetooth LE radios breakout boards available to makers right nowthe RedBearLab BLE mini and the Adafruit Bluefruit LE, for instancepretend to look like serial devices for simplicitys sake and present a UART service to the user. Effectively these radios are faking old-style serial communication on top of the underlying bulletin board paradigm. Its a hack, and not a bad one.
Serial over Bluetooth LE makes the transition from classic Bluetooth to Bluetooth LE easier for people who are used to Serial Port Profile (SPP) and UART. However, theres a downside. It doesnt take advantage of Bluetooth LE. These Bluetooth serial services are just stuffing data across generic transmit and receive pipes. If a device uses serial, it also needs to define a protocol for the data that is sent and received.
Bluetooth LE offers device makers the ability to create Bluetooth LE devices with self-describing services. If the characteristics are well designed and the descriptors make sense, you can use services without documentation. (An example of this is using a Smartbotics Lightbulb with the LightBlue iOS app.)
Contrast this with other devices using SPP, like services where you need to learn the details of which bytes to send over the wire to turn on an LED and change its color. These devices need really good documentation and/or additional libraries to do simple stuff like set the color of a LED.
Returning to our bulletin board example, were creating a board (the service) that has a sticky note attached (known as a characteristic in Bluetooth LE parlance), which we can read, letting us know if the LED is on or off, or write toallowing us to control the LED.
Building a Custom Service
Unfortunately, until recently, building custom services for Bluetooth LE has been fairly complicated and not for the faint-hearted. However, its getting simpler as several good tools now exist to do most of the heavy lifting for you.
In light of that, we decided to look at one platformthe Nordic Semiconductor radiosand figure out a complete toolchain that would allow you to build a custom service for those radios, and make use of that service from an Arduino project. We picked this particular radio because its readily available and there is good library support.
This book is the result.
Who Should Read This Book?
This book provides an introduction to the topic of how to build and deploy Bluetooth LE sensors and devices. It takes a hands-on approach and teaches you how to build things and how to use them, not just the protocols and architecture behind the standard.
If youre a programmer or a maker who wants to get started with Bluetooth LE, this book is for you.
What You Should Already Know
This book is intended as an introduction to working with Bluetooth LE, and it assumes a technical background. However, we walk you through installing and using all the software and development environments youll need, including how to get started with the Arduino, Raspberry Pi, Node.js, and PhoneGap.
What You Will Learn
This book will guide you through building a series of connected projectsfrom lightbulbs, to locks, to beacons, and drones. It will walk you through your first hardware prototypes, show you how to improve them, and teach you how to build Bluetooth LE connected devices for the Internet of Things.
Whats In This Book
This chapter talks about the Bluetooth LE standard and the concepts youll need to know about before you can start building Bluetooth LE devices.
This chapter walks you through setting up the tools and software youll need to set up your development environment.