Bluetooth Low Energy

Introduction

Bluetooth low energy wireless technology gives context to the environment around you.It perfect for devices that run for long periods on power sources, such as coin cell batteries. Native support for Bluetooth technology on every major operating system enables development for a broad range of connected devices.

Key Benefit

  • low power consumption.
  • small size.
  • connectivity to mobile phones.
  • low cost,robust,efficient.
  • multi-vendor interoperability.
  • global availability, license free.

How low power work?

BLE device remains in sleep mode constantly except for when a connection is initiated. The actual connection times are only a few ms, unlike Bluetooth which would take ~100mS. The reason the connections are so short, is that the data rates are so high at 1 Mb/s.

How achieved?

  • By keeping the radio off.
  • Lower standby time, Faster connection, Lower peak power.
  • BLE technology uses only 3 advertising channels.

Application of BLE

Bluetooth with its LE functionality powers the Internet of Things. You wake up and go for a run with a heart rate monitor that communicates with your smart watch, then listen to music through your shower head.

  • Transferring small amounts of data between nearby devices.
  • Interacting with proximity sensors like Google Beacons to give users a customized experience based on their current location.
  • It can enable proximity detection(I’m in the car).
  • It can enable presence detection (Turn the lights on when I walk around the house).
  • It can send data from anything to the Internet (I’ve walked 4,000 steps today)
  • It can control everything (Unlock doors)
  • It connects everything to the Internet (check if you switched the lights off)

Key Terms and Concepts

1.Generic Attribute Profile (GATT) : The GATT profile is a general specification for sending and receiving short pieces of data known as “attributes” over a BLE link. All current Low Energy application profiles are based on GATT.

  • The Bluetooth SIG defines many profiles for Low Energy devices. A profile is a specification for how a device works in a particular application. Device can implement more than one profile. For example, a device could contain a heart rate monitor and a battery level detector

 

2.Attribute Protocol (ATT)—GATT is built on top of the Attribute Protocol (ATT). This is also referred to as GATT/ATT. ATT is optimized to run on BLE devices. To this end, it uses as few bytes as possible. Each attribute is uniquely identified by a Universally Unique Identifier (UUID), which is a standardized 128-bit format for a string ID used to uniquely identify information. The attributes transported by ATT are formatted as characteristics and services.

3.Characteristic—A characteristic contains a single value and 0-n descriptors that describe the characteristic’s value. A characteristic can be thought of as a type, analogous to a class.

4.Descriptor—Descriptors are defined attributes that describe a characteristic value. For example, a descriptor might specify a human-readable description, an acceptable range for a characteristic’s value, or a unit of measure that is specific to a characteristic’s value.

5.Service—A service is a collection of characteristics. For example, you could have a service called “Heart Rate Monitor” that includes characteristics such as “heart rate measurement”. You can find a list of existing GATT-based profiles and services on bluetooth.org.

6.Central vs. peripheral : The device in the central role scans, looking for advertisement, and the device in the peripheral role makes the advertisement.For example , you have an phone and battery level detector that is a BLE device. The phone supports the central role and the activity tracker supports the peripheral role.

7.GATT server vs. GATT client This determines how two devices talk to each other once they’ve established the connection.For example , App is the GATT client. The app gets data from the GATT server, which is a BLE Battery Level Detector.

Android bluetooth low energy example

In the example, the Android app running on an Android device is the GATT client. The app gets data from the GATT server, which is a BLE Battery Level Detector that supports the Battery Level Service.

Key Classes

BluetoothDevice: Represents a remote Bluetooth device. You create a connection with the respective device or query information about it, such as the name, address, class, and bonding state. 

BluetoothAdapter: You perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of paired devices, instantiate a BluetoothDevice using a known MAC address.getRemoteDevice(String) to create one representing a device of a known MAC address or get one from the set of bonded devices returned by getBondedDevices().Use getBluetoothLeScanner() to get an instance of BluetoothLeScanner.

BluetoothGatt: Provides Bluetooth GATT functionality to enable communication with Bluetooth Smart or Smart Ready devices.

BluetoothLeScanner: Class provides methods to perform scan related operations for Bluetooth LE devices. An application can scan for a particular type of Bluetooth LE devices using ScanFilter. It can also request different types of callbacks for delivering the result.

BLE Permissions

You need BLUETOOTH permission to perform requesting a connection, accepting a connection, and transferring data.You must also declare the BLUETOOTH_ADMIN permission for device discovery or manipulate Bluetooth settings.

Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

Get the BluetoothAdapter

The BluetoothAdapter represents the device’s own Bluetooth adapter (the Bluetooth radio). There’s one Bluetooth adapter for the entire system, and your application can interact with it using this object.

Enable Bluetooth

If Bluetooth but disabled, then you can request that the user enable Bluetooth without leaving your application.

Scan BLE Devices

To scan BLE devices, you use the startScan() method.Scanning is battery-intensive, you should observe the following guidelines:

  • As soon as you find the desired device, stop scanning.
  • Never scan on a loop, and set a time limit on your scan. A device that was previously available may have moved out of range, and continuing to scan drains the battery.

startScan() method takes a ScanCallback as a parameter. You must implement this callback for results are returned.

The following snippet shows how to start and stop a scan

 

In this example, app provides an activity BluetoothDetectorActivity to connect, display data, and display GATT services and characteristics supported by the device.

Ble Client
Battery Level Detector

Connecting to a GATT Server(BLE Device)

Based on user input,DeviceControlActivity communicates with a Service called BluetoothLEService, which interacts with the BLE device via the Android BLE API

BluetoothGattCallback:Used to deliver results to the client, such as connection status, as well as any further GATT client operations. 

The first step in interacting with a BLE device is connecting to it (connecting to the GATT server on the device). To connect to a GATT server on a BLE device, you use the connectGatt() method. This method takes three parameters: a Context object,autoConnect, and a reference to a BluetoothGattCallback

When a particular callback is triggered, it calls the broadcastUpdate() helper method and passes it an action. Data parsing in this section is performed in accordance with the Bluetooth Battery Service Measurement profile specifications

 

To enable or disable notifications for a given characteristic setCharacteristicNotification to
BluetoothGatt.Reads the requested characteristic from the associated remote device set
readCharacteristic.

 

In BatteryDetectorActivity, these events are handled by a BroadcastReceiver.

The Following snippet show display Gatt connected service

Connect Bluetooth service

Closing the Client App

Once your app has finished using a BLE device, it should call close() so the app can release resources appropriately.

How to test BLE Client?

The BLE Peripheral Simulator is an Android app that allows developers to try out new features of app Bluetooth without the need for a BLE Peripheral Device.

BLE Peripheral with one of three services:

  • Battery Service
  • Heart Rate Service
  • Health Thermometer Service

Use the  Bluetooth features to connect to the app to Read and Write Characteristics, Subscribe to Notifications for when the Characteristics change, and Read and Write Descriptors.

The device in the central role scans, looking for advertisement of Battery Level Detector.

The device in the peripheral role makes the advertisement of Battery Level.

Download this project from GitHub.

 

Related Post

Android MQTT Client

Creating and Monitoring Geofences

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *