Bluetooth

Android Bluetooth HAL icon

Android provides a default Bluetooth stack that supports both Classic Bluetooth and Bluetooth Low Energy. Using Bluetooth, Android devices can create personal area networks to send and receive data with nearby Bluetooth devices.

In Android 4.3 and later, the Android Bluetooth stack provides the ability to implement Bluetooth Low Energy (BLE). To fully leverage the BLE APIs, follow the Android Bluetooth HCI Requirements. Android devices with a qualified chipset can implement either Classic Bluetooth or both Classic Bluetooth and BLE. BLE is not backwards compatible with older Bluetooth chipsets.

In Android 8.0, the native Bluetooth stack is fully qualified for Bluetooth 5. To use available Bluetooth 5 features, the device needs to have a Bluetooth 5 qualified chipset.

Android architecture

A Bluetooth app communicates with the Bluetooth process through Binder. The Bluetooth process uses JNI to communicate with the Bluetooth stack and provides developers with access to various Bluetooth profiles. This diagram shows the general structure of the Bluetooth stack:

Android Bluetooth architecture
Figure 1. Android Bluetooth architecture
App framework
At the app framework level is app code, which uses the android.bluetooth APIs to interact with the Bluetooth hardware. Internally, this code calls the Bluetooth process through the Binder IPC mechanism.
Bluetooth app
The Bluetooth app, located in packages/modules/Bluetooth/android/app, is packaged as an Android app and implements the Bluetooth profiles at the Android framework layer. This app calls into the native Bluetooth stack through JNI.
JNI
The JNI code associated with android.bluetooth is located in packages/modules/Bluetooth/android/app/jni. The JNI code calls into the Bluetooth stack when certain Bluetooth operations occur, such as when devices are discovered.
Bluetooth stack
The default Bluetooth stack is provided in AOSP and is located in packages/modules/Bluetooth/system. The stack implements the generic Bluetooth HAL and customizes it with extensions and configuration changes.
Vendor implementation
Vendor devices interact with the Bluetooth stack using the Hardware Interface Design Language (HIDL).

HIDL

HIDL defines the interface between the Bluetooth stack and the vendor implementation. To generate the Bluetooth HIDL files, pass the Bluetooth interface files into the HIDL generation tool. The interface files are located in hardware/interfaces/bluetooth.

Bluetooth stack development

The Android Bluetooth stack is a fully qualified Bluetooth stack. The qualification listing is on the Bluetooth SIG website under QDID 169365.

The core Bluetooth stack resides in packages/modules/Bluetooth. Development happens in AOSP, and contributions are welcome.

Android 7.x and lower architecture

A Bluetooth system service communicates with the Bluetooth stack through JNI and with apps through Binder IPC. The system service provides developers with access to various Bluetooth profiles. This diagram shows the general structure of the Bluetooth stack:

Android Bluetooth architecture
Figure 2. Android 7.x and lower Bluetooth architecture
App framework
At the app framework level is app code, which uses the android.bluetooth APIs to interact with the Bluetooth hardware. Internally, this code calls the Bluetooth process through the Binder IPC mechanism.
Bluetooth system service
The Bluetooth system service, located in packages/apps/Bluetooth, is packaged as an Android app and implements the Bluetooth service and profiles at the Android framework layer. This app calls into the HAL layer through JNI.
JNI
The JNI code associated with android.bluetooth is located in packages/apps/Bluetooth/jni. The JNI code calls into the HAL layer and receives callbacks from the HAL when certain Bluetooth operations occur, such as when devices are discovered.
HAL
The hardware abstraction layer defines the standard interface that the android.bluetooth APIs and Bluetooth process call into and that you must implement to have your Bluetooth hardware function correctly. The header file for the Bluetooth HAL is hardware/libhardware/include/hardware/bluetooth.h. Additionally, review all of the hardware/libhardware/include/hardware/bt_*.h files.
Bluetooth stack
The default Bluetooth stack is provided for you and is located in system/bt. The stack implements the generic Bluetooth HAL and customizes it with extensions and configuration changes.
Vendor extensions
To add custom extensions and an HCI layer for tracing, you can create a libbt-vendor module and specify these components.

Implement the HAL

The Bluetooth HAL is located in /hardware/libhardware/include/hardware/bluetooth.h. The bluetooth.h file contains the basic interface for the Bluetooth stack, and you must implement its functions.

Profile-specific files are located in the same directory. For details, see the HAL File Reference.