Developing an Android App with Bluetooth Low Energy (BLE)

About BLE

Bluetooth Low Energy (BLE) is the intelligent, power-friendly version of Bluetooth wireless technology. It is also marketed as Bluetooth Smart, started as part of the Bluetooth 4.0 Core Specification.

BLE plays a role in the creation of smarter, more compact, more affordable, less complex devices. It was designed by Nokia initially as Wibree before being adopted by the Bluetooth Special Interest Group (SIG).

About BLE in Android

BLE was introduced in Android 4.3 (API level 18) as built-in platform support in the Central role. Android provides APIs to discover devices, query for services and transmit information by the applications.

Mobile app (for example, Home Automation Android Application) sends and receives data via BLE

Common use cases for BLE include transferring small amounts of data between nearby devices and interacting with proximity sensors to give users a customized experience based on their current location.

BLE services and data transfer methods

A BLE service must be created to extend the Android Service component, which takes care of notifications on the connection status and the data status.

Your application should use the right BLE operations — Read, Write and Notify/Indicate — depending on data communication needs. The BLE protocol specification allows that the maximum data payload size for these operations is 247 bytes, or in the case of read operations, 249 bytes.

  • Read: If the data is to be received on demand. This request is done by the Master on a specific attribute and the Slave responds with the requested value.
  • Write: If data needs to be sent. This is requested by the Master on a specific attribute with a new value to be written.
  • Notify/Indicate: Data is to be received without first being requested. This operation is initiated by the Slave when a new value is written to a notify-supported characteristic (see below). If the Master has subscribed to notification on that characteristic, then the new value is pushed to the client when it is written. Notification cannot be supported by Slave; it must be supported from the Master for the actual data transmission to occur.

Device should support BLE

As an initial step, Bluetooth should be supported on the mobile device. Using the BluetoothAdapter class from Android, the BLE service can be supported as follows:

  //Initializing the Bluetooth Adapter
  final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BluetoothService);
  mBluetoothAdapter = btManager.getAdapter();

Runtime permissions

For seamless communication, it is important to request the following runtime permissions from the user to list available BLE devices:

  • Phone Location (for searching nearby devices)
  • App location
  • Bluetooth

Declare permissions in the AndroidManifest.xml file as follows:

  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

BLE scanning in different Android versions

Android has deprecated some of the APIs from the Oreo version. For applications with the Nougat version, you can use startLeScan method; after the Nougat version, use startScan method.

Background for BLE communication (GATT Profile)

Here are other important terms to understand before implementing BLE communication:

GATT: The Generic Attributes Profile (GAP) defines how the data is exchanged using predefined attributes.

UUID: Universally Unique ID is a 128-bit number used to uniquely identify information in computer systems.

Characteristics: Characteristics contain a single logical value and are defined attributes types. We can understand characteristics as containers for user data.

Services: Services are collections of relationships and characteristics to other services that encapsulate the behavior of part of a device.

Descriptor: Descriptors are placed within the characteristic definition and after the characteristic value attribute. GATT characteristic descriptors are mostly used to provide the client with metadata.