27,59 €
Bluetooth Low Energy, or Bluetooth Smart, is Wireless Personal Area networking aimed at smart devices and IoT applications. BLE has been increasingly adopted by application developers and IoT enthusiasts to establish connections between smart devices.
This book initially covers all the required aspects of BLE, before you start working on IoT projects. In the initial stages of the book, you will learn about the basic aspects of Bluetooth Low Energy—such as discovering devices, services, and characteristics—that will be helpful for advanced-level projects. This book will guide you through building hands-on projects using BLE and IoT. These projects include tracking health data, using a mobile App, and making this data available for health practitioners; Indoor navigation; creating beacons using the Raspberry Pi; and warehouse weather Monitoring. This book also covers aspects of Bluetooth 5 (the latest release) and its effect on each of these projects.
By the end of this book, you will have hands-on experience of using Bluetooth Low Energy to integrate with smart devices and IoT projects.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 232
Veröffentlichungsjahr: 2017
BIRMINGHAM - MUMBAI
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2017
Production reference: 1280817
ISBN 978-1-78839-944-9
www.packtpub.com
Author
Madhur Bhargava
Copy Editor
Laxmi Subramanian
Reviewers
Jacqueline Wilson
Gustavo Litovsky
Project Coordinator
Shweta H Birwatkar
Commissioning Editor
Gebin George
Proofreader
Safis Editing
Acquisition Editor
Divya Poojari
Indexer
Mariammal Chettiyar
ContentDevelopmentEditor
Dattatraya More
Graphics
Tania Dutta
Technical Editor
Sneha Hanchate
Production Coordinator
Shantanu Zagade
This book is fun, disguised as education.
Many folks say that the best way to learn something is by doing it. Even more effective, though, is building something tangible that can immediately be put to use. With this in mind, the book you have in your hands is an impressive accomplishment!
Let's not fool ourselves. Nobody will be able to survive in a technical field without the drive and ability to learn new things. Staying current in this industry can be quite challenging. Aside from the rapid pace with which technology evolves, there's often a palpable tedium of trudging through dry technical materials. One's mind can wander easily. Paragraphs have to be reread several times. Interruptions can feel like blessings.
This book is not like that.
Its title, IoT Projects with Bluetooth Low Energy, is a bit humble. To start off with the book, Madhur Bhargava breaks down the intricacies of the BLE protocol using a conversational voice. Almost without realizing it, the reader internalizes highly technical information, progressing to a full understanding of BLE. To read only that first chapter would be worth the investment of time and money into this book. But there's more! Madhur then accomplishes a feat rare among technical authors. Using the same delivery technique, he provides instructions on building a series of full-stack apps and covering several diverse technologies, without alienating the audience.
Fellow reader, the book's title might suggest that you will learn BLE -- and you will -- but you will learn so much more. Without even intending to.
Somehow, by chapter 4, I had coded multiple versions of mobile apps on both major platforms. I am not a mobile programmer; nor did I rely on copying and pasting code from the companion Github repo. Still, from Madhur's careful explanations, I understood every line of code used in the projects. The short, sweet introductions to Firebase, Xcode, Android Studio and emulators were enough to get me productive in each of them. While I do have programming experience, I haven't created much in Swift or Java. I have to credit the author with being thorough and keeping the instructions straightforward and focused. You will finish this book wanting to learn and do more. There are not enough books like this.
Finally, I should issue a warning. This book may start a costly addiction. If you don't already have the hardware for these projects, you will want it. I have Raspberry Pis, sensors, beacons, Android devices... all sitting in front of me as I write this.
Enjoy your adventure! Time for me to go play with my new toys now.
Jacqueline Wilson
Assistant Professor of Computer Science
Cecil College
Madhur Bhargava holds a bachelor’s degree in electronics and communication, after which he did a specialization in Wireless and Mobile Computing at CDAC ACTS Pune, India. He started his career at Electronic Arts as a trainee software engineer working on mobile games and eventually moved on to address problems in personalized healthcare, leveraging the power of mobile computing.
He is proficient in various mobile/embedded technologies and strives to be a software generalist. Since the inception of his career, he has worked extensively with various mobile technologies, such as Android, iOS, J2ME, Brew, Blackberry OS, and Xamarin. He has designed and developed Bluetooth Low Energy applications pertaining to the healthcare and automation domains for both Android and iOS platforms.
He believes that good software is the result of talented individuals working together as a communicative team in an Agile manner. He is both a Certified Scrum Master and a Certified Xamarin Developer.
Apart from work, he likes to spend time with family, read, and watch movies.
Dedicated to my family and to the team of amazing people I am working with. They are the reason that made this book happen.
Jacqueline Wilson has degrees in computer science and information management systems. She has an affinity for cutting edge technology, shiny gadgets, and technical books. Several years ago, she discovered the perfect way to channel her passion, and currently works as an assistant professor of computer science at Cecil College in North East, MD, where her aim is to make learning fun again. She lives in an old farmhouse filled with techie projects, along with her very accommodating husband, son, two dogs, four cats, and two fish.
Gustavo Litovsky is a lifelong engineer and expert on Bluetooth, Wi-Fi, and other wireless communications. As the founder and CEO of Argenox Technologies, he leads a team of engineers building amazing connected products for some of the world’s most recognizable brands, as well as numerous startups. Prior to founding Argenox, he spent time consulting at Samsung and several other companies on integrating wireless connectivity. He previously worked as an engineer at Texas Instrument’s Wireless Connectivity business unit, supporting customers with Wi-Fi, Bluetooth, and GPS solutions for products such as the Nike FuelBand, Pebble Smartwatch, and Motorola Droid Bionic. He is also an inventor and co-inventor of several patents.
I would like to thank my wife, Rebecca, without whose help and support nothing would get done. To my parents and children, for all their love and support.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788399447.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
What is Bluetooth Low Energy?
An Overview of Bluetooth Low Energy
The Need for Bluetooth Low Energy
Bluetooth Low Energy versus Bluetooth Classic
Architecture of Bluetooth Low Energy
Application
Host
Controller
Profiles
Services
Characteristics
Indications and Notifications
Bluetooth 5, Meshes, and Beacons
Summary
Setting Up
Introduction to Bluetooth Low Energy Sensors
Setting Up Your System to Write Android Applications
Setting Up Your System to Write iOS Applications
Configuring the Firebase Cloud Backend
Introduction to Raspberry Pi
Introduction to GitHub
Summary
Building a Service Explorer App
Central Peripheral Architecture for BLE
Services and Characteristics
Creating a Service Explorer App
Android
Application Code
iOS
Summary
Designing a Personal Tracking System
RSSI and Proximity
Indoor Proximity and Localization with an iTag
Designing the Tracking App
Android
iOS
Summary
Beacons with Raspberry Pi
Introduction to Beacons
Eddystone
iBeacon
Introduction to Raspberry Pi
Creating a Beacon with Raspberry Pi
Writing an App to Detect the Beacon
Android
Application code
iOS
Summary
Weather Monitoring Using BLE in Warehouses
Problem Statement and Solution Design
Temperature and Humidity Sensors
Interrogating Temperature and Humidity Sensors
Designing the Monitoring App
Android
iOS
Practical Applications
Idemitsu Museum of Arts
Salisbury Cathedral
Summary
Going Further
Bluetooth 5
8 Times the Broadcasting Capacity
Doubles the Speed
Quadruples the Range
Practical Use Cases
Philips Sonicare Toothbrush
Wahoo Balance Smartphone Scale
iLumi Smart Bulb
Eve Smart Light Switch
Danalock
Fobo
Tile
HAPIfork
Future of BLE
Dreem
BabyGigl
Future of IoT
Other Technologies
Products
Summary
Bluetooth Low Energy is one of the key cornerstones of the IoT paradigm. This book intends to bridge the gap between the theoretical and practical understanding of Bluetooth Low Energy by first introducing the reader to Bluetooth Low Energy and then creating a practical understanding of the subject on top of that theoretical foundation by building four hands-on Bluetooth Low Energy projects focused on IoT.
You will be leveraging existing popular mobile technologies (Android and iOS) to design IoT-oriented applications that will interact with various Bluetooth Low Energy based hardware and sensors such as Raspberry Pi, iTags, Fitness Trackers, and TI SensorTag.
Chapter 1, What is Bluetooth Low Energy, introduces the reader to Bluetooth Low Energy and its various building blocks—Profiles, Services, Characteristics, and Descriptors.
Chapter 2, Setting Up, sets up the development environment to execute the Android and iOS code samples included in the book.
Chapter 3, Building a Service Explorer App, solidifies our understanding of the concepts introduced in the first chapter by building a Service Explorer app that can be used to explore services on any Bluetooth Low Energy device. We eventually use the same app to explore services on a fitness tracker, read heart rate data, and upload it to a remote Firebase backend.
Chapter 4, Designing a Personal Tracking System, focuses on a key concept in IoT, that is, proximity. We design a Personal Tracker app using an iTag. The app uses the RSSI received from an iTag to approximate the distance between the app-hosting device and the iTag.
Chapter 5, Beacons with Raspberry Pi, introduces the reader to one of the key cornerstones of IoT, that is, Beacons, and the two primary Beacon protocols—Eddystone and iBeacon. In addition, we also configure a Raspberry Pi to be used as an Eddystone Beacon and write an Android and iOS app to detect it.
Chapter 6, Weather Monitoring Using BLE in Warehouses, shows you how to combine the knowledge that you gained in the previous chapters and address a real-life problem revolving around BLE and IoT. We introduce a new sensor type and build a Weather Monitoring app using that sensor.
Chapter 7, Going Further, discusses the future of BLE and IoT by introducing the reader to various use cases and products based on Bluetooth Low Energy, that are already available or will become available on the consumer market in the future.
This book will guide you through the installation of all the tools that you need to follow the code samples. Code samples introduced in various chapters are for both Android and iOS platforms hence you will need to install the Android Studio and XCode IDEs. Since simulators lack Bluetooth functionality, hence you will need physical Android and iOS devices to run the code samples. In terms of hardware, you will be needing a Raspberry Pi for the Code Lab specific for Chapter 5, Beacons with Raspberry Pi. For Chapter 4, Designing a Personal Tracking System, and Chapter 6, Weather Monitoring Using BLE in Warehouses, you will be needing a very low cost iTag and the Texas Instruments Sensor Tag. All of the hardware can be easily procured online.
This book is intended for anyone with a technical bend of mind who wants to gain practical knowledge of BLE via its usage in IoT projects. Although the book has a fairly practical undertone, it starts by building a theoretical foundation of Bluetooth Low Energy, finally using that foundation as a platform to design practical projects and solutions, and hence it can be picked up by novices and experts alike.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the
SUPPORT
tab at the top.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on
Code Download
.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/IoT-Projects-with-Bluetooth-Low-Energy. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/IoTProjectswithBluetoothLowEnergy_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
At a very basic level IoT can be described as a network of things (physical devices, vehicles, buildings, and what not), which when augmented with sensors and servers enables these objects or things to collect and exchange data. A major driver behind this growth has been the advent of a comparatively lesser-known technology known as Bluetooth Low Energy (BLE).
In this chapter, we discuss this technology in the light of the following topics:
An Overview of Bluetooth Low Energy
The Need for Bluetooth Low Energy
Bluetooth Low Energy versus Bluetooth Classic
Architecture of Bluetooth Low Energy
Profiles
Services
Characteristics
Indications
Notifications
Bluetooth 5, Meshes, and Beacons
Bluetooth Low Energy or Bluetooth Smart is a comparatively new wireless communication technology that was introduced by the Bluetooth Special Interest Group in 2010. Although the technology itself was being developed way earlier by Nokia around 2001 to 2006 under the name Wibree, it was not until 2007 that an agreement was reached with the various members of Bluetooth SIG that Wibree should now be included in the Core Bluetooth Specification, a task which was completed in 2010, when Wibree became a part of Bluetooth Core Specification version 4.0 as Bluetooth Smart, commonly known as Bluetooth Low Energy (its original name). The first mobile device to incorporate the 4.0 specification was iPhone 4S. However, as it is almost always with early adopters, the then iPhone 4S operating system did have some bugs regarding Bluetooth connectivity and range sometimes being poor. Bluetooth Smart technology has matured rapidly since then and come a long way. In fact, the dawn of the current IoT revolution is relying significantly on Bluetooth Low Energy for its success. According to an analysis done by IndustryArc.
Bluetooth Low Energy device shipments are forecast to increase to 8.4 billion units by 2020 at a mean annual growth rate of 29% which eventually will also lead to a surge in the number of IoT devices (things, if spoken semantically). We are seeing the early stages of this revolution all around us as a result of which, almost every handheld device and wearable nowadays has BLE capabilities.
So, what is it that makes Bluetooth Low Energy so special? How is Bluetooth Low Energy different from the good old regular Bluetooth? Why is every other wearable device (for example, Fitbit, Nike Fuelband, Apple watch and maybe your own smartphone) on the market using Bluetooth Low Energy (Couldn't they just do away with regular bluetooth and be happy about it)? We will explore the answer to all these questions and many others in this chapter. Furthermore, we will also discuss the architecture and what lies under the hood that makes Bluetooth Low Energy live up to its name.
So, coming to the very first question, what makes Bluetooth Low Energy so special? We will have to go back in time a little bit to address this. During the year 2001, the researchers at Nokia had already identified a number of scenarios, which were not being addressed by any of the existing Wireless Personal Area Network (WPAN) communication technologies,
The most common factors that came out as a result of studying these scenarios were:
Low power usage
Low cost
Minimal differences with current Bluetooth technology
Hence, what started in 2001 ended up being Bluetooth Low Energy or Bluetooth Smart in 2010. The technology was given a descriptive name, which also describes its real purpose of existence. Bluetooth Low Energy was designed for devices which had:
Low power requirements, operating on a coin cell for longer periods of time (months or even years)
Low cost
Industry standard wireless protocol, which can be easily adopted
And guess what? They finally succeeded in achieving all the set goals. If you remember the age of early mobile phones, the ones which had classic Bluetooth only, then you might remember that classic Bluetooth was a battery-expensive feature and continuous usage used to drain the battery on the device pretty quickly. Also, there were not many low-cost Bluetooth accessories available as there are now. However, with the advent of Bluetooth Low Energy (which consumes low power), all these things are bygones. We can now buy a Bluetooth tag, which supports and operates on Bluetooth 4.0, similar to the ones shown next for as low as 1-2 euros (Don't worry too much if you do not yet understand what this tag is used for. In the later chapters, we will build an application around the usage of this tag.):
Fitbit, one of the most common and popular fitness trackers, used for recording the body vitals and daily activity, has a battery life of 7-10 days after a full charge (source: https://www.fitbit.com/charge). Please note that this is a device which is working continuously day and night and even records data when you are sleeping:
Lastly, Bluetooth Low Energy is now being incorporated in each and every smartphone being rolled out. Apple which was one of the early adopters of Bluetooth Low Energy was later joined by Samsung, LG, Motorola, and every other major mobile device manufacturer out there. The popularity and adoption rate of the technology has already seen exponential growth and with the announcement of Bluetooth 5, which promises double the speed and four times the range, we will continue to see an even larger wave of Bluetooth Low Energy and IoT devices hitting the market.
Although Bluetooth Classic and Bluetooth Low Energy share many important things such as architecture, and both operate in 2.4 GHz ISM Band, the fundamental difference between them is that BLE is designed to consume less power. Due to this caveat, BLE is not an ideal candidate for applications such as streaming voice data (talking over the phone); however it makes BLE an excellent choice when it comes to communicating via exchanging small amounts of data over short periods of time. We shall discuss the communication differences between Bluetooth Classic and Bluetooth Low Energy in detail below.
Under Bluetooth Classic, when two or more devices want to talk to each other, then they always need to pair first (although pairing does happen when two devices communicate over Bluetooth Low Energy too, however; it is not mandatory in the case of BLE). Once the pairing has succeeded, an ad hoc network is established also known as the piconet:
A piconet can consist of a single master and up to seven slave devices. The devices can switch role by agreement (a slave can become a master at a later stage during the timeline of communication). Although a master can have up to seven slaves, at any point in time the master is addressing a single slave and the slave is supposed to listen when this happens. Also, it is important to note that being a slave to more than one master is certainly possible. This often results in interconnected piconets, also known as a scatternet:
Whether it is piconet or scatternet, the communication channel between the master and slave remains established even if no data is being exchanged and is only terminated when one (or both) of the parties (master/slave) explicitly decides to terminate the connection.
On the other hand, communication over Bluetooth Low Energy can be abstracted away as interacting with a really intelligent database. During this type of communication, each of the devices involved either plays the role of the database (known as peripheral in Bluetooth Low Energy terminology) or a listener (known as central in Bluetooth Low Energy terminology) of that database updates. Whenever new data is available, the database magically
