Bluetooth Low Energy (BLE) is widely used in healthcare, fitness, security, automotive and entertainment. With recent IoT trends like home automation, beacons or asset tracking, more and more smart devices become available. ABI Research predicts 1.6 billion annual shipments of BLE devices by 2023.

bluetooth-mobile-mood-1

Read this guide to learn all about Bluetooth LE, what you can build with it, and how to use it in your Desktop, Mobile or Embedded applications:

What is Bluetooth LE?

Bluetooth Low Energy, also known as Bluetooth LE or BLE, is a wireless network technology and part of the Bluetooth Core Specification since version 4.0. It requires considerably less power and cost, while maintaining a similar communication range as Classic Bluetooth. Operating systems like iOS, Android, macOS, Linux and Windows natively support Bluetooth Low Energy.

Bluetooth-logo

Differences of Bluetooth Classic and Bluetooth Smart

Bluetooth Smart is another marketing name for BLE and hints at the fact that it is intended for smart devices. While it’s part of Bluetooth, it is not simply a smaller version of Bluetooth Classic. BLE has entirely different design goals and features. There’s a big difference in technical specification, implementation and the types of applications the two variants of Bluetooth are suitable for.

BLE works on the same 2.4 GHz frequency as Classic Bluetooth, but it transfers small amounts of data at low speeds. Bluetooth Low Energy devices can operate for months, even years, on coin-cell batteries. Some of the notable differences include:

Bluetooth Classic

  • Used for streaming applications such as audio streaming and file transfers.
  • Not optimized for low power, has a higher data rate.
  • Operates over 79 RF (Radio Frequency) channels.
  • Discovery operates on 32 channels and is thus slower.

Bluetooth Smart

  • Used for sensor data, control of devices, and low-bandwidth applications.
  • Low power, low duty data cycles.
  • Operates over 40 RF channels
  • Connections are established faster (discovery occurs on 3 channels).

Bluetooth Smart keeps the radio idle while not in use and transmits small data packets to require very little power. This ensures a long life-time for battery-powered smart devices.

Wearables, Smart Home Devices and IoT (Internet of Things)

The Internet of Things aims to interconnect everything. Wearables like smart watches and many home automation devices already take advantage of Bluetooth Low Energy. You can connect many different types of sensors and gadgets to track and report data, automate chores, and improve your every-day life. Developers have used Bluetooth for a long time already, and BLE allows Bluetooth-enabled devices to be around for all kinds of IoT applications in the future. All Bluetooth Smart Ready Devices support the BLE technology.

smart-home-3148026_1920

But BLE and Bluetooth in general also have some disadvantages in the smart devices sector, most notably the data throughput. 

Disadvantages of Bluetooth LE

  • The maximum data rate is 1-2 Mbps. BLE is thus not suited for bandwidth-heavy operations like high-quality video streaming. 
  • The communication range is limited. Bluetooth was designed for short range applications.
  • Small BLE-only devices can not directly access the internet. You require another smart device that supports BLE and has an IP connection as well. Smart devices using WiFi can directly connect to access points for internet connection, which can be an advantage.

Benefits of Bluetooth LE

  • Extremely low power consumption even compared to other low power technologies. BLE stays idle, keeps the radio off, and only communicates when necessary. 
  • Ability to run for years on standard, coin-cell batteries
  • Low cost for modules and usage in microchips.
  • Multi-vendor interoperability. Most notably, smartphones support BLE and can connect to all devices that are Bluetooth Smart Ready.

To counteract the limited communication range of Bluetooth, BLE supports mesh topology networks. Intermediate devices can forward data to increase the overall range. This means that adding more smart devices to your BLE network can improve connectivity to reach devices that are farther away. It is thus expected to overtake its nearest competitors (ZigBee and WiFi) in smart home systems. 

BLE Services and Characteristics: The Generic Attribute Profile (GATT)

To implement Bluetooth communication with BLE devices, we first need some understanding of the basic protocol underneath. There are multiple protocols and profiles defined in the Bluetooth 4.0+ specifications. The Generic Attribute Profile (GATT) is most commonly used for BLE wearables and IoT. Devices either take the role of a Peripheral (server that advertises a service), or Central (client that connects to a service).

bluetooth-central-peripheral

To realize communication and device interoperability, GATT works with predefined Services and Characteristics. Several services can also be grouped in a Profile. While the BLE protocol itself does not include profiles, Bluetooth SIG or peripheral designers usually create profiles to group several services that commonly go together. For example: The Heart Rate Profile combines the Heart Rate Service and the Device Information Service. See the full list of official GATT profiles here.

GATT Profile Architecture Graphic

Bluetooth SIG provides predefined services as XML representations for many standard cases. These configurations for services, characteristics and descriptors ensure compatibility with all BLE devices that provide a similar service.

Bluetooth LE services group one or more input and output Characteristics. Services and characteristics are identified by a 128bit unique identifier UUID. Short 16 bit identifier versions are used for standard services and characteristics. Custom ones must use full size 128 bit identifiers. Devices usually also provide a name string for their service.

Characteristics are the actual logical input and output nodes. Data can be readable, writable or both. The service can also notify about changes. A Characteristic consists of information like descriptive metadata, its value, as well as permissions and notifications.

Each characteristic value holds binary data. Services and clients must know the format, size and order of the data themselves to make sense of the transmitted information. Services can provide a characteristic presentation format descriptor to help the client parse and format the binary data.

Note: Advanced dual-mode BLE devices can also operate in multiple roles and advertise or consume different services concurrently. For example, your smartphone has the Central role when communicating with your smartwatch, but acts as a Peripheral while communicating with a PC.

How to Integrate Bluetooth LE in Your Application and Connect to Devices

The previous section of this guide introduced Bluetooth LE and how communication between BLE devices works. To build applications using BLE you can aim to create your own smart device (Peripheral), client application (Central), or both.

Create Your Own BLE Smart Device: Embedded Development with Qt Bluetooth

The most important programming language for embedded development and smart devices is C++. It offers maximum performance and control, which is important for low-energy devices. See the Embedded Systems Programming Beginner's Guide for a general introduction on how to develop for embedded platforms.

C++ also supports cross-platform development, with Qt being the most popular framework. Qt has a great set of features and an active community. To implement your own Bluetooth Peripherals or Clients with ready-made C++ networking components, use the Qt Bluetooth Low Energy API.

Arduino is a very beginner-friendly board for starting with embedded development. To get an overview of different embedded boards see the Best Microcontrollers for Embedded Computing post.

ESP32-arduino-board

The Arduino ESP32 is a powerful Microcontroller Unit that offers WiFI and Bluetooth LE. You can use the ESP32 Arduino libraries to test reading, notification and writing of Characteristics with your Central application. See the Arduino ESP32 examples in the documentation for snippets on how to communicate with the board from your Qt app.

Develop Bluetooth LE Apps and Connect to Smart Devices with QML and Felgo

Qt is a C++ framework and offers great stability and performance. But C++ can be challenging compared to more flexible languages like JavaScript, especially when it comes to UI. The great thing about Qt is that it combines the best of both worlds, with the QML language.

QML is a declarative language, that makes defining user interfaces and their behavior easy and intuitive. It supports JavaScript for adding logic, and removes the need to touch C++. For performance-critical tasks (e.g. to integrate other libraries), you can fall back to C++. Developing with QML is extremely fast, reduces complexity, and saves code.

hot-reload-3 (1)

Felgo extends the Qt framework with useful libraries and services, and also includes Bluetooth LE APIs that you can use from QML. The Bluetooth LE QML API wraps the Qt Bluetooth Low Energy implementation. It provides you with a fast and simple way to integrate IoT, wearables and custom devices to your apps. You can create Felgo applications for Desktop, Mobile or Embedded platforms and connect to smart devices without the need to write C++ code.

Bluetooth LE QML Example: Communicate with BLE Battery Service

This example shows you how to read the battery of a Bluetooth Low Energy device that is battery-operated and provides the Battery Service. If you don’t own a device, you can also use an Android phone to simulate the service. Install the BLE Peripheral Simulator app on your Android device and run the Battery Service emulation.

felgo-bluetooth-example-battery-android-1-1

In our Central application on Desktop or Mobile, we can connect to the Peripheral and read the battery. Here is an example of how an implementation with the Bluetooth LE QML API looks like:

Test on your mobile phone now! Run This Example
import Felgo 3.0
import QtQuick 2.0

App {
  NavigationStack {
    Page {
      title: qsTr("BLE Battery")
      
      Rectangle {
        anchors.centerIn: parent
        width: parent.width * 0.6
        height: parent.height * 0.8
        radius: dp(30)
        border.color: 'black'
        border.width: dp(5)
        
        Rectangle {
          anchors.bottom: parent.bottom
          width: parent.width
          height: parent.height * batteryCharacteristic.value / 100
          color: batteryCharacteristic.value > 80 ? 'green' : (batteryCharacteristic.value > 30 ? 'orange' : 'red')
          radius: parent.radius
        }
      }
      
      AppText {
        anchors.centerIn: parent
        text: batteryCharacteristic.value + '%'
        fontSize: dp(15)
      }
    }
  }
  
  BluetoothLeManager {
    discoveryRunning: true
    
    BluetoothLeDevice{
      id: myBleDevice
      
      BluetoothLeService {
        uuid: 0x180F // Battery Service
        BluetoothLeCharacteristic {
          id: batteryCharacteristic
          uuid: 0x2A19 // Battery Characteristic
          dataFormat: 4 // 0x04 for uint8
          
          onValueChanged: {
            // Value updates
            console.log('Battery Level Changed', value)
          }
        }
      }
    }
    
    onDeviceDiscovered: {
      // Match device with desired service UUID
      if (device.services.indexOf('{0000180f-0000-1000-8000-00805f9b34fb}') > -1) {
        myBleDevice.setDevice(device, true)
        discoveryRunning = false
      }
    }
  }
}

If you want to quickly try it yourself, get a second mobile device and run the above example with the Felgo Developer App on Android or iOS. Once the two devices are connected, you can change the battery value on your BLE simulator and press notify. The QML app on the second device then shows the updated value.

felgo-bluetooth-example-battery-iphone-1-1

It takes very little code to implement the BLE connection with QML. The BluetoothLeManager discovers BLE devices that we process in the onDeviceDiscovered callback. The application then filters for the Battery Service using the UUID. The UUID is standardized for each type of GATT service. You can find a list of such services and the associated UUIDs here.

To open a communication channel, connect the BluetoothLeDevice using the setDevice function. You can then read or write BluetoothLeCharacteristics of the configured BluetoothLeService, in this case the battery percentage.

Bluetooth LE Demo App: Heart Rate Monitor

Felgo offers many open-source demos and examples you can use to build your cross-platform application. The Bluetooth LE Heart Rate Demo shows how to connect to a Heart Rate sensor and display the result.

ble-heart-rate-monitor-1ble-heart-rate-monitor-2

If you have a device capable of transmitting the heart rate over BLE, you can fire it up and connect it to the demo app. QML makes it easy to create Bluetooth LE applications. Sign-up for Felgo and get started!

 

Download Felgo

 

 

More Posts Like This

TitleImage_HowToAddFelgoToQt

How to Add Felgo to Your Qt App on Desktop, Mobile or Embedded

Release-3-7-0-qt-tooling-cloud-builds-wasm-web-editor

Release 3.7.0: Build Your Desktop Qt App with Cloud Builds and Preview Code with Online QML Web Editor

Release-3-7-0-android-ios-native-app-integration-for-animationsn-3d-charts-games-1

Native App Integration: How to Add Smooth Animations, 3D, Charts or Mini-Games with a Custom Qt View in Xcode or Android Studio

Release-3-7-0-developer-app-new-demos-examples-githubr

Release 3.7.0: The New Developer App and the Best Examples and Demos for You

Release-3-7-0-felgo-bluetooth-le-qml-api-for-qt
Release 3.7.0: Bluetooth LE (Low Energy) with QML, Apple Sign In, Secure Keychain Storage

Release-3-5-0-webassembly-qt-and-felgo-apps-in-browserRelease 3.5.0: Run your Qt and Felgo Apps in the Browser with WebAssembly (WASM)


QML Hot Reload with Felgo Live for Qt